@memberjunction/ng-markdown 0.0.1 → 2.126.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +211 -43
- package/dist/lib/components/markdown.component.d.ts +210 -0
- package/dist/lib/components/markdown.component.js +553 -0
- package/dist/lib/components/markdown.component.js.map +1 -0
- package/dist/lib/extensions/code-copy.extension.d.ts +71 -0
- package/dist/lib/extensions/code-copy.extension.js +169 -0
- package/dist/lib/extensions/code-copy.extension.js.map +1 -0
- package/dist/lib/extensions/collapsible-headings.extension.d.ts +86 -0
- package/dist/lib/extensions/collapsible-headings.extension.js +240 -0
- package/dist/lib/extensions/collapsible-headings.extension.js.map +1 -0
- package/dist/lib/extensions/svg-renderer.extension.d.ts +38 -0
- package/dist/lib/extensions/svg-renderer.extension.js +126 -0
- package/dist/lib/extensions/svg-renderer.extension.js.map +1 -0
- package/dist/lib/markdown.module.d.ts +38 -0
- package/dist/lib/markdown.module.js +59 -0
- package/dist/lib/markdown.module.js.map +1 -0
- package/dist/lib/services/markdown.service.d.ts +101 -0
- package/dist/lib/services/markdown.service.js +310 -0
- package/dist/lib/services/markdown.service.js.map +1 -0
- package/dist/lib/types/markdown.types.d.ts +191 -0
- package/dist/lib/types/markdown.types.js +25 -0
- package/dist/lib/types/markdown.types.js.map +1 -0
- package/dist/public-api.d.ts +7 -0
- package/dist/public-api.js +14 -0
- package/dist/public-api.js.map +1 -0
- package/package.json +44 -6
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
const DEFAULT_OPTIONS = {
|
|
2
|
+
startLevel: 2,
|
|
3
|
+
defaultExpanded: true,
|
|
4
|
+
autoExpandLevels: undefined,
|
|
5
|
+
classPrefix: 'collapsible'
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Creates a marked extension that wraps heading sections in collapsible containers.
|
|
9
|
+
*
|
|
10
|
+
* This extension transforms headings into clickable toggles that can expand/collapse
|
|
11
|
+
* the content that follows them (until the next heading of equal or higher level).
|
|
12
|
+
*
|
|
13
|
+
* The key innovation is that child sections are properly NESTED inside parent sections,
|
|
14
|
+
* so collapsing a parent will hide all its children.
|
|
15
|
+
*
|
|
16
|
+
* Usage:
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { Marked } from 'marked';
|
|
19
|
+
* import { createCollapsibleHeadingsExtension } from './collapsible-headings.extension';
|
|
20
|
+
*
|
|
21
|
+
* const marked = new Marked();
|
|
22
|
+
* marked.use(createCollapsibleHeadingsExtension({ startLevel: 2 }));
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* The generated HTML structure (properly nested):
|
|
26
|
+
* ```html
|
|
27
|
+
* <div class="collapsible-section" data-level="2">
|
|
28
|
+
* <div class="collapsible-heading-wrapper">
|
|
29
|
+
* <h2 class="collapsible-heading" id="...">Parent Heading</h2>
|
|
30
|
+
* </div>
|
|
31
|
+
* <div class="collapsible-content">
|
|
32
|
+
* <p>Content under parent...</p>
|
|
33
|
+
* <div class="collapsible-section" data-level="3">
|
|
34
|
+
* <div class="collapsible-heading-wrapper">
|
|
35
|
+
* <h3 class="collapsible-heading" id="...">Child Heading</h3>
|
|
36
|
+
* </div>
|
|
37
|
+
* <div class="collapsible-content">
|
|
38
|
+
* <p>Content under child...</p>
|
|
39
|
+
* </div>
|
|
40
|
+
* </div>
|
|
41
|
+
* </div>
|
|
42
|
+
* </div>
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* Note: The toggle button is added dynamically by the component after rendering
|
|
46
|
+
* to avoid issues with Angular's HTML sanitizer.
|
|
47
|
+
*/
|
|
48
|
+
export function createCollapsibleHeadingsExtension(options) {
|
|
49
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
50
|
+
return {
|
|
51
|
+
renderer: {
|
|
52
|
+
// Mark headings with a special marker that we'll process in postprocess
|
|
53
|
+
heading(token) {
|
|
54
|
+
const { depth, text } = token;
|
|
55
|
+
const escapedText = text.toLowerCase().replace(/[^\w]+/g, '-');
|
|
56
|
+
const id = `heading-${escapedText}`;
|
|
57
|
+
// Only make headings collapsible at or below the start level
|
|
58
|
+
if (depth >= opts.startLevel) {
|
|
59
|
+
// Use a marker format that we'll parse in postprocess
|
|
60
|
+
// Format: <!--COLLAPSIBLE_HEADING:level:id:text-->
|
|
61
|
+
const marker = `<!--COLLAPSIBLE_HEADING:${depth}:${id}:${encodeURIComponent(text)}-->`;
|
|
62
|
+
return `${marker}<h${depth} id="${id}">${text}</h${depth}>\n`;
|
|
63
|
+
}
|
|
64
|
+
// Regular heading rendering for levels above startLevel
|
|
65
|
+
return `<h${depth} id="${id}">${text}</h${depth}>\n`;
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
hooks: {
|
|
69
|
+
postprocess(html) {
|
|
70
|
+
return restructureToNestedSections(html, opts);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Restructures flat HTML with heading markers into properly nested collapsible sections.
|
|
77
|
+
* This is the key function that creates the hierarchical structure needed for
|
|
78
|
+
* parent collapse to affect children.
|
|
79
|
+
*/
|
|
80
|
+
function restructureToNestedSections(html, opts) {
|
|
81
|
+
// Parse the HTML to find all content chunks and headings
|
|
82
|
+
const markerRegex = /<!--COLLAPSIBLE_HEADING:(\d):([^:]+):([^>]+)-->/g;
|
|
83
|
+
// Split HTML by markers while capturing the markers
|
|
84
|
+
const parts = [];
|
|
85
|
+
let lastIndex = 0;
|
|
86
|
+
let match;
|
|
87
|
+
while ((match = markerRegex.exec(html)) !== null) {
|
|
88
|
+
// Add content before this marker
|
|
89
|
+
if (match.index > lastIndex) {
|
|
90
|
+
const content = html.slice(lastIndex, match.index);
|
|
91
|
+
if (content.trim()) {
|
|
92
|
+
parts.push({ type: 'content', content });
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// Add the heading marker info
|
|
96
|
+
parts.push({
|
|
97
|
+
type: 'heading',
|
|
98
|
+
content: '', // Will be filled with the actual h tag
|
|
99
|
+
level: parseInt(match[1], 10),
|
|
100
|
+
id: match[2],
|
|
101
|
+
text: decodeURIComponent(match[3])
|
|
102
|
+
});
|
|
103
|
+
lastIndex = match.index + match[0].length;
|
|
104
|
+
}
|
|
105
|
+
// Add remaining content after last marker
|
|
106
|
+
if (lastIndex < html.length) {
|
|
107
|
+
const content = html.slice(lastIndex);
|
|
108
|
+
if (content.trim()) {
|
|
109
|
+
parts.push({ type: 'content', content });
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
// If no collapsible headings, return original HTML
|
|
113
|
+
if (!parts.some(p => p.type === 'heading')) {
|
|
114
|
+
return html;
|
|
115
|
+
}
|
|
116
|
+
// Now build the nested structure
|
|
117
|
+
return buildNestedStructure(parts, opts);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Determine if a heading level should start expanded based on options
|
|
121
|
+
*/
|
|
122
|
+
function shouldLevelBeExpanded(level, opts) {
|
|
123
|
+
// If autoExpandLevels is specified, use it
|
|
124
|
+
if (opts.autoExpandLevels !== undefined) {
|
|
125
|
+
return opts.autoExpandLevels.includes(level);
|
|
126
|
+
}
|
|
127
|
+
// Otherwise fall back to defaultExpanded
|
|
128
|
+
return opts.defaultExpanded;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Builds the nested HTML structure from the parsed parts.
|
|
132
|
+
*/
|
|
133
|
+
function buildNestedStructure(parts, opts) {
|
|
134
|
+
const result = [];
|
|
135
|
+
// Stack tracks open sections: { level, hasContent }
|
|
136
|
+
const sectionStack = [];
|
|
137
|
+
for (let i = 0; i < parts.length; i++) {
|
|
138
|
+
const part = parts[i];
|
|
139
|
+
if (part.type === 'content') {
|
|
140
|
+
// Regular content - just add it
|
|
141
|
+
result.push(part.content);
|
|
142
|
+
}
|
|
143
|
+
else if (part.type === 'heading' && part.level !== undefined) {
|
|
144
|
+
const level = part.level;
|
|
145
|
+
// Close any sections at same or higher level (lower number = higher level)
|
|
146
|
+
while (sectionStack.length > 0 && sectionStack[sectionStack.length - 1].level >= level) {
|
|
147
|
+
// Close the content div and section div
|
|
148
|
+
result.push('</div></div>');
|
|
149
|
+
sectionStack.pop();
|
|
150
|
+
}
|
|
151
|
+
// Determine expanded state for this specific level
|
|
152
|
+
const isExpanded = shouldLevelBeExpanded(level, opts);
|
|
153
|
+
const expandedClass = isExpanded ? '' : ' collapsed';
|
|
154
|
+
// Extract the actual h tag from the next content piece if it starts with it
|
|
155
|
+
let headingHtml = `<h${level} class="${opts.classPrefix}-heading" id="${part.id}">${part.text}</h${level}>`;
|
|
156
|
+
// Look ahead to find and remove the actual h tag from content
|
|
157
|
+
if (i + 1 < parts.length && parts[i + 1].type === 'content') {
|
|
158
|
+
const nextContent = parts[i + 1].content;
|
|
159
|
+
const hTagRegex = new RegExp(`<h${level}[^>]*id="${part.id}"[^>]*>[^<]*</h${level}>\\s*`);
|
|
160
|
+
const hTagMatch = nextContent.match(hTagRegex);
|
|
161
|
+
if (hTagMatch) {
|
|
162
|
+
// Use the actual h tag and remove it from content
|
|
163
|
+
headingHtml = hTagMatch[0].replace(`<h${level}`, `<h${level} class="${opts.classPrefix}-heading"`);
|
|
164
|
+
parts[i + 1].content = nextContent.replace(hTagRegex, '');
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
// Open new section
|
|
168
|
+
result.push(`<div class="${opts.classPrefix}-section${expandedClass}" data-level="${level}">`);
|
|
169
|
+
result.push(`<div class="${opts.classPrefix}-heading-wrapper">`);
|
|
170
|
+
result.push(headingHtml);
|
|
171
|
+
result.push('</div>');
|
|
172
|
+
result.push(`<div class="${opts.classPrefix}-content">`);
|
|
173
|
+
sectionStack.push({ level });
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
// Close any remaining open sections
|
|
177
|
+
while (sectionStack.length > 0) {
|
|
178
|
+
result.push('</div></div>');
|
|
179
|
+
sectionStack.pop();
|
|
180
|
+
}
|
|
181
|
+
return result.join('');
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Helper function to toggle a collapsible section programmatically
|
|
185
|
+
*/
|
|
186
|
+
export function toggleCollapsibleSection(sectionElement) {
|
|
187
|
+
const isCollapsed = sectionElement.classList.contains('collapsed');
|
|
188
|
+
const toggle = sectionElement.querySelector('.collapsible-toggle');
|
|
189
|
+
sectionElement.classList.toggle('collapsed');
|
|
190
|
+
if (toggle) {
|
|
191
|
+
toggle.setAttribute('aria-expanded', String(isCollapsed));
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Expand all collapsible sections in a container
|
|
196
|
+
*/
|
|
197
|
+
export function expandAllSections(container) {
|
|
198
|
+
const sections = container.querySelectorAll('.collapsible-section.collapsed');
|
|
199
|
+
sections.forEach((section) => {
|
|
200
|
+
section.classList.remove('collapsed');
|
|
201
|
+
const toggle = section.querySelector('.collapsible-toggle');
|
|
202
|
+
if (toggle) {
|
|
203
|
+
toggle.setAttribute('aria-expanded', 'true');
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Collapse all collapsible sections in a container
|
|
209
|
+
*/
|
|
210
|
+
export function collapseAllSections(container) {
|
|
211
|
+
const sections = container.querySelectorAll('.collapsible-section:not(.collapsed)');
|
|
212
|
+
sections.forEach((section) => {
|
|
213
|
+
section.classList.add('collapsed');
|
|
214
|
+
const toggle = section.querySelector('.collapsible-toggle');
|
|
215
|
+
if (toggle) {
|
|
216
|
+
toggle.setAttribute('aria-expanded', 'false');
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Expand sections to reveal a specific heading by ID
|
|
222
|
+
*/
|
|
223
|
+
export function expandToHeading(container, headingId) {
|
|
224
|
+
const heading = container.querySelector(`#${headingId}`);
|
|
225
|
+
if (!heading)
|
|
226
|
+
return;
|
|
227
|
+
// Find all ancestor collapsible sections and expand them
|
|
228
|
+
let current = heading.closest('.collapsible-section');
|
|
229
|
+
while (current) {
|
|
230
|
+
if (current.classList.contains('collapsed')) {
|
|
231
|
+
current.classList.remove('collapsed');
|
|
232
|
+
const toggle = current.querySelector(':scope > .collapsible-heading-wrapper .collapsible-toggle');
|
|
233
|
+
if (toggle) {
|
|
234
|
+
toggle.setAttribute('aria-expanded', 'true');
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
current = current.parentElement?.closest('.collapsible-section') || null;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
//# sourceMappingURL=collapsible-headings.extension.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collapsible-headings.extension.js","sourceRoot":"","sources":["../../../src/lib/extensions/collapsible-headings.extension.ts"],"names":[],"mappings":"AAwCA,MAAM,eAAe,GAAoB;IACvC,UAAU,EAAE,CAAC;IACb,eAAe,EAAE,IAAI;IACrB,gBAAgB,EAAE,SAAS;IAC3B,WAAW,EAAE,aAAa;CAC3B,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,MAAM,UAAU,kCAAkC,CAChD,OAAoC;IAEpC,MAAM,IAAI,GAAoB,EAAE,GAAG,eAAe,EAAE,GAAG,OAAO,EAAE,CAAC;IAEjE,OAAO;QACL,QAAQ,EAAE;YACR,wEAAwE;YACxE,OAAO,CAAC,KAAqB;gBAC3B,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,KAAK,CAAC;gBAC9B,MAAM,WAAW,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,SAAS,EAAE,GAAG,CAAC,CAAC;gBAC/D,MAAM,EAAE,GAAG,WAAW,WAAW,EAAE,CAAC;gBAEpC,6DAA6D;gBAC7D,IAAI,KAAK,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;oBAC7B,sDAAsD;oBACtD,mDAAmD;oBACnD,MAAM,MAAM,GAAG,2BAA2B,KAAK,IAAI,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AE,SAAiB;IACvE,MAAM,OAAO,GAAG,SAAS,CAAC,aAAa,CAAC,IAAI,SAAS,EAAE,CAAC,CAAC;IACzD,IAAI,CAAC,OAAO;QAAE,OAAO;IAErB,yDAAyD;IACzD,IAAI,OAAO,GAAuB,OAAO,CAAC,OAAO,CAAC,sBAAsB,CAAC,CAAC;IAC1E,OAAO,OAAO,EAAE,CAAC;QACf,IAAI,OAAO,CAAC,SAAS,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAC;YAC5C,OAAO,CAAC,SAAS,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;YACtC,MAAM,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC,2DAA2D,CAAC,CAAC;YAClG,IAAI,MAAM,EAAE,CAAC;gBACX,MAAM,CAAC,YAAY,CAAC,eAAe,EAAE,MAAM,CAAC,CAAC;YAC/C,CAAC;QACH,CAAC;QACD,OAAO,GAAG,OAAO,CAAC,aAAa,EAAE,OAAO,CAAC,sBAAsB,CAAC,IAAI,IAAI,CAAC;IAC3E,CAAC;AACH,CAAC","sourcesContent":["import { MarkedExtension, Tokens } from 'marked';\n\n/**\n * Options for the collapsible headings extension\n */\nexport interface CollapsibleHeadingsOptions {\n /**\n * The minimum heading level to make collapsible (1-6)\n * Headings at this level and below will be collapsible\n * @default 2\n */\n startLevel?: 1 | 2 | 3 | 4 | 5 | 6;\n\n /**\n * Whether sections should be expanded by default\n * @default true\n */\n defaultExpanded?: boolean;\n\n /**\n * Specify which heading levels should start expanded.\n * Takes precedence over defaultExpanded for specified levels.\n * @default undefined (uses defaultExpanded for all levels)\n */\n autoExpandLevels?: number[];\n\n /**\n * CSS class prefix for generated elements\n * @default 'collapsible'\n */\n classPrefix?: string;\n}\n\ninterface ResolvedOptions {\n startLevel: 1 | 2 | 3 | 4 | 5 | 6;\n defaultExpanded: boolean;\n autoExpandLevels?: number[];\n classPrefix: string;\n}\n\nconst DEFAULT_OPTIONS: ResolvedOptions = {\n startLevel: 2,\n defaultExpanded: true,\n autoExpandLevels: undefined,\n classPrefix: 'collapsible'\n};\n\n/**\n * Creates a marked extension that wraps heading sections in collapsible containers.\n *\n * This extension transforms headings into clickable toggles that can expand/collapse\n * the content that follows them (until the next heading of equal or higher level).\n *\n * The key innovation is that child sections are properly NESTED inside parent sections,\n * so collapsing a parent will hide all its children.\n *\n * Usage:\n * ```typescript\n * import { Marked } from 'marked';\n * import { createCollapsibleHeadingsExtension } from './collapsible-headings.extension';\n *\n * const marked = new Marked();\n * marked.use(createCollapsibleHeadingsExtension({ startLevel: 2 }));\n * ```\n *\n * The generated HTML structure (properly nested):\n * ```html\n * <div class=\"collapsible-section\" data-level=\"2\">\n * <div class=\"collapsible-heading-wrapper\">\n * <h2 class=\"collapsible-heading\" id=\"...\">Parent Heading</h2>\n * </div>\n * <div class=\"collapsible-content\">\n * <p>Content under parent...</p>\n * <div class=\"collapsible-section\" data-level=\"3\">\n * <div class=\"collapsible-heading-wrapper\">\n * <h3 class=\"collapsible-heading\" id=\"...\">Child Heading</h3>\n * </div>\n * <div class=\"collapsible-content\">\n * <p>Content under child...</p>\n * </div>\n * </div>\n * </div>\n * </div>\n * ```\n *\n * Note: The toggle button is added dynamically by the component after rendering\n * to avoid issues with Angular's HTML sanitizer.\n */\nexport function createCollapsibleHeadingsExtension(\n options?: CollapsibleHeadingsOptions\n): MarkedExtension {\n const opts: ResolvedOptions = { ...DEFAULT_OPTIONS, ...options };\n\n return {\n renderer: {\n // Mark headings with a special marker that we'll process in postprocess\n heading(token: Tokens.Heading): string {\n const { depth, text } = token;\n const escapedText = text.toLowerCase().replace(/[^\\w]+/g, '-');\n const id = `heading-${escapedText}`;\n\n // Only make headings collapsible at or below the start level\n if (depth >= opts.startLevel) {\n // Use a marker format that we'll parse in postprocess\n // Format: <!--COLLAPSIBLE_HEADING:level:id:text-->\n const marker = `<!--COLLAPSIBLE_HEADING:${depth}:${id}:${encodeURIComponent(text)}-->`;\n return `${marker}<h${depth} id=\"${id}\">${text}</h${depth}>\\n`;\n }\n\n // Regular heading rendering for levels above startLevel\n return `<h${depth} id=\"${id}\">${text}</h${depth}>\\n`;\n }\n },\n\n hooks: {\n postprocess(html: string): string {\n return restructureToNestedSections(html, opts);\n }\n }\n };\n}\n\n/**\n * Restructures flat HTML with heading markers into properly nested collapsible sections.\n * This is the key function that creates the hierarchical structure needed for\n * parent collapse to affect children.\n */\nfunction restructureToNestedSections(\n html: string,\n opts: ResolvedOptions\n): string {\n // Parse the HTML to find all content chunks and headings\n const markerRegex = /<!--COLLAPSIBLE_HEADING:(\\d):([^:]+):([^>]+)-->/g;\n\n // Split HTML by markers while capturing the markers\n const parts: Array<{ type: 'content' | 'heading'; content: string; level?: number; id?: string; text?: string }> = [];\n let lastIndex = 0;\n let match;\n\n while ((match = markerRegex.exec(html)) !== null) {\n // Add content before this marker\n if (match.index > lastIndex) {\n const content = html.slice(lastIndex, match.index);\n if (content.trim()) {\n parts.push({ type: 'content', content });\n }\n }\n\n // Add the heading marker info\n parts.push({\n type: 'heading',\n content: '', // Will be filled with the actual h tag\n level: parseInt(match[1], 10),\n id: match[2],\n text: decodeURIComponent(match[3])\n });\n\n lastIndex = match.index + match[0].length;\n }\n\n // Add remaining content after last marker\n if (lastIndex < html.length) {\n const content = html.slice(lastIndex);\n if (content.trim()) {\n parts.push({ type: 'content', content });\n }\n }\n\n // If no collapsible headings, return original HTML\n if (!parts.some(p => p.type === 'heading')) {\n return html;\n }\n\n // Now build the nested structure\n return buildNestedStructure(parts, opts);\n}\n\n/**\n * Determine if a heading level should start expanded based on options\n */\nfunction shouldLevelBeExpanded(level: number, opts: ResolvedOptions): boolean {\n // If autoExpandLevels is specified, use it\n if (opts.autoExpandLevels !== undefined) {\n return opts.autoExpandLevels.includes(level);\n }\n // Otherwise fall back to defaultExpanded\n return opts.defaultExpanded;\n}\n\n/**\n * Builds the nested HTML structure from the parsed parts.\n */\nfunction buildNestedStructure(\n parts: Array<{ type: 'content' | 'heading'; content: string; level?: number; id?: string; text?: string }>,\n opts: ResolvedOptions\n): string {\n const result: string[] = [];\n // Stack tracks open sections: { level, hasContent }\n const sectionStack: Array<{ level: number }> = [];\n\n for (let i = 0; i < parts.length; i++) {\n const part = parts[i];\n\n if (part.type === 'content') {\n // Regular content - just add it\n result.push(part.content);\n } else if (part.type === 'heading' && part.level !== undefined) {\n const level = part.level;\n\n // Close any sections at same or higher level (lower number = higher level)\n while (sectionStack.length > 0 && sectionStack[sectionStack.length - 1].level >= level) {\n // Close the content div and section div\n result.push('</div></div>');\n sectionStack.pop();\n }\n\n // Determine expanded state for this specific level\n const isExpanded = shouldLevelBeExpanded(level, opts);\n const expandedClass = isExpanded ? '' : ' collapsed';\n\n // Extract the actual h tag from the next content piece if it starts with it\n let headingHtml = `<h${level} class=\"${opts.classPrefix}-heading\" id=\"${part.id}\">${part.text}</h${level}>`;\n\n // Look ahead to find and remove the actual h tag from content\n if (i + 1 < parts.length && parts[i + 1].type === 'content') {\n const nextContent = parts[i + 1].content;\n const hTagRegex = new RegExp(`<h${level}[^>]*id=\"${part.id}\"[^>]*>[^<]*</h${level}>\\\\s*`);\n const hTagMatch = nextContent.match(hTagRegex);\n if (hTagMatch) {\n // Use the actual h tag and remove it from content\n headingHtml = hTagMatch[0].replace(`<h${level}`, `<h${level} class=\"${opts.classPrefix}-heading\"`);\n parts[i + 1].content = nextContent.replace(hTagRegex, '');\n }\n }\n\n // Open new section\n result.push(`<div class=\"${opts.classPrefix}-section${expandedClass}\" data-level=\"${level}\">`);\n result.push(`<div class=\"${opts.classPrefix}-heading-wrapper\">`);\n result.push(headingHtml);\n result.push('</div>');\n result.push(`<div class=\"${opts.classPrefix}-content\">`);\n\n sectionStack.push({ level });\n }\n }\n\n // Close any remaining open sections\n while (sectionStack.length > 0) {\n result.push('</div></div>');\n sectionStack.pop();\n }\n\n return result.join('');\n}\n\n/**\n * Helper function to toggle a collapsible section programmatically\n */\nexport function toggleCollapsibleSection(sectionElement: HTMLElement): void {\n const isCollapsed = sectionElement.classList.contains('collapsed');\n const toggle = sectionElement.querySelector('.collapsible-toggle');\n\n sectionElement.classList.toggle('collapsed');\n\n if (toggle) {\n toggle.setAttribute('aria-expanded', String(isCollapsed));\n }\n}\n\n/**\n * Expand all collapsible sections in a container\n */\nexport function expandAllSections(container: HTMLElement): void {\n const sections = container.querySelectorAll('.collapsible-section.collapsed');\n sections.forEach((section) => {\n section.classList.remove('collapsed');\n const toggle = section.querySelector('.collapsible-toggle');\n if (toggle) {\n toggle.setAttribute('aria-expanded', 'true');\n }\n });\n}\n\n/**\n * Collapse all collapsible sections in a container\n */\nexport function collapseAllSections(container: HTMLElement): void {\n const sections = container.querySelectorAll('.collapsible-section:not(.collapsed)');\n sections.forEach((section) => {\n section.classList.add('collapsed');\n const toggle = section.querySelector('.collapsible-toggle');\n if (toggle) {\n toggle.setAttribute('aria-expanded', 'false');\n }\n });\n}\n\n/**\n * Expand sections to reveal a specific heading by ID\n */\nexport function expandToHeading(container: HTMLElement, headingId: string): void {\n const heading = container.querySelector(`#${headingId}`);\n if (!heading) return;\n\n // Find all ancestor collapsible sections and expand them\n let current: HTMLElement | null = heading.closest('.collapsible-section');\n while (current) {\n if (current.classList.contains('collapsed')) {\n current.classList.remove('collapsed');\n const toggle = current.querySelector(':scope > .collapsible-heading-wrapper .collapsible-toggle');\n if (toggle) {\n toggle.setAttribute('aria-expanded', 'true');\n }\n }\n current = current.parentElement?.closest('.collapsible-section') || null;\n }\n}\n"]}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { MarkedExtension } from 'marked';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a marked extension that renders SVG code blocks as actual SVG images.
|
|
4
|
+
*
|
|
5
|
+
* When encountering a code block with language "svg", this extension will
|
|
6
|
+
* render it as an actual SVG element instead of showing the code.
|
|
7
|
+
*
|
|
8
|
+
* This is useful for:
|
|
9
|
+
* - UX mockups and wireframes
|
|
10
|
+
* - Diagrams and illustrations
|
|
11
|
+
* - Icons and simple graphics
|
|
12
|
+
* - Any visual content that can be expressed as SVG
|
|
13
|
+
*
|
|
14
|
+
* Usage in markdown:
|
|
15
|
+
* ```svg
|
|
16
|
+
* <svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
|
|
17
|
+
* <circle cx="50" cy="50" r="40" fill="blue"/>
|
|
18
|
+
* </svg>
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* The generated HTML structure:
|
|
22
|
+
* ```html
|
|
23
|
+
* <div class="svg-rendered">
|
|
24
|
+
* <svg ...>...</svg>
|
|
25
|
+
* </div>
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* Security note: SVG content is rendered as-is. Make sure to only render
|
|
29
|
+
* trusted SVG content or enable sanitization if rendering user-provided content.
|
|
30
|
+
*/
|
|
31
|
+
export declare function createSvgRendererExtension(): MarkedExtension;
|
|
32
|
+
/**
|
|
33
|
+
* Helper function to sanitize SVG content by removing potentially dangerous elements.
|
|
34
|
+
* Call this on the container element after rendering if you need additional security.
|
|
35
|
+
*
|
|
36
|
+
* @param container The DOM element containing rendered SVG
|
|
37
|
+
*/
|
|
38
|
+
export declare function sanitizeSvgContent(container: HTMLElement): void;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a marked extension that renders SVG code blocks as actual SVG images.
|
|
3
|
+
*
|
|
4
|
+
* When encountering a code block with language "svg", this extension will
|
|
5
|
+
* render it as an actual SVG element instead of showing the code.
|
|
6
|
+
*
|
|
7
|
+
* This is useful for:
|
|
8
|
+
* - UX mockups and wireframes
|
|
9
|
+
* - Diagrams and illustrations
|
|
10
|
+
* - Icons and simple graphics
|
|
11
|
+
* - Any visual content that can be expressed as SVG
|
|
12
|
+
*
|
|
13
|
+
* Usage in markdown:
|
|
14
|
+
* ```svg
|
|
15
|
+
* <svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
|
|
16
|
+
* <circle cx="50" cy="50" r="40" fill="blue"/>
|
|
17
|
+
* </svg>
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* The generated HTML structure:
|
|
21
|
+
* ```html
|
|
22
|
+
* <div class="svg-rendered">
|
|
23
|
+
* <svg ...>...</svg>
|
|
24
|
+
* </div>
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* Security note: SVG content is rendered as-is. Make sure to only render
|
|
28
|
+
* trusted SVG content or enable sanitization if rendering user-provided content.
|
|
29
|
+
*/
|
|
30
|
+
export function createSvgRendererExtension() {
|
|
31
|
+
return {
|
|
32
|
+
extensions: [{
|
|
33
|
+
name: 'svgCodeBlock',
|
|
34
|
+
level: 'block',
|
|
35
|
+
start(src) {
|
|
36
|
+
// Look for ```svg at the start of the source
|
|
37
|
+
const match = src.match(/^```svg\b/);
|
|
38
|
+
return match ? match.index : undefined;
|
|
39
|
+
},
|
|
40
|
+
tokenizer(src) {
|
|
41
|
+
// Match ```svg code blocks
|
|
42
|
+
const rule = /^```svg\n([\s\S]*?)```(?:\n|$)/;
|
|
43
|
+
const match = rule.exec(src);
|
|
44
|
+
if (match) {
|
|
45
|
+
const svgContent = match[1].trim();
|
|
46
|
+
// Only process if it looks like valid SVG
|
|
47
|
+
if (isSvgContent(svgContent)) {
|
|
48
|
+
return {
|
|
49
|
+
type: 'svgCodeBlock',
|
|
50
|
+
raw: match[0],
|
|
51
|
+
svgContent: svgContent
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return undefined;
|
|
56
|
+
},
|
|
57
|
+
renderer(token) {
|
|
58
|
+
const svgToken = token;
|
|
59
|
+
return `<div class="svg-rendered">${svgToken.svgContent}</div>\n`;
|
|
60
|
+
}
|
|
61
|
+
}]
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Basic validation to check if content appears to be SVG.
|
|
66
|
+
* This is a simple check - it doesn't fully validate SVG syntax.
|
|
67
|
+
*/
|
|
68
|
+
function isSvgContent(content) {
|
|
69
|
+
// Check if it starts with <svg and contains closing </svg>
|
|
70
|
+
const startsWithSvg = content.toLowerCase().startsWith('<svg');
|
|
71
|
+
const hasSvgClosing = content.toLowerCase().includes('</svg>');
|
|
72
|
+
// Also accept self-closing SVG (rare but valid)
|
|
73
|
+
const isSelfClosing = content.toLowerCase().match(/<svg[^>]*\/>/);
|
|
74
|
+
return (startsWithSvg && hasSvgClosing) || !!isSelfClosing;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Helper function to sanitize SVG content by removing potentially dangerous elements.
|
|
78
|
+
* Call this on the container element after rendering if you need additional security.
|
|
79
|
+
*
|
|
80
|
+
* @param container The DOM element containing rendered SVG
|
|
81
|
+
*/
|
|
82
|
+
export function sanitizeSvgContent(container) {
|
|
83
|
+
// Remove script elements
|
|
84
|
+
const scripts = container.querySelectorAll('script');
|
|
85
|
+
scripts.forEach(script => script.remove());
|
|
86
|
+
// Remove event handlers from all elements
|
|
87
|
+
const allElements = container.querySelectorAll('*');
|
|
88
|
+
allElements.forEach(el => {
|
|
89
|
+
// Remove common event handler attributes
|
|
90
|
+
const dangerousAttrs = [
|
|
91
|
+
'onload', 'onerror', 'onclick', 'onmouseover', 'onmouseout',
|
|
92
|
+
'onfocus', 'onblur', 'onchange', 'onsubmit', 'onreset',
|
|
93
|
+
'onkeydown', 'onkeyup', 'onkeypress'
|
|
94
|
+
];
|
|
95
|
+
dangerousAttrs.forEach(attr => {
|
|
96
|
+
if (el.hasAttribute(attr)) {
|
|
97
|
+
el.removeAttribute(attr);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
// Remove javascript: URLs from href/xlink:href
|
|
101
|
+
if (el.hasAttribute('href')) {
|
|
102
|
+
const href = el.getAttribute('href') || '';
|
|
103
|
+
if (href.toLowerCase().startsWith('javascript:')) {
|
|
104
|
+
el.removeAttribute('href');
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (el.hasAttribute('xlink:href')) {
|
|
108
|
+
const href = el.getAttribute('xlink:href') || '';
|
|
109
|
+
if (href.toLowerCase().startsWith('javascript:')) {
|
|
110
|
+
el.removeAttribute('xlink:href');
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
// Remove foreignObject elements (can contain HTML/scripts)
|
|
115
|
+
const foreignObjects = container.querySelectorAll('foreignObject');
|
|
116
|
+
foreignObjects.forEach(fo => fo.remove());
|
|
117
|
+
// Remove use elements pointing to external resources
|
|
118
|
+
const useElements = container.querySelectorAll('use');
|
|
119
|
+
useElements.forEach(use => {
|
|
120
|
+
const href = use.getAttribute('href') || use.getAttribute('xlink:href') || '';
|
|
121
|
+
if (href.startsWith('http://') || href.startsWith('https://') || href.startsWith('//')) {
|
|
122
|
+
use.remove();
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
//# sourceMappingURL=svg-renderer.extension.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"svg-renderer.extension.js","sourceRoot":"","sources":["../../../src/lib/extensions/svg-renderer.extension.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,UAAU,0BAA0B;IACxC,OAAO;QACL,UAAU,EAAE,CAAC;gBACX,IAAI,EAAE,cAAc;gBACpB,KAAK,EAAE,OAAO;gBACd,KAAK,CAAC,GAAW;oBACf,6CAA6C;oBAC7C,MAAM,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;oBACrC,OAAO,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC;gBACzC,CAAC;gBACD,SAAS,CAAC,GAAW;oBACnB,2BAA2B;oBAC3B,MAAM,IAAI,GAAG,gCAAgC,CAAC;oBAC9C,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;oBAE7B,IAAI,KAAK,EAAE,CAAC;wBACV,MAAM,UAAU,GAAG,KAAK,CA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,GAAG,SAAS,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC;IACtD,WAAW,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;QACxB,MAAM,IAAI,GAAG,GAAG,CAAC,YAAY,CAAC,MAAM,CAAC,IAAI,GAAG,CAAC,YAAY,CAAC,YAAY,CAAC,IAAI,EAAE,CAAC;QAC9E,IAAI,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,IAAI,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,IAAI,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;YACvF,GAAG,CAAC,MAAM,EAAE,CAAC;QACf,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { MarkedExtension } from 'marked';\n\n/**\n * Creates a marked extension that renders SVG code blocks as actual SVG images.\n *\n * When encountering a code block with language \"svg\", this extension will\n * render it as an actual SVG element instead of showing the code.\n *\n * This is useful for:\n * - UX mockups and wireframes\n * - Diagrams and illustrations\n * - Icons and simple graphics\n * - Any visual content that can be expressed as SVG\n *\n * Usage in markdown:\n * ```svg\n * <svg width=\"100\" height=\"100\" xmlns=\"http://www.w3.org/2000/svg\">\n * <circle cx=\"50\" cy=\"50\" r=\"40\" fill=\"blue\"/>\n * </svg>\n * ```\n *\n * The generated HTML structure:\n * ```html\n * <div class=\"svg-rendered\">\n * <svg ...>...</svg>\n * </div>\n * ```\n *\n * Security note: SVG content is rendered as-is. Make sure to only render\n * trusted SVG content or enable sanitization if rendering user-provided content.\n */\nexport function createSvgRendererExtension(): MarkedExtension {\n return {\n extensions: [{\n name: 'svgCodeBlock',\n level: 'block',\n start(src: string) {\n // Look for ```svg at the start of the source\n const match = src.match(/^```svg\\b/);\n return match ? match.index : undefined;\n },\n tokenizer(src: string) {\n // Match ```svg code blocks\n const rule = /^```svg\\n([\\s\\S]*?)```(?:\\n|$)/;\n const match = rule.exec(src);\n\n if (match) {\n const svgContent = match[1].trim();\n\n // Only process if it looks like valid SVG\n if (isSvgContent(svgContent)) {\n return {\n type: 'svgCodeBlock',\n raw: match[0],\n svgContent: svgContent\n };\n }\n }\n return undefined;\n },\n renderer(token) {\n const svgToken = token as unknown as { svgContent: string };\n return `<div class=\"svg-rendered\">${svgToken.svgContent}</div>\\n`;\n }\n }]\n };\n}\n\n/**\n * Basic validation to check if content appears to be SVG.\n * This is a simple check - it doesn't fully validate SVG syntax.\n */\nfunction isSvgContent(content: string): boolean {\n // Check if it starts with <svg and contains closing </svg>\n const startsWithSvg = content.toLowerCase().startsWith('<svg');\n const hasSvgClosing = content.toLowerCase().includes('</svg>');\n\n // Also accept self-closing SVG (rare but valid)\n const isSelfClosing = content.toLowerCase().match(/<svg[^>]*\\/>/);\n\n return (startsWithSvg && hasSvgClosing) || !!isSelfClosing;\n}\n\n/**\n * Helper function to sanitize SVG content by removing potentially dangerous elements.\n * Call this on the container element after rendering if you need additional security.\n *\n * @param container The DOM element containing rendered SVG\n */\nexport function sanitizeSvgContent(container: HTMLElement): void {\n // Remove script elements\n const scripts = container.querySelectorAll('script');\n scripts.forEach(script => script.remove());\n\n // Remove event handlers from all elements\n const allElements = container.querySelectorAll('*');\n allElements.forEach(el => {\n // Remove common event handler attributes\n const dangerousAttrs = [\n 'onload', 'onerror', 'onclick', 'onmouseover', 'onmouseout',\n 'onfocus', 'onblur', 'onchange', 'onsubmit', 'onreset',\n 'onkeydown', 'onkeyup', 'onkeypress'\n ];\n\n dangerousAttrs.forEach(attr => {\n if (el.hasAttribute(attr)) {\n el.removeAttribute(attr);\n }\n });\n\n // Remove javascript: URLs from href/xlink:href\n if (el.hasAttribute('href')) {\n const href = el.getAttribute('href') || '';\n if (href.toLowerCase().startsWith('javascript:')) {\n el.removeAttribute('href');\n }\n }\n if (el.hasAttribute('xlink:href')) {\n const href = el.getAttribute('xlink:href') || '';\n if (href.toLowerCase().startsWith('javascript:')) {\n el.removeAttribute('xlink:href');\n }\n }\n });\n\n // Remove foreignObject elements (can contain HTML/scripts)\n const foreignObjects = container.querySelectorAll('foreignObject');\n foreignObjects.forEach(fo => fo.remove());\n\n // Remove use elements pointing to external resources\n const useElements = container.querySelectorAll('use');\n useElements.forEach(use => {\n const href = use.getAttribute('href') || use.getAttribute('xlink:href') || '';\n if (href.startsWith('http://') || href.startsWith('https://') || href.startsWith('//')) {\n use.remove();\n }\n });\n}\n"]}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import * as i0 from "@angular/core";
|
|
2
|
+
import * as i1 from "./components/markdown.component";
|
|
3
|
+
import * as i2 from "@angular/common";
|
|
4
|
+
/**
|
|
5
|
+
* MemberJunction Markdown Module
|
|
6
|
+
*
|
|
7
|
+
* A lightweight Angular module for rendering markdown content with:
|
|
8
|
+
* - Prism.js syntax highlighting
|
|
9
|
+
* - Mermaid diagram support
|
|
10
|
+
* - Copy-to-clipboard for code blocks
|
|
11
|
+
* - Collapsible heading sections
|
|
12
|
+
* - GitHub-style alerts
|
|
13
|
+
* - Heading anchor IDs
|
|
14
|
+
*
|
|
15
|
+
* Usage:
|
|
16
|
+
* ```typescript
|
|
17
|
+
* import { MarkdownModule } from '@memberjunction/ng-markdown';
|
|
18
|
+
*
|
|
19
|
+
* @NgModule({
|
|
20
|
+
* imports: [MarkdownModule]
|
|
21
|
+
* })
|
|
22
|
+
* export class YourModule { }
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* Then in your template:
|
|
26
|
+
* ```html
|
|
27
|
+
* <mj-markdown [data]="markdownContent"></mj-markdown>
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* Note: This module does NOT use forRoot(). Simply import it in any module
|
|
31
|
+
* where you need markdown rendering. The MarkdownService is provided at root
|
|
32
|
+
* level for efficient sharing across the application.
|
|
33
|
+
*/
|
|
34
|
+
export declare class MarkdownModule {
|
|
35
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<MarkdownModule, never>;
|
|
36
|
+
static ɵmod: i0.ɵɵNgModuleDeclaration<MarkdownModule, [typeof i1.MarkdownComponent], [typeof i2.CommonModule], [typeof i1.MarkdownComponent]>;
|
|
37
|
+
static ɵinj: i0.ɵɵInjectorDeclaration<MarkdownModule>;
|
|
38
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { NgModule } from '@angular/core';
|
|
2
|
+
import { CommonModule } from '@angular/common';
|
|
3
|
+
import { MarkdownComponent } from './components/markdown.component';
|
|
4
|
+
import * as i0 from "@angular/core";
|
|
5
|
+
/**
|
|
6
|
+
* MemberJunction Markdown Module
|
|
7
|
+
*
|
|
8
|
+
* A lightweight Angular module for rendering markdown content with:
|
|
9
|
+
* - Prism.js syntax highlighting
|
|
10
|
+
* - Mermaid diagram support
|
|
11
|
+
* - Copy-to-clipboard for code blocks
|
|
12
|
+
* - Collapsible heading sections
|
|
13
|
+
* - GitHub-style alerts
|
|
14
|
+
* - Heading anchor IDs
|
|
15
|
+
*
|
|
16
|
+
* Usage:
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { MarkdownModule } from '@memberjunction/ng-markdown';
|
|
19
|
+
*
|
|
20
|
+
* @NgModule({
|
|
21
|
+
* imports: [MarkdownModule]
|
|
22
|
+
* })
|
|
23
|
+
* export class YourModule { }
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* Then in your template:
|
|
27
|
+
* ```html
|
|
28
|
+
* <mj-markdown [data]="markdownContent"></mj-markdown>
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* Note: This module does NOT use forRoot(). Simply import it in any module
|
|
32
|
+
* where you need markdown rendering. The MarkdownService is provided at root
|
|
33
|
+
* level for efficient sharing across the application.
|
|
34
|
+
*/
|
|
35
|
+
export class MarkdownModule {
|
|
36
|
+
static { this.ɵfac = function MarkdownModule_Factory(t) { return new (t || MarkdownModule)(); }; }
|
|
37
|
+
static { this.ɵmod = /*@__PURE__*/ i0.ɵɵdefineNgModule({ type: MarkdownModule }); }
|
|
38
|
+
static { this.ɵinj = /*@__PURE__*/ i0.ɵɵdefineInjector({ imports: [CommonModule] }); }
|
|
39
|
+
}
|
|
40
|
+
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(MarkdownModule, [{
|
|
41
|
+
type: NgModule,
|
|
42
|
+
args: [{
|
|
43
|
+
declarations: [
|
|
44
|
+
MarkdownComponent
|
|
45
|
+
],
|
|
46
|
+
imports: [
|
|
47
|
+
CommonModule
|
|
48
|
+
],
|
|
49
|
+
exports: [
|
|
50
|
+
MarkdownComponent
|
|
51
|
+
],
|
|
52
|
+
providers: [
|
|
53
|
+
// MarkdownService is providedIn: 'root', so no need to provide here
|
|
54
|
+
// This ensures a single instance is shared across the app
|
|
55
|
+
]
|
|
56
|
+
}]
|
|
57
|
+
}], null, null); })();
|
|
58
|
+
(function () { (typeof ngJitMode === "undefined" || ngJitMode) && i0.ɵɵsetNgModuleScope(MarkdownModule, { declarations: [MarkdownComponent], imports: [CommonModule], exports: [MarkdownComponent] }); })();
|
|
59
|
+
//# sourceMappingURL=markdown.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"markdown.module.js","sourceRoot":"","sources":["../../src/lib/markdown.module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAC;;AAGpE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAgBH,MAAM,OAAO,cAAc;+EAAd,cAAc;mEAAd,cAAc;uEAVvB,YAAY;;iFAUH,cAAc;cAf1B,QAAQ;eAAC;gBACR,YAAY,EAAE;oBACZ,iBAAiB;iBAClB;gBACD,OAAO,EAAE;oBACP,YAAY;iBACb;gBACD,OAAO,EAAE;oBACP,iBAAiB;iBAClB;gBACD,SAAS,EAAE;gBACT,oEAAoE;gBACpE,0DAA0D;iBAC3D;aACF;;wFACY,cAAc,mBAbvB,iBAAiB,aAGjB,YAAY,aAGZ,iBAAiB","sourcesContent":["import { NgModule } from '@angular/core';\nimport { CommonModule } from '@angular/common';\nimport { MarkdownComponent } from './components/markdown.component';\nimport { MarkdownService } from './services/markdown.service';\n\n/**\n * MemberJunction Markdown Module\n *\n * A lightweight Angular module for rendering markdown content with:\n * - Prism.js syntax highlighting\n * - Mermaid diagram support\n * - Copy-to-clipboard for code blocks\n * - Collapsible heading sections\n * - GitHub-style alerts\n * - Heading anchor IDs\n *\n * Usage:\n * ```typescript\n * import { MarkdownModule } from '@memberjunction/ng-markdown';\n *\n * @NgModule({\n * imports: [MarkdownModule]\n * })\n * export class YourModule { }\n * ```\n *\n * Then in your template:\n * ```html\n * <mj-markdown [data]=\"markdownContent\"></mj-markdown>\n * ```\n *\n * Note: This module does NOT use forRoot(). Simply import it in any module\n * where you need markdown rendering. The MarkdownService is provided at root\n * level for efficient sharing across the application.\n */\n@NgModule({\n declarations: [\n MarkdownComponent\n ],\n imports: [\n CommonModule\n ],\n exports: [\n MarkdownComponent\n ],\n providers: [\n // MarkdownService is providedIn: 'root', so no need to provide here\n // This ensures a single instance is shared across the app\n ]\n})\nexport class MarkdownModule { }\n"]}
|