quikdown 1.1.0 → 1.2.2
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 +46 -6
- package/dist/quikdown.cjs +5 -5
- package/dist/quikdown.dark.css +1 -1
- package/dist/quikdown.esm.js +5 -5
- package/dist/quikdown.esm.min.js +2 -2
- package/dist/quikdown.esm.min.js.map +1 -1
- package/dist/quikdown.light.css +1 -1
- package/dist/quikdown.umd.js +5 -5
- package/dist/quikdown.umd.min.js +2 -2
- package/dist/quikdown.umd.min.js.map +1 -1
- package/dist/quikdown_ast.cjs +513 -0
- package/dist/quikdown_ast.d.ts +227 -0
- package/dist/quikdown_ast.esm.js +511 -0
- package/dist/quikdown_ast.esm.min.js +8 -0
- package/dist/quikdown_ast.esm.min.js.map +1 -0
- package/dist/quikdown_ast.umd.js +519 -0
- package/dist/quikdown_ast.umd.min.js +8 -0
- package/dist/quikdown_ast.umd.min.js.map +1 -0
- package/dist/quikdown_ast_html.cjs +1058 -0
- package/dist/quikdown_ast_html.d.ts +68 -0
- package/dist/quikdown_ast_html.esm.js +1056 -0
- package/dist/quikdown_ast_html.esm.min.js +8 -0
- package/dist/quikdown_ast_html.esm.min.js.map +1 -0
- package/dist/quikdown_ast_html.umd.js +1064 -0
- package/dist/quikdown_ast_html.umd.min.js +8 -0
- package/dist/quikdown_ast_html.umd.min.js.map +1 -0
- package/dist/quikdown_bd.cjs +12 -12
- package/dist/quikdown_bd.esm.js +12 -12
- package/dist/quikdown_bd.esm.min.js +2 -2
- package/dist/quikdown_bd.esm.min.js.map +1 -1
- package/dist/quikdown_bd.umd.js +12 -12
- package/dist/quikdown_bd.umd.min.js +2 -2
- package/dist/quikdown_bd.umd.min.js.map +1 -1
- package/dist/quikdown_edit.cjs +2297 -136
- package/dist/quikdown_edit.d.ts +110 -132
- package/dist/quikdown_edit.esm.js +2297 -136
- package/dist/quikdown_edit.esm.min.js +3 -4
- package/dist/quikdown_edit.esm.min.js.map +1 -1
- package/dist/quikdown_edit.umd.js +2298 -137
- package/dist/quikdown_edit.umd.min.js +3 -4
- package/dist/quikdown_edit.umd.min.js.map +1 -1
- package/dist/quikdown_json.cjs +556 -0
- package/dist/quikdown_json.d.ts +48 -0
- package/dist/quikdown_json.esm.js +554 -0
- package/dist/quikdown_json.esm.min.js +8 -0
- package/dist/quikdown_json.esm.min.js.map +1 -0
- package/dist/quikdown_json.umd.js +562 -0
- package/dist/quikdown_json.umd.min.js +8 -0
- package/dist/quikdown_json.umd.min.js.map +1 -0
- package/dist/quikdown_yaml.cjs +717 -0
- package/dist/quikdown_yaml.d.ts +51 -0
- package/dist/quikdown_yaml.esm.js +715 -0
- package/dist/quikdown_yaml.esm.min.js +8 -0
- package/dist/quikdown_yaml.esm.min.js.map +1 -0
- package/dist/quikdown_yaml.umd.js +723 -0
- package/dist/quikdown_yaml.umd.min.js +8 -0
- package/dist/quikdown_yaml.umd.min.js.map +1 -0
- package/package.json +92 -39
|
@@ -0,0 +1,1056 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* quikdown_ast_html - AST to HTML Markdown Parser
|
|
3
|
+
* @version 1.2.2
|
|
4
|
+
* @license BSD-2-Clause
|
|
5
|
+
* @copyright DeftIO 2025
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* quikdown_ast - Forgiving markdown to AST parser
|
|
9
|
+
* Converts markdown to a structured Abstract Syntax Tree
|
|
10
|
+
* @param {string} markdown - The markdown source text
|
|
11
|
+
* @param {Object} options - Optional configuration object
|
|
12
|
+
* @returns {Object} - The AST object
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
// Version will be injected at build time
|
|
16
|
+
const quikdownVersion$1 = '1.2.2';
|
|
17
|
+
|
|
18
|
+
// Safety limit to prevent infinite loops in list parsing
|
|
19
|
+
const MAX_LOOP_ITERATIONS = 1000;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Parse markdown into an AST
|
|
23
|
+
* @param {string} markdown - The markdown source text
|
|
24
|
+
* @param {Object} options - Optional configuration object
|
|
25
|
+
* @returns {Object} - The AST object
|
|
26
|
+
*/
|
|
27
|
+
function quikdown_ast(markdown, options = {}) {
|
|
28
|
+
if (!markdown || typeof markdown !== 'string') {
|
|
29
|
+
return { type: 'document', children: [] };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Normalize line endings (handle CRLF, CR, LF uniformly)
|
|
33
|
+
const text = markdown.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
|
|
34
|
+
|
|
35
|
+
const children = parseBlocks(text);
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
type: 'document',
|
|
39
|
+
children
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Parse block-level elements
|
|
45
|
+
*/
|
|
46
|
+
function parseBlocks(text, options) {
|
|
47
|
+
const blocks = [];
|
|
48
|
+
const lines = text.split('\n');
|
|
49
|
+
let i = 0;
|
|
50
|
+
|
|
51
|
+
while (i < lines.length) {
|
|
52
|
+
const line = lines[i];
|
|
53
|
+
|
|
54
|
+
// Empty line - skip
|
|
55
|
+
if (line.trim() === '') {
|
|
56
|
+
i++;
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Fenced code block (``` or ~~~)
|
|
61
|
+
const fenceMatch = line.match(/^(```|~~~)(.*)$/);
|
|
62
|
+
if (fenceMatch) {
|
|
63
|
+
const [, openFence, langPart] = fenceMatch;
|
|
64
|
+
const lang = langPart.trim();
|
|
65
|
+
const codeLines = [];
|
|
66
|
+
i++;
|
|
67
|
+
|
|
68
|
+
// Find closing fence (forgiving: accept mismatched fences or EOF)
|
|
69
|
+
while (i < lines.length) {
|
|
70
|
+
const closingMatch = lines[i].match(/^(```|~~~)\s*$/);
|
|
71
|
+
if (closingMatch) {
|
|
72
|
+
i++;
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
codeLines.push(lines[i]);
|
|
76
|
+
i++;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
blocks.push({
|
|
80
|
+
type: 'code_block',
|
|
81
|
+
lang: lang || null,
|
|
82
|
+
content: codeLines.join('\n'),
|
|
83
|
+
fence: openFence
|
|
84
|
+
});
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Horizontal rule
|
|
89
|
+
if (/^---+\s*$/.test(line) || /^\*\*\*+\s*$/.test(line) || /^___+\s*$/.test(line)) {
|
|
90
|
+
blocks.push({ type: 'hr' });
|
|
91
|
+
i++;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Heading (forgiving: accept #heading without space)
|
|
96
|
+
const headingMatch = line.match(/^(#{1,6})\s*(.+?)\s*#*$/);
|
|
97
|
+
if (headingMatch) {
|
|
98
|
+
const [, hashes, content] = headingMatch;
|
|
99
|
+
blocks.push({
|
|
100
|
+
type: 'heading',
|
|
101
|
+
level: hashes.length,
|
|
102
|
+
children: parseInline(content)
|
|
103
|
+
});
|
|
104
|
+
i++;
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Table (look for separator line)
|
|
109
|
+
if (line.includes('|')) {
|
|
110
|
+
const tableResult = tryParseTable(lines, i);
|
|
111
|
+
if (tableResult) {
|
|
112
|
+
blocks.push(tableResult.node);
|
|
113
|
+
i = tableResult.nextIndex;
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Blockquote
|
|
119
|
+
if (line.match(/^>\s*/)) {
|
|
120
|
+
const quoteLines = [];
|
|
121
|
+
while (i < lines.length && lines[i].match(/^>\s*/)) {
|
|
122
|
+
quoteLines.push(lines[i].replace(/^>\s*/, ''));
|
|
123
|
+
i++;
|
|
124
|
+
}
|
|
125
|
+
blocks.push({
|
|
126
|
+
type: 'blockquote',
|
|
127
|
+
children: parseBlocks(quoteLines.join('\n'))
|
|
128
|
+
});
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// List (ordered or unordered)
|
|
133
|
+
const listMatch = line.match(/^(\s*)([*\-+]|\d+\.)\s+(.*)$/);
|
|
134
|
+
if (listMatch) {
|
|
135
|
+
const listResult = parseList(lines, i);
|
|
136
|
+
blocks.push(listResult.node);
|
|
137
|
+
i = listResult.nextIndex;
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Paragraph - collect lines until empty line or block element
|
|
142
|
+
const paragraphLines = [];
|
|
143
|
+
while (i < lines.length) {
|
|
144
|
+
const pLine = lines[i];
|
|
145
|
+
|
|
146
|
+
// Stop on empty line
|
|
147
|
+
if (pLine.trim() === '') break;
|
|
148
|
+
|
|
149
|
+
// Stop on block elements
|
|
150
|
+
if (/^(```|~~~)/.test(pLine)) break;
|
|
151
|
+
if (/^#{1,6}\s/.test(pLine)) break;
|
|
152
|
+
if (/^---+\s*$/.test(pLine) || /^\*\*\*+\s*$/.test(pLine) || /^___+\s*$/.test(pLine)) break;
|
|
153
|
+
if (/^>\s*/.test(pLine)) break;
|
|
154
|
+
if (/^(\s*)([*\-+]|\d+\.)\s+/.test(pLine)) break;
|
|
155
|
+
if (pLine.includes('|') && i + 1 < lines.length && /^\|?[\s\-:|]+\|?$/.test(lines[i + 1])) break;
|
|
156
|
+
|
|
157
|
+
paragraphLines.push(pLine);
|
|
158
|
+
i++;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (paragraphLines.length > 0) {
|
|
162
|
+
blocks.push({
|
|
163
|
+
type: 'paragraph',
|
|
164
|
+
children: parseInline(paragraphLines.join('\n'))
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return blocks;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Try to parse a table starting at the given line
|
|
174
|
+
*/
|
|
175
|
+
function tryParseTable(lines, startIndex, options) {
|
|
176
|
+
// Need at least 2 lines (header + separator)
|
|
177
|
+
if (startIndex + 1 >= lines.length) return null;
|
|
178
|
+
|
|
179
|
+
const headerLine = lines[startIndex];
|
|
180
|
+
const separatorLine = lines[startIndex + 1];
|
|
181
|
+
|
|
182
|
+
// Check if separator line is valid
|
|
183
|
+
if (!/^\|?[\s\-:|]+\|?$/.test(separatorLine) || !separatorLine.includes('-')) {
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Parse header
|
|
188
|
+
const headerCells = parseTableRow(headerLine);
|
|
189
|
+
if (headerCells.length === 0) return null;
|
|
190
|
+
|
|
191
|
+
// Parse alignments from separator
|
|
192
|
+
const separatorCells = parseTableRow(separatorLine);
|
|
193
|
+
const alignments = separatorCells.map(cell => {
|
|
194
|
+
const trimmed = cell.trim();
|
|
195
|
+
if (trimmed.startsWith(':') && trimmed.endsWith(':')) return 'center';
|
|
196
|
+
if (trimmed.endsWith(':')) return 'right';
|
|
197
|
+
return 'left';
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
// Parse headers with inline formatting
|
|
201
|
+
const headers = headerCells.map(cell => parseInline(cell.trim()));
|
|
202
|
+
|
|
203
|
+
// Parse body rows
|
|
204
|
+
const rows = [];
|
|
205
|
+
let i = startIndex + 2;
|
|
206
|
+
while (i < lines.length) {
|
|
207
|
+
const rowLine = lines[i];
|
|
208
|
+
if (!rowLine.includes('|') || rowLine.trim() === '') break;
|
|
209
|
+
|
|
210
|
+
const cells = parseTableRow(rowLine);
|
|
211
|
+
rows.push(cells.map(cell => parseInline(cell.trim())));
|
|
212
|
+
i++;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return {
|
|
216
|
+
node: {
|
|
217
|
+
type: 'table',
|
|
218
|
+
headers,
|
|
219
|
+
rows,
|
|
220
|
+
alignments
|
|
221
|
+
},
|
|
222
|
+
nextIndex: i
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Parse a table row into cells
|
|
228
|
+
*/
|
|
229
|
+
function parseTableRow(line) {
|
|
230
|
+
// Handle pipes at start/end or not
|
|
231
|
+
let trimmed = line.trim();
|
|
232
|
+
if (trimmed.startsWith('|')) trimmed = trimmed.slice(1);
|
|
233
|
+
if (trimmed.endsWith('|')) trimmed = trimmed.slice(0, -1);
|
|
234
|
+
return trimmed.split('|');
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Parse a list starting at the given line
|
|
239
|
+
*/
|
|
240
|
+
function parseList(lines, startIndex, options) {
|
|
241
|
+
const items = [];
|
|
242
|
+
let i = startIndex;
|
|
243
|
+
let loopCount = 0;
|
|
244
|
+
|
|
245
|
+
// Determine initial list type
|
|
246
|
+
const firstMatch = lines[i].match(/^(\s*)([*\-+]|\d+\.)\s+(.*)$/);
|
|
247
|
+
const isOrdered = /^\d+\./.test(firstMatch[2]);
|
|
248
|
+
const baseIndent = firstMatch[1].length;
|
|
249
|
+
|
|
250
|
+
while (i < lines.length && loopCount < MAX_LOOP_ITERATIONS) {
|
|
251
|
+
loopCount++;
|
|
252
|
+
const line = lines[i];
|
|
253
|
+
const match = line.match(/^(\s*)([*\-+]|\d+\.)\s+(.*)$/);
|
|
254
|
+
|
|
255
|
+
if (!match) break;
|
|
256
|
+
|
|
257
|
+
const [, indent, marker, content] = match;
|
|
258
|
+
const indentLevel = indent.length;
|
|
259
|
+
|
|
260
|
+
// If less indented than base, stop
|
|
261
|
+
if (indentLevel < baseIndent) break;
|
|
262
|
+
|
|
263
|
+
// If same indentation but different list type, stop
|
|
264
|
+
const itemIsOrdered = /^\d+\./.test(marker);
|
|
265
|
+
if (indentLevel === baseIndent && itemIsOrdered !== isOrdered) break;
|
|
266
|
+
|
|
267
|
+
// If more indented, it's a nested list - handle by collecting sub-lines
|
|
268
|
+
if (indentLevel > baseIndent) {
|
|
269
|
+
// This is a nested list item, collect and parse as sublist
|
|
270
|
+
const subLines = [];
|
|
271
|
+
let subLoopCount = 0;
|
|
272
|
+
while (i < lines.length && subLoopCount < MAX_LOOP_ITERATIONS) {
|
|
273
|
+
subLoopCount++;
|
|
274
|
+
const subLine = lines[i];
|
|
275
|
+
const subMatch = subLine.match(/^(\s*)([*\-+]|\d+\.)\s+/);
|
|
276
|
+
if (!subMatch) break;
|
|
277
|
+
if (subMatch[1].length < baseIndent) break;
|
|
278
|
+
if (subMatch[1].length === baseIndent) break;
|
|
279
|
+
subLines.push(subLine);
|
|
280
|
+
i++;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
if (subLines.length > 0 && items.length > 0) {
|
|
284
|
+
// Add nested list to last item
|
|
285
|
+
const nestedResult = parseList(subLines, 0);
|
|
286
|
+
const lastItem = items[items.length - 1];
|
|
287
|
+
if (!lastItem.children) {
|
|
288
|
+
lastItem.children = [];
|
|
289
|
+
} else if (!Array.isArray(lastItem.children)) {
|
|
290
|
+
lastItem.children = [{ type: 'paragraph', children: lastItem.children }];
|
|
291
|
+
}
|
|
292
|
+
lastItem.children.push(nestedResult.node);
|
|
293
|
+
}
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// Parse list item
|
|
298
|
+
const itemNode = {
|
|
299
|
+
type: 'list_item',
|
|
300
|
+
checked: null,
|
|
301
|
+
children: null
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
// Check for task list syntax
|
|
305
|
+
const taskMatch = content.match(/^\[([x ])\]\s*(.*)$/i);
|
|
306
|
+
if (taskMatch && !isOrdered) {
|
|
307
|
+
itemNode.checked = taskMatch[1].toLowerCase() === 'x';
|
|
308
|
+
itemNode.children = parseInline(taskMatch[2]);
|
|
309
|
+
} else {
|
|
310
|
+
itemNode.children = parseInline(content);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
items.push(itemNode);
|
|
314
|
+
i++;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
return {
|
|
318
|
+
node: {
|
|
319
|
+
type: 'list',
|
|
320
|
+
ordered: isOrdered,
|
|
321
|
+
items
|
|
322
|
+
},
|
|
323
|
+
nextIndex: i
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Parse inline elements
|
|
329
|
+
*/
|
|
330
|
+
function parseInline(text, options) {
|
|
331
|
+
if (!text) return [];
|
|
332
|
+
|
|
333
|
+
const nodes = [];
|
|
334
|
+
let remaining = text;
|
|
335
|
+
|
|
336
|
+
while (remaining.length > 0) {
|
|
337
|
+
let matched = false;
|
|
338
|
+
|
|
339
|
+
// Line break (1+ trailing spaces or explicit \n after processing)
|
|
340
|
+
// Handle inline line breaks (two spaces at end of line or backslash before newline)
|
|
341
|
+
const brMatch = remaining.match(/^(.+?)(?: {2}|\\\n|\n)/);
|
|
342
|
+
if (brMatch && remaining.includes('\n')) {
|
|
343
|
+
const beforeBr = remaining.indexOf('\n');
|
|
344
|
+
const beforeText = remaining.slice(0, beforeBr);
|
|
345
|
+
const afterText = remaining.slice(beforeBr + 1);
|
|
346
|
+
|
|
347
|
+
// Check if line break is significant (2+ trailing spaces or backslash)
|
|
348
|
+
if (beforeText.endsWith(' ') || beforeText.endsWith('\\')) {
|
|
349
|
+
const cleanText = beforeText.replace(/\\$/, '').replace(/ +$/, '');
|
|
350
|
+
if (cleanText) {
|
|
351
|
+
nodes.push(...parseInlineContent(cleanText));
|
|
352
|
+
}
|
|
353
|
+
nodes.push({ type: 'br' });
|
|
354
|
+
remaining = afterText;
|
|
355
|
+
matched = true;
|
|
356
|
+
continue;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// Images: 
|
|
361
|
+
const imgMatch = remaining.match(/^!\[([^\]]*)\]\(\s*([^)\s]+)\s*\)/);
|
|
362
|
+
if (imgMatch) {
|
|
363
|
+
nodes.push({
|
|
364
|
+
type: 'image',
|
|
365
|
+
alt: imgMatch[1],
|
|
366
|
+
url: imgMatch[2].trim() // Forgiving: trim whitespace in URL
|
|
367
|
+
});
|
|
368
|
+
remaining = remaining.slice(imgMatch[0].length);
|
|
369
|
+
matched = true;
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// Links: [text](url)
|
|
374
|
+
const linkMatch = remaining.match(/^\[([^\]]+)\]\(\s*([^)\s]+)\s*\)/);
|
|
375
|
+
if (linkMatch) {
|
|
376
|
+
nodes.push({
|
|
377
|
+
type: 'link',
|
|
378
|
+
url: linkMatch[2].trim(), // Forgiving: trim whitespace in URL
|
|
379
|
+
children: parseInlineContent(linkMatch[1])
|
|
380
|
+
});
|
|
381
|
+
remaining = remaining.slice(linkMatch[0].length);
|
|
382
|
+
matched = true;
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// Inline code: `code`
|
|
387
|
+
const codeMatch = remaining.match(/^`([^`]+)`/);
|
|
388
|
+
if (codeMatch) {
|
|
389
|
+
nodes.push({
|
|
390
|
+
type: 'code',
|
|
391
|
+
value: codeMatch[1]
|
|
392
|
+
});
|
|
393
|
+
remaining = remaining.slice(codeMatch[0].length);
|
|
394
|
+
matched = true;
|
|
395
|
+
continue;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
// Bold: **text** or __text__
|
|
399
|
+
const boldMatch = remaining.match(/^(\*\*|__)(.+?)\1/);
|
|
400
|
+
if (boldMatch) {
|
|
401
|
+
nodes.push({
|
|
402
|
+
type: 'strong',
|
|
403
|
+
children: parseInlineContent(boldMatch[2])
|
|
404
|
+
});
|
|
405
|
+
remaining = remaining.slice(boldMatch[0].length);
|
|
406
|
+
matched = true;
|
|
407
|
+
continue;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// Strikethrough: ~~text~~
|
|
411
|
+
const strikeMatch = remaining.match(/^~~(.+?)~~/);
|
|
412
|
+
if (strikeMatch) {
|
|
413
|
+
nodes.push({
|
|
414
|
+
type: 'del',
|
|
415
|
+
children: parseInlineContent(strikeMatch[1])
|
|
416
|
+
});
|
|
417
|
+
remaining = remaining.slice(strikeMatch[0].length);
|
|
418
|
+
matched = true;
|
|
419
|
+
continue;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// Italic: *text* or _text_ (not at word boundary for underscores)
|
|
423
|
+
const emMatch = remaining.match(/^(\*|_)(?!\1)(.+?)(?<!\1)\1(?!\1)/);
|
|
424
|
+
if (emMatch) {
|
|
425
|
+
nodes.push({
|
|
426
|
+
type: 'em',
|
|
427
|
+
children: parseInlineContent(emMatch[2])
|
|
428
|
+
});
|
|
429
|
+
remaining = remaining.slice(emMatch[0].length);
|
|
430
|
+
matched = true;
|
|
431
|
+
continue;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// Autolinks: URLs starting with http:// or https://
|
|
435
|
+
const urlMatch = remaining.match(/^(https?:\/\/[^\s<>[\]]+)/);
|
|
436
|
+
if (urlMatch) {
|
|
437
|
+
nodes.push({
|
|
438
|
+
type: 'link',
|
|
439
|
+
url: urlMatch[1],
|
|
440
|
+
children: [{ type: 'text', value: urlMatch[1] }]
|
|
441
|
+
});
|
|
442
|
+
remaining = remaining.slice(urlMatch[0].length);
|
|
443
|
+
matched = true;
|
|
444
|
+
continue;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// Plain text - consume until next potential inline element or end
|
|
448
|
+
if (!matched) {
|
|
449
|
+
// Find next potential inline marker
|
|
450
|
+
const nextMarker = remaining.search(/[`*_~![\\n]|https?:\/\//);
|
|
451
|
+
if (nextMarker === -1) {
|
|
452
|
+
// No more markers, consume rest as text
|
|
453
|
+
nodes.push({ type: 'text', value: remaining });
|
|
454
|
+
break;
|
|
455
|
+
} else if (nextMarker === 0) {
|
|
456
|
+
// Current char is a marker but didn't match - consume it as text
|
|
457
|
+
nodes.push({ type: 'text', value: remaining[0] });
|
|
458
|
+
remaining = remaining.slice(1);
|
|
459
|
+
} else {
|
|
460
|
+
// Consume text up to next marker
|
|
461
|
+
nodes.push({ type: 'text', value: remaining.slice(0, nextMarker) });
|
|
462
|
+
remaining = remaining.slice(nextMarker);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
// Merge adjacent text nodes
|
|
468
|
+
return mergeTextNodes(nodes);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* Parse inline content (recursive helper for nested inline elements)
|
|
473
|
+
*/
|
|
474
|
+
function parseInlineContent(text, options) {
|
|
475
|
+
// For simple nested content, use parseInline
|
|
476
|
+
// But handle newlines as spaces for inline content
|
|
477
|
+
const normalized = text.replace(/\n/g, ' ');
|
|
478
|
+
return parseInline(normalized);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
/**
|
|
482
|
+
* Merge adjacent text nodes
|
|
483
|
+
*/
|
|
484
|
+
function mergeTextNodes(nodes) {
|
|
485
|
+
const merged = [];
|
|
486
|
+
for (const node of nodes) {
|
|
487
|
+
if (node.type === 'text' && merged.length > 0 && merged[merged.length - 1].type === 'text') {
|
|
488
|
+
merged[merged.length - 1].value += node.value;
|
|
489
|
+
} else {
|
|
490
|
+
merged.push(node);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
return merged;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// Attach version
|
|
497
|
+
quikdown_ast.version = quikdownVersion$1;
|
|
498
|
+
|
|
499
|
+
// Export for both CommonJS and ES6
|
|
500
|
+
/* istanbul ignore next */
|
|
501
|
+
if (typeof module !== 'undefined' && module.exports) {
|
|
502
|
+
module.exports = quikdown_ast;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
// For browser global
|
|
506
|
+
/* istanbul ignore next */
|
|
507
|
+
if (typeof window !== 'undefined') {
|
|
508
|
+
window.quikdown_ast = quikdown_ast;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* quikdown_ast_html - AST to HTML converter
|
|
513
|
+
* Converts AST (or markdown/JSON/YAML) to HTML
|
|
514
|
+
* @param {string|Object} input - Markdown string, AST object, JSON string, or YAML string
|
|
515
|
+
* @param {Object} options - Optional configuration object
|
|
516
|
+
* @returns {string} - HTML string
|
|
517
|
+
*/
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
// Version will be injected at build time
|
|
521
|
+
const quikdownVersion = '1.2.2';
|
|
522
|
+
|
|
523
|
+
// Constants
|
|
524
|
+
const CLASS_PREFIX = 'quikdown-';
|
|
525
|
+
|
|
526
|
+
// Escape map for HTML
|
|
527
|
+
const ESC_MAP = {'&':'&','<':'<','>':'>','"':'"',"'":'''};
|
|
528
|
+
|
|
529
|
+
// Style definitions (matching quikdown.js)
|
|
530
|
+
const QUIKDOWN_STYLES = {
|
|
531
|
+
h1: 'font-size:2em;font-weight:600;margin:.67em 0;text-align:left',
|
|
532
|
+
h2: 'font-size:1.5em;font-weight:600;margin:.83em 0',
|
|
533
|
+
h3: 'font-size:1.25em;font-weight:600;margin:1em 0',
|
|
534
|
+
h4: 'font-size:1em;font-weight:600;margin:1.33em 0',
|
|
535
|
+
h5: 'font-size:.875em;font-weight:600;margin:1.67em 0',
|
|
536
|
+
h6: 'font-size:.85em;font-weight:600;margin:2em 0',
|
|
537
|
+
pre: 'background:#f4f4f4;padding:10px;border-radius:4px;overflow-x:auto;margin:1em 0',
|
|
538
|
+
code: 'background:#f0f0f0;padding:2px 4px;border-radius:3px;font-family:monospace',
|
|
539
|
+
blockquote: 'border-left:4px solid #ddd;margin-left:0;padding-left:1em',
|
|
540
|
+
table: 'border-collapse:collapse;width:100%;margin:1em 0',
|
|
541
|
+
th: 'border:1px solid #ddd;padding:8px;background-color:#f2f2f2;font-weight:bold;text-align:left',
|
|
542
|
+
td: 'border:1px solid #ddd;padding:8px;text-align:left',
|
|
543
|
+
hr: 'border:none;border-top:1px solid #ddd;margin:1em 0',
|
|
544
|
+
img: 'max-width:100%;height:auto',
|
|
545
|
+
a: 'color:#06c;text-decoration:underline',
|
|
546
|
+
strong: 'font-weight:bold',
|
|
547
|
+
em: 'font-style:italic',
|
|
548
|
+
del: 'text-decoration:line-through',
|
|
549
|
+
ul: 'margin:.5em 0;padding-left:2em',
|
|
550
|
+
ol: 'margin:.5em 0;padding-left:2em',
|
|
551
|
+
li: 'margin:.25em 0',
|
|
552
|
+
'task-item': 'list-style:none',
|
|
553
|
+
'task-checkbox': 'margin-right:.5em'
|
|
554
|
+
};
|
|
555
|
+
|
|
556
|
+
/**
|
|
557
|
+
* Escape HTML entities
|
|
558
|
+
*/
|
|
559
|
+
function escapeHtml(text) {
|
|
560
|
+
if (!text) return '';
|
|
561
|
+
return String(text).replace(/[&<>"']/g, m => ESC_MAP[m]);
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/**
|
|
565
|
+
* Create attribute string generator
|
|
566
|
+
*/
|
|
567
|
+
function createGetAttr(inline_styles) {
|
|
568
|
+
return function(tag, additionalStyle = '') {
|
|
569
|
+
if (inline_styles) {
|
|
570
|
+
let style = QUIKDOWN_STYLES[tag];
|
|
571
|
+
if (!style && !additionalStyle) return '';
|
|
572
|
+
|
|
573
|
+
if (additionalStyle && additionalStyle.includes('text-align') && style && style.includes('text-align')) {
|
|
574
|
+
style = style.replace(/text-align:[^;]+;?/, '').trim();
|
|
575
|
+
if (style && !style.endsWith(';')) style += ';';
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
const fullStyle = additionalStyle ? (style ? `${style}${additionalStyle}` : additionalStyle) : style;
|
|
579
|
+
return ` style="${fullStyle}"`;
|
|
580
|
+
} else {
|
|
581
|
+
const classAttr = ` class="${CLASS_PREFIX}${tag}"`;
|
|
582
|
+
if (additionalStyle) {
|
|
583
|
+
return `${classAttr} style="${additionalStyle}"`;
|
|
584
|
+
}
|
|
585
|
+
return classAttr;
|
|
586
|
+
}
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* Sanitize URLs
|
|
592
|
+
*/
|
|
593
|
+
function sanitizeUrl(url) {
|
|
594
|
+
if (!url) return '';
|
|
595
|
+
const trimmedUrl = url.trim();
|
|
596
|
+
const lowerUrl = trimmedUrl.toLowerCase();
|
|
597
|
+
|
|
598
|
+
const dangerousProtocols = ['javascript:', 'vbscript:', 'data:'];
|
|
599
|
+
for (const protocol of dangerousProtocols) {
|
|
600
|
+
if (lowerUrl.startsWith(protocol)) {
|
|
601
|
+
if (protocol === 'data:' && lowerUrl.startsWith('data:image/')) {
|
|
602
|
+
return trimmedUrl;
|
|
603
|
+
}
|
|
604
|
+
return '#';
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
return trimmedUrl;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
/**
|
|
612
|
+
* Convert input to AST
|
|
613
|
+
* Accepts markdown string, AST object, JSON string, or YAML string
|
|
614
|
+
*/
|
|
615
|
+
function toAst(input, options = {}) {
|
|
616
|
+
if (!input) {
|
|
617
|
+
return { type: 'document', children: [] };
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
// Already an AST object
|
|
621
|
+
if (typeof input === 'object' && input.type) {
|
|
622
|
+
return input;
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
if (typeof input === 'string') {
|
|
626
|
+
const trimmed = input.trim();
|
|
627
|
+
|
|
628
|
+
// Try JSON first (starts with { or [)
|
|
629
|
+
if (trimmed.startsWith('{') || trimmed.startsWith('[')) {
|
|
630
|
+
try {
|
|
631
|
+
const parsed = JSON.parse(trimmed);
|
|
632
|
+
if (parsed.type === 'document') {
|
|
633
|
+
return parsed;
|
|
634
|
+
}
|
|
635
|
+
// If it's an array, wrap it as document children
|
|
636
|
+
if (Array.isArray(parsed)) {
|
|
637
|
+
return { type: 'document', children: parsed };
|
|
638
|
+
}
|
|
639
|
+
return parsed;
|
|
640
|
+
} catch (_e) {
|
|
641
|
+
// Not valid JSON, fall through to markdown
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
// Try YAML detection (has type: and children: patterns typical of AST)
|
|
646
|
+
if (trimmed.includes('type:') && (trimmed.includes('children:') || trimmed.includes('value:'))) {
|
|
647
|
+
try {
|
|
648
|
+
const parsed = parseYaml(trimmed);
|
|
649
|
+
if (parsed && parsed.type) {
|
|
650
|
+
return parsed;
|
|
651
|
+
}
|
|
652
|
+
} catch (_e) {
|
|
653
|
+
// Not valid YAML AST, fall through to markdown
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
// Treat as markdown
|
|
658
|
+
return quikdown_ast(input, options);
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
return { type: 'document', children: [] };
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
/**
|
|
665
|
+
* Simple YAML parser for AST format
|
|
666
|
+
* Only handles the subset needed for quikdown AST
|
|
667
|
+
*/
|
|
668
|
+
function parseYaml(yaml) {
|
|
669
|
+
const lines = yaml.split('\n');
|
|
670
|
+
return parseYamlNode(lines, 0, 0).value;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
/**
|
|
674
|
+
* Parse a YAML node starting at given line and indent
|
|
675
|
+
*/
|
|
676
|
+
function parseYamlNode(lines, startLine, minIndent) {
|
|
677
|
+
if (startLine >= lines.length) {
|
|
678
|
+
return { value: null, nextLine: startLine };
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
const line = lines[startLine];
|
|
682
|
+
const trimmed = line.trim();
|
|
683
|
+
|
|
684
|
+
// Skip empty lines
|
|
685
|
+
if (trimmed === '') {
|
|
686
|
+
return parseYamlNode(lines, startLine + 1, minIndent);
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
// Get current indent
|
|
690
|
+
const indent = line.search(/\S/);
|
|
691
|
+
if (indent < minIndent && indent >= 0) {
|
|
692
|
+
return { value: null, nextLine: startLine };
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
// Array item
|
|
696
|
+
if (trimmed.startsWith('- ')) {
|
|
697
|
+
return parseYamlArray(lines, startLine, indent);
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
// Empty array
|
|
701
|
+
if (trimmed === '[]') {
|
|
702
|
+
return { value: [], nextLine: startLine + 1 };
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
// Empty object
|
|
706
|
+
if (trimmed === '{}') {
|
|
707
|
+
return { value: {}, nextLine: startLine + 1 };
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
// Key-value pair
|
|
711
|
+
const colonIndex = trimmed.indexOf(':');
|
|
712
|
+
if (colonIndex > 0) {
|
|
713
|
+
return parseYamlObject(lines, startLine, indent);
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
// Scalar value
|
|
717
|
+
return { value: parseYamlScalar(trimmed), nextLine: startLine + 1 };
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
/**
|
|
721
|
+
* Parse YAML array
|
|
722
|
+
*/
|
|
723
|
+
function parseYamlArray(lines, startLine, baseIndent) {
|
|
724
|
+
const items = [];
|
|
725
|
+
let i = startLine;
|
|
726
|
+
|
|
727
|
+
while (i < lines.length) {
|
|
728
|
+
const line = lines[i];
|
|
729
|
+
const trimmed = line.trim();
|
|
730
|
+
|
|
731
|
+
if (trimmed === '') {
|
|
732
|
+
i++;
|
|
733
|
+
continue;
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
const indent = line.search(/\S/);
|
|
737
|
+
if (indent < baseIndent && indent >= 0) break;
|
|
738
|
+
if (indent > baseIndent && items.length > 0) {
|
|
739
|
+
// Continuation of previous item
|
|
740
|
+
i++;
|
|
741
|
+
continue;
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
if (!trimmed.startsWith('- ')) break;
|
|
745
|
+
|
|
746
|
+
// Parse the item after "- "
|
|
747
|
+
const itemContent = trimmed.slice(2);
|
|
748
|
+
|
|
749
|
+
if (itemContent.includes(':')) {
|
|
750
|
+
// Object item - parse inline and following properties
|
|
751
|
+
const obj = {};
|
|
752
|
+
const colonIdx = itemContent.indexOf(':');
|
|
753
|
+
const key = itemContent.slice(0, colonIdx).trim();
|
|
754
|
+
const value = itemContent.slice(colonIdx + 1).trim();
|
|
755
|
+
|
|
756
|
+
if (value === '' || value.startsWith('\n')) {
|
|
757
|
+
// Value on next lines
|
|
758
|
+
const result = parseYamlNode(lines, i + 1, indent + 2);
|
|
759
|
+
obj[key] = result.value;
|
|
760
|
+
i = result.nextLine;
|
|
761
|
+
} else {
|
|
762
|
+
obj[key] = parseYamlScalar(value);
|
|
763
|
+
i++;
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
// Parse remaining properties at same indent
|
|
767
|
+
while (i < lines.length) {
|
|
768
|
+
const nextLine = lines[i];
|
|
769
|
+
const nextTrimmed = nextLine.trim();
|
|
770
|
+
if (nextTrimmed === '') {
|
|
771
|
+
i++;
|
|
772
|
+
continue;
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
const nextIndent = nextLine.search(/\S/);
|
|
776
|
+
if (nextIndent <= baseIndent) break;
|
|
777
|
+
if (nextTrimmed.startsWith('- ')) break;
|
|
778
|
+
|
|
779
|
+
const nextColonIdx = nextTrimmed.indexOf(':');
|
|
780
|
+
if (nextColonIdx > 0) {
|
|
781
|
+
const nextKey = nextTrimmed.slice(0, nextColonIdx).trim();
|
|
782
|
+
const nextValue = nextTrimmed.slice(nextColonIdx + 1).trim();
|
|
783
|
+
|
|
784
|
+
if (nextValue === '' || nextValue.startsWith('\n')) {
|
|
785
|
+
const result = parseYamlNode(lines, i + 1, nextIndent + 2);
|
|
786
|
+
obj[nextKey] = result.value;
|
|
787
|
+
i = result.nextLine;
|
|
788
|
+
} else {
|
|
789
|
+
obj[nextKey] = parseYamlScalar(nextValue);
|
|
790
|
+
i++;
|
|
791
|
+
}
|
|
792
|
+
} else {
|
|
793
|
+
i++;
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
items.push(obj);
|
|
798
|
+
} else {
|
|
799
|
+
items.push(parseYamlScalar(itemContent));
|
|
800
|
+
i++;
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
return { value: items, nextLine: i };
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
/**
|
|
808
|
+
* Parse YAML object
|
|
809
|
+
*/
|
|
810
|
+
function parseYamlObject(lines, startLine, baseIndent) {
|
|
811
|
+
const obj = {};
|
|
812
|
+
let i = startLine;
|
|
813
|
+
|
|
814
|
+
while (i < lines.length) {
|
|
815
|
+
const line = lines[i];
|
|
816
|
+
const trimmed = line.trim();
|
|
817
|
+
|
|
818
|
+
if (trimmed === '') {
|
|
819
|
+
i++;
|
|
820
|
+
continue;
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
const indent = line.search(/\S/);
|
|
824
|
+
if (indent < baseIndent && indent >= 0) break;
|
|
825
|
+
|
|
826
|
+
const colonIdx = trimmed.indexOf(':');
|
|
827
|
+
if (colonIdx <= 0) {
|
|
828
|
+
i++;
|
|
829
|
+
continue;
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
const key = trimmed.slice(0, colonIdx).trim();
|
|
833
|
+
const value = trimmed.slice(colonIdx + 1).trim();
|
|
834
|
+
|
|
835
|
+
if (value === '' || value === '|' || value === '>') {
|
|
836
|
+
// Value on next lines
|
|
837
|
+
const result = parseYamlNode(lines, i + 1, indent + 2);
|
|
838
|
+
obj[key] = result.value;
|
|
839
|
+
i = result.nextLine;
|
|
840
|
+
} else {
|
|
841
|
+
obj[key] = parseYamlScalar(value);
|
|
842
|
+
i++;
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
return { value: obj, nextLine: i };
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
/**
|
|
850
|
+
* Parse YAML scalar value
|
|
851
|
+
*/
|
|
852
|
+
function parseYamlScalar(str) {
|
|
853
|
+
if (!str) return null;
|
|
854
|
+
|
|
855
|
+
const trimmed = str.trim();
|
|
856
|
+
|
|
857
|
+
if (trimmed === 'null' || trimmed === '~') return null;
|
|
858
|
+
if (trimmed === 'true') return true;
|
|
859
|
+
if (trimmed === 'false') return false;
|
|
860
|
+
|
|
861
|
+
// Quoted string
|
|
862
|
+
if ((trimmed.startsWith('"') && trimmed.endsWith('"')) ||
|
|
863
|
+
(trimmed.startsWith("'") && trimmed.endsWith("'"))) {
|
|
864
|
+
return trimmed.slice(1, -1)
|
|
865
|
+
.replace(/\\n/g, '\n')
|
|
866
|
+
.replace(/\\"/g, '"')
|
|
867
|
+
.replace(/\\\\/g, '\\');
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
// Number
|
|
871
|
+
if (/^-?\d+$/.test(trimmed)) return parseInt(trimmed, 10);
|
|
872
|
+
if (/^-?\d+\.\d+$/.test(trimmed)) return parseFloat(trimmed);
|
|
873
|
+
|
|
874
|
+
return trimmed;
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
/**
|
|
878
|
+
* Convert AST (or any valid input) to HTML
|
|
879
|
+
* @param {string|Object} input - Markdown, AST, JSON, or YAML
|
|
880
|
+
* @param {Object} options - Configuration options
|
|
881
|
+
* @returns {string} - HTML string
|
|
882
|
+
*/
|
|
883
|
+
function quikdown_ast_html(input, options = {}) {
|
|
884
|
+
const ast = toAst(input, options);
|
|
885
|
+
return renderAst(ast, options);
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
/**
|
|
889
|
+
* Render an AST node to HTML
|
|
890
|
+
*/
|
|
891
|
+
function renderAst(node, options = {}) {
|
|
892
|
+
if (!node) return '';
|
|
893
|
+
|
|
894
|
+
const { inline_styles = false } = options;
|
|
895
|
+
const getAttr = createGetAttr(inline_styles);
|
|
896
|
+
|
|
897
|
+
return renderNode(node, getAttr, options);
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
/**
|
|
901
|
+
* Render a single node
|
|
902
|
+
*/
|
|
903
|
+
function renderNode(node, getAttr, options) {
|
|
904
|
+
if (!node) return '';
|
|
905
|
+
|
|
906
|
+
switch (node.type) {
|
|
907
|
+
case 'document':
|
|
908
|
+
return renderChildren(node.children, getAttr, options);
|
|
909
|
+
|
|
910
|
+
case 'paragraph':
|
|
911
|
+
return `<p>${renderChildren(node.children, getAttr, options)}</p>`;
|
|
912
|
+
|
|
913
|
+
case 'heading':
|
|
914
|
+
const level = node.level || 1;
|
|
915
|
+
return `<h${level}${getAttr('h' + level)}>${renderChildren(node.children, getAttr, options)}</h${level}>`;
|
|
916
|
+
|
|
917
|
+
case 'code_block':
|
|
918
|
+
const langClass = !options.inline_styles && node.lang ? ` class="language-${node.lang}"` : '';
|
|
919
|
+
const codeAttr = options.inline_styles ? getAttr('code') : langClass;
|
|
920
|
+
return `<pre${getAttr('pre')}><code${codeAttr}>${escapeHtml(node.content)}</code></pre>`;
|
|
921
|
+
|
|
922
|
+
case 'blockquote':
|
|
923
|
+
return `<blockquote${getAttr('blockquote')}>${renderChildren(node.children, getAttr, options)}</blockquote>`;
|
|
924
|
+
|
|
925
|
+
case 'list':
|
|
926
|
+
const listTag = node.ordered ? 'ol' : 'ul';
|
|
927
|
+
const items = (node.items || []).map(item => renderNode(item, getAttr, options)).join('');
|
|
928
|
+
return `<${listTag}${getAttr(listTag)}>${items}</${listTag}>`;
|
|
929
|
+
|
|
930
|
+
case 'list_item':
|
|
931
|
+
// Handle task list items
|
|
932
|
+
if (node.checked !== null && node.checked !== undefined) {
|
|
933
|
+
const checkboxAttr = options.inline_styles
|
|
934
|
+
? ' style="margin-right:.5em"'
|
|
935
|
+
: ` class="${CLASS_PREFIX}task-checkbox"`;
|
|
936
|
+
const checked = node.checked ? ' checked' : '';
|
|
937
|
+
const itemAttr = options.inline_styles
|
|
938
|
+
? ' style="list-style:none"'
|
|
939
|
+
: ` class="${CLASS_PREFIX}task-item"`;
|
|
940
|
+
return `<li${itemAttr}><input type="checkbox"${checkboxAttr}${checked} disabled> ${renderChildren(node.children, getAttr, options)}</li>`;
|
|
941
|
+
}
|
|
942
|
+
return `<li${getAttr('li')}>${renderChildren(node.children, getAttr, options)}</li>`;
|
|
943
|
+
|
|
944
|
+
case 'table':
|
|
945
|
+
return renderTable(node, getAttr, options);
|
|
946
|
+
|
|
947
|
+
case 'hr':
|
|
948
|
+
return `<hr${getAttr('hr')}>`;
|
|
949
|
+
|
|
950
|
+
case 'text':
|
|
951
|
+
return escapeHtml(node.value || '');
|
|
952
|
+
|
|
953
|
+
case 'strong':
|
|
954
|
+
return `<strong${getAttr('strong')}>${renderChildren(node.children, getAttr, options)}</strong>`;
|
|
955
|
+
|
|
956
|
+
case 'em':
|
|
957
|
+
return `<em${getAttr('em')}>${renderChildren(node.children, getAttr, options)}</em>`;
|
|
958
|
+
|
|
959
|
+
case 'del':
|
|
960
|
+
return `<del${getAttr('del')}>${renderChildren(node.children, getAttr, options)}</del>`;
|
|
961
|
+
|
|
962
|
+
case 'code':
|
|
963
|
+
return `<code${getAttr('code')}>${escapeHtml(node.value || '')}</code>`;
|
|
964
|
+
|
|
965
|
+
case 'link':
|
|
966
|
+
const sanitizedHref = sanitizeUrl(node.url);
|
|
967
|
+
const isExternal = /^https?:\/\//i.test(sanitizedHref);
|
|
968
|
+
const rel = isExternal ? ' rel="noopener noreferrer"' : '';
|
|
969
|
+
return `<a${getAttr('a')} href="${sanitizedHref}"${rel}>${renderChildren(node.children, getAttr, options)}</a>`;
|
|
970
|
+
|
|
971
|
+
case 'image':
|
|
972
|
+
const sanitizedSrc = sanitizeUrl(node.url);
|
|
973
|
+
return `<img${getAttr('img')} src="${sanitizedSrc}" alt="${escapeHtml(node.alt || '')}">`;
|
|
974
|
+
|
|
975
|
+
case 'br':
|
|
976
|
+
return '<br>';
|
|
977
|
+
|
|
978
|
+
default:
|
|
979
|
+
// Unknown node type - try to render children if present
|
|
980
|
+
if (node.children) {
|
|
981
|
+
return renderChildren(node.children, getAttr, options);
|
|
982
|
+
}
|
|
983
|
+
if (node.value !== undefined) {
|
|
984
|
+
return escapeHtml(String(node.value));
|
|
985
|
+
}
|
|
986
|
+
return '';
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
/**
|
|
991
|
+
* Render children array
|
|
992
|
+
*/
|
|
993
|
+
function renderChildren(children, getAttr, options) {
|
|
994
|
+
if (!children) return '';
|
|
995
|
+
if (!Array.isArray(children)) {
|
|
996
|
+
return renderNode(children, getAttr, options);
|
|
997
|
+
}
|
|
998
|
+
return children.map(child => renderNode(child, getAttr, options)).join('');
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
/**
|
|
1002
|
+
* Render a table node
|
|
1003
|
+
*/
|
|
1004
|
+
function renderTable(node, getAttr, options) {
|
|
1005
|
+
const alignments = node.alignments || [];
|
|
1006
|
+
|
|
1007
|
+
let html = `<table${getAttr('table')}>\n`;
|
|
1008
|
+
|
|
1009
|
+
// Headers
|
|
1010
|
+
if (node.headers && node.headers.length > 0) {
|
|
1011
|
+
html += '<thead>\n<tr>\n';
|
|
1012
|
+
node.headers.forEach((header, i) => {
|
|
1013
|
+
const alignStyle = alignments[i] && alignments[i] !== 'left' ? `text-align:${alignments[i]}` : '';
|
|
1014
|
+
html += `<th${getAttr('th', alignStyle)}>${renderChildren(header, getAttr, options)}</th>\n`;
|
|
1015
|
+
});
|
|
1016
|
+
html += '</tr>\n</thead>\n';
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
// Body
|
|
1020
|
+
if (node.rows && node.rows.length > 0) {
|
|
1021
|
+
html += '<tbody>\n';
|
|
1022
|
+
node.rows.forEach(row => {
|
|
1023
|
+
html += '<tr>\n';
|
|
1024
|
+
row.forEach((cell, i) => {
|
|
1025
|
+
const alignStyle = alignments[i] && alignments[i] !== 'left' ? `text-align:${alignments[i]}` : '';
|
|
1026
|
+
html += `<td${getAttr('td', alignStyle)}>${renderChildren(cell, getAttr, options)}</td>\n`;
|
|
1027
|
+
});
|
|
1028
|
+
html += '</tr>\n';
|
|
1029
|
+
});
|
|
1030
|
+
html += '</tbody>\n';
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
html += '</table>';
|
|
1034
|
+
return html;
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
// Expose helper functions
|
|
1038
|
+
quikdown_ast_html.toAst = toAst;
|
|
1039
|
+
quikdown_ast_html.renderAst = renderAst;
|
|
1040
|
+
|
|
1041
|
+
// Attach version
|
|
1042
|
+
quikdown_ast_html.version = quikdownVersion;
|
|
1043
|
+
|
|
1044
|
+
// Export for both CommonJS and ES6
|
|
1045
|
+
/* istanbul ignore next */
|
|
1046
|
+
if (typeof module !== 'undefined' && module.exports) {
|
|
1047
|
+
module.exports = quikdown_ast_html;
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
// For browser global
|
|
1051
|
+
/* istanbul ignore next */
|
|
1052
|
+
if (typeof window !== 'undefined') {
|
|
1053
|
+
window.quikdown_ast_html = quikdown_ast_html;
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
export { quikdown_ast_html as default };
|