quikdown 1.1.1 → 1.2.3

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.
Files changed (72) hide show
  1. package/README.md +39 -7
  2. package/dist/quikdown.cjs +23 -10
  3. package/dist/quikdown.dark.css +1 -1
  4. package/dist/quikdown.esm.js +23 -10
  5. package/dist/quikdown.esm.min.js +2 -2
  6. package/dist/quikdown.esm.min.js.gz +0 -0
  7. package/dist/quikdown.esm.min.js.map +1 -1
  8. package/dist/quikdown.light.css +1 -1
  9. package/dist/quikdown.umd.js +23 -10
  10. package/dist/quikdown.umd.min.js +2 -2
  11. package/dist/quikdown.umd.min.js.gz +0 -0
  12. package/dist/quikdown.umd.min.js.map +1 -1
  13. package/dist/quikdown_ast.cjs +513 -0
  14. package/dist/quikdown_ast.d.ts +227 -0
  15. package/dist/quikdown_ast.esm.js +511 -0
  16. package/dist/quikdown_ast.esm.min.js +8 -0
  17. package/dist/quikdown_ast.esm.min.js.gz +0 -0
  18. package/dist/quikdown_ast.esm.min.js.map +1 -0
  19. package/dist/quikdown_ast.umd.js +519 -0
  20. package/dist/quikdown_ast.umd.min.js +8 -0
  21. package/dist/quikdown_ast.umd.min.js.gz +0 -0
  22. package/dist/quikdown_ast.umd.min.js.map +1 -0
  23. package/dist/quikdown_ast_html.cjs +1058 -0
  24. package/dist/quikdown_ast_html.d.ts +68 -0
  25. package/dist/quikdown_ast_html.esm.js +1056 -0
  26. package/dist/quikdown_ast_html.esm.min.js +8 -0
  27. package/dist/quikdown_ast_html.esm.min.js.gz +0 -0
  28. package/dist/quikdown_ast_html.esm.min.js.map +1 -0
  29. package/dist/quikdown_ast_html.umd.js +1064 -0
  30. package/dist/quikdown_ast_html.umd.min.js +8 -0
  31. package/dist/quikdown_ast_html.umd.min.js.gz +0 -0
  32. package/dist/quikdown_ast_html.umd.min.js.map +1 -0
  33. package/dist/quikdown_bd.cjs +38 -19
  34. package/dist/quikdown_bd.esm.js +38 -19
  35. package/dist/quikdown_bd.esm.min.js +2 -2
  36. package/dist/quikdown_bd.esm.min.js.gz +0 -0
  37. package/dist/quikdown_bd.esm.min.js.map +1 -1
  38. package/dist/quikdown_bd.umd.js +38 -19
  39. package/dist/quikdown_bd.umd.min.js +2 -2
  40. package/dist/quikdown_bd.umd.min.js.gz +0 -0
  41. package/dist/quikdown_bd.umd.min.js.map +1 -1
  42. package/dist/quikdown_edit.cjs +836 -117
  43. package/dist/quikdown_edit.d.ts +123 -131
  44. package/dist/quikdown_edit.esm.js +836 -117
  45. package/dist/quikdown_edit.esm.min.js +3 -3
  46. package/dist/quikdown_edit.esm.min.js.gz +0 -0
  47. package/dist/quikdown_edit.esm.min.js.map +1 -1
  48. package/dist/quikdown_edit.umd.js +836 -117
  49. package/dist/quikdown_edit.umd.min.js +3 -3
  50. package/dist/quikdown_edit.umd.min.js.gz +0 -0
  51. package/dist/quikdown_edit.umd.min.js.map +1 -1
  52. package/dist/quikdown_json.cjs +556 -0
  53. package/dist/quikdown_json.d.ts +48 -0
  54. package/dist/quikdown_json.esm.js +554 -0
  55. package/dist/quikdown_json.esm.min.js +8 -0
  56. package/dist/quikdown_json.esm.min.js.gz +0 -0
  57. package/dist/quikdown_json.esm.min.js.map +1 -0
  58. package/dist/quikdown_json.umd.js +562 -0
  59. package/dist/quikdown_json.umd.min.js +8 -0
  60. package/dist/quikdown_json.umd.min.js.gz +0 -0
  61. package/dist/quikdown_json.umd.min.js.map +1 -0
  62. package/dist/quikdown_yaml.cjs +717 -0
  63. package/dist/quikdown_yaml.d.ts +51 -0
  64. package/dist/quikdown_yaml.esm.js +715 -0
  65. package/dist/quikdown_yaml.esm.min.js +8 -0
  66. package/dist/quikdown_yaml.esm.min.js.gz +0 -0
  67. package/dist/quikdown_yaml.esm.min.js.map +1 -0
  68. package/dist/quikdown_yaml.umd.js +723 -0
  69. package/dist/quikdown_yaml.umd.min.js +8 -0
  70. package/dist/quikdown_yaml.umd.min.js.gz +0 -0
  71. package/dist/quikdown_yaml.umd.min.js.map +1 -0
  72. package/package.json +100 -45
@@ -0,0 +1,723 @@
1
+ /**
2
+ * quikdown_yaml - YAML Markdown Parser
3
+ * @version 1.2.3
4
+ * @license BSD-2-Clause
5
+ * @copyright DeftIO 2025
6
+ */
7
+ (function (global, factory) {
8
+ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
9
+ typeof define === 'function' && define.amd ? define(factory) :
10
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.quikdown_yaml = factory());
11
+ })(this, (function () { 'use strict';
12
+
13
+ /**
14
+ * quikdown_ast - Forgiving markdown to AST parser
15
+ * Converts markdown to a structured Abstract Syntax Tree
16
+ * @param {string} markdown - The markdown source text
17
+ * @param {Object} options - Optional configuration object
18
+ * @returns {Object} - The AST object
19
+ */
20
+
21
+ // Version will be injected at build time
22
+ const quikdownVersion$1 = '1.2.3';
23
+
24
+ // Safety limit to prevent infinite loops in list parsing
25
+ const MAX_LOOP_ITERATIONS = 1000;
26
+
27
+ /**
28
+ * Parse markdown into an AST
29
+ * @param {string} markdown - The markdown source text
30
+ * @param {Object} options - Optional configuration object
31
+ * @returns {Object} - The AST object
32
+ */
33
+ function quikdown_ast(markdown, options = {}) {
34
+ if (!markdown || typeof markdown !== 'string') {
35
+ return { type: 'document', children: [] };
36
+ }
37
+
38
+ // Normalize line endings (handle CRLF, CR, LF uniformly)
39
+ const text = markdown.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
40
+
41
+ const children = parseBlocks(text);
42
+
43
+ return {
44
+ type: 'document',
45
+ children
46
+ };
47
+ }
48
+
49
+ /**
50
+ * Parse block-level elements
51
+ */
52
+ function parseBlocks(text, options) {
53
+ const blocks = [];
54
+ const lines = text.split('\n');
55
+ let i = 0;
56
+
57
+ while (i < lines.length) {
58
+ const line = lines[i];
59
+
60
+ // Empty line - skip
61
+ if (line.trim() === '') {
62
+ i++;
63
+ continue;
64
+ }
65
+
66
+ // Fenced code block (``` or ~~~)
67
+ const fenceMatch = line.match(/^(```|~~~)(.*)$/);
68
+ if (fenceMatch) {
69
+ const [, openFence, langPart] = fenceMatch;
70
+ const lang = langPart.trim();
71
+ const codeLines = [];
72
+ i++;
73
+
74
+ // Find closing fence (forgiving: accept mismatched fences or EOF)
75
+ while (i < lines.length) {
76
+ const closingMatch = lines[i].match(/^(```|~~~)\s*$/);
77
+ if (closingMatch) {
78
+ i++;
79
+ break;
80
+ }
81
+ codeLines.push(lines[i]);
82
+ i++;
83
+ }
84
+
85
+ blocks.push({
86
+ type: 'code_block',
87
+ lang: lang || null,
88
+ content: codeLines.join('\n'),
89
+ fence: openFence
90
+ });
91
+ continue;
92
+ }
93
+
94
+ // Horizontal rule
95
+ if (/^---+\s*$/.test(line) || /^\*\*\*+\s*$/.test(line) || /^___+\s*$/.test(line)) {
96
+ blocks.push({ type: 'hr' });
97
+ i++;
98
+ continue;
99
+ }
100
+
101
+ // Heading (forgiving: accept #heading without space)
102
+ const headingMatch = line.match(/^(#{1,6})\s*(.+?)\s*#*$/);
103
+ if (headingMatch) {
104
+ const [, hashes, content] = headingMatch;
105
+ blocks.push({
106
+ type: 'heading',
107
+ level: hashes.length,
108
+ children: parseInline(content)
109
+ });
110
+ i++;
111
+ continue;
112
+ }
113
+
114
+ // Table (look for separator line)
115
+ if (line.includes('|')) {
116
+ const tableResult = tryParseTable(lines, i);
117
+ if (tableResult) {
118
+ blocks.push(tableResult.node);
119
+ i = tableResult.nextIndex;
120
+ continue;
121
+ }
122
+ }
123
+
124
+ // Blockquote
125
+ if (line.match(/^>\s*/)) {
126
+ const quoteLines = [];
127
+ while (i < lines.length && lines[i].match(/^>\s*/)) {
128
+ quoteLines.push(lines[i].replace(/^>\s*/, ''));
129
+ i++;
130
+ }
131
+ blocks.push({
132
+ type: 'blockquote',
133
+ children: parseBlocks(quoteLines.join('\n'))
134
+ });
135
+ continue;
136
+ }
137
+
138
+ // List (ordered or unordered)
139
+ const listMatch = line.match(/^(\s*)([*\-+]|\d+\.)\s+(.*)$/);
140
+ if (listMatch) {
141
+ const listResult = parseList(lines, i);
142
+ blocks.push(listResult.node);
143
+ i = listResult.nextIndex;
144
+ continue;
145
+ }
146
+
147
+ // Paragraph - collect lines until empty line or block element
148
+ const paragraphLines = [];
149
+ while (i < lines.length) {
150
+ const pLine = lines[i];
151
+
152
+ // Stop on empty line
153
+ if (pLine.trim() === '') break;
154
+
155
+ // Stop on block elements
156
+ if (/^(```|~~~)/.test(pLine)) break;
157
+ if (/^#{1,6}\s/.test(pLine)) break;
158
+ if (/^---+\s*$/.test(pLine) || /^\*\*\*+\s*$/.test(pLine) || /^___+\s*$/.test(pLine)) break;
159
+ if (/^>\s*/.test(pLine)) break;
160
+ if (/^(\s*)([*\-+]|\d+\.)\s+/.test(pLine)) break;
161
+ if (pLine.includes('|') && i + 1 < lines.length && /^\|?[\s\-:|]+\|?$/.test(lines[i + 1])) break;
162
+
163
+ paragraphLines.push(pLine);
164
+ i++;
165
+ }
166
+
167
+ if (paragraphLines.length > 0) {
168
+ blocks.push({
169
+ type: 'paragraph',
170
+ children: parseInline(paragraphLines.join('\n'))
171
+ });
172
+ }
173
+ }
174
+
175
+ return blocks;
176
+ }
177
+
178
+ /**
179
+ * Try to parse a table starting at the given line
180
+ */
181
+ function tryParseTable(lines, startIndex, options) {
182
+ // Need at least 2 lines (header + separator)
183
+ if (startIndex + 1 >= lines.length) return null;
184
+
185
+ const headerLine = lines[startIndex];
186
+ const separatorLine = lines[startIndex + 1];
187
+
188
+ // Check if separator line is valid
189
+ if (!/^\|?[\s\-:|]+\|?$/.test(separatorLine) || !separatorLine.includes('-')) {
190
+ return null;
191
+ }
192
+
193
+ // Parse header
194
+ const headerCells = parseTableRow(headerLine);
195
+ if (headerCells.length === 0) return null;
196
+
197
+ // Parse alignments from separator
198
+ const separatorCells = parseTableRow(separatorLine);
199
+ const alignments = separatorCells.map(cell => {
200
+ const trimmed = cell.trim();
201
+ if (trimmed.startsWith(':') && trimmed.endsWith(':')) return 'center';
202
+ if (trimmed.endsWith(':')) return 'right';
203
+ return 'left';
204
+ });
205
+
206
+ // Parse headers with inline formatting
207
+ const headers = headerCells.map(cell => parseInline(cell.trim()));
208
+
209
+ // Parse body rows
210
+ const rows = [];
211
+ let i = startIndex + 2;
212
+ while (i < lines.length) {
213
+ const rowLine = lines[i];
214
+ if (!rowLine.includes('|') || rowLine.trim() === '') break;
215
+
216
+ const cells = parseTableRow(rowLine);
217
+ rows.push(cells.map(cell => parseInline(cell.trim())));
218
+ i++;
219
+ }
220
+
221
+ return {
222
+ node: {
223
+ type: 'table',
224
+ headers,
225
+ rows,
226
+ alignments
227
+ },
228
+ nextIndex: i
229
+ };
230
+ }
231
+
232
+ /**
233
+ * Parse a table row into cells
234
+ */
235
+ function parseTableRow(line) {
236
+ // Handle pipes at start/end or not
237
+ let trimmed = line.trim();
238
+ if (trimmed.startsWith('|')) trimmed = trimmed.slice(1);
239
+ if (trimmed.endsWith('|')) trimmed = trimmed.slice(0, -1);
240
+ return trimmed.split('|');
241
+ }
242
+
243
+ /**
244
+ * Parse a list starting at the given line
245
+ */
246
+ function parseList(lines, startIndex, options) {
247
+ const items = [];
248
+ let i = startIndex;
249
+ let loopCount = 0;
250
+
251
+ // Determine initial list type
252
+ const firstMatch = lines[i].match(/^(\s*)([*\-+]|\d+\.)\s+(.*)$/);
253
+ const isOrdered = /^\d+\./.test(firstMatch[2]);
254
+ const baseIndent = firstMatch[1].length;
255
+
256
+ while (i < lines.length && loopCount < MAX_LOOP_ITERATIONS) {
257
+ loopCount++;
258
+ const line = lines[i];
259
+ const match = line.match(/^(\s*)([*\-+]|\d+\.)\s+(.*)$/);
260
+
261
+ if (!match) break;
262
+
263
+ const [, indent, marker, content] = match;
264
+ const indentLevel = indent.length;
265
+
266
+ // If less indented than base, stop
267
+ if (indentLevel < baseIndent) break;
268
+
269
+ // If same indentation but different list type, stop
270
+ const itemIsOrdered = /^\d+\./.test(marker);
271
+ if (indentLevel === baseIndent && itemIsOrdered !== isOrdered) break;
272
+
273
+ // If more indented, it's a nested list - handle by collecting sub-lines
274
+ if (indentLevel > baseIndent) {
275
+ // This is a nested list item, collect and parse as sublist
276
+ const subLines = [];
277
+ let subLoopCount = 0;
278
+ while (i < lines.length && subLoopCount < MAX_LOOP_ITERATIONS) {
279
+ subLoopCount++;
280
+ const subLine = lines[i];
281
+ const subMatch = subLine.match(/^(\s*)([*\-+]|\d+\.)\s+/);
282
+ if (!subMatch) break;
283
+ if (subMatch[1].length < baseIndent) break;
284
+ if (subMatch[1].length === baseIndent) break;
285
+ subLines.push(subLine);
286
+ i++;
287
+ }
288
+
289
+ if (subLines.length > 0 && items.length > 0) {
290
+ // Add nested list to last item
291
+ const nestedResult = parseList(subLines, 0);
292
+ const lastItem = items[items.length - 1];
293
+ if (!lastItem.children) {
294
+ lastItem.children = [];
295
+ } else if (!Array.isArray(lastItem.children)) {
296
+ lastItem.children = [{ type: 'paragraph', children: lastItem.children }];
297
+ }
298
+ lastItem.children.push(nestedResult.node);
299
+ }
300
+ continue;
301
+ }
302
+
303
+ // Parse list item
304
+ const itemNode = {
305
+ type: 'list_item',
306
+ checked: null,
307
+ children: null
308
+ };
309
+
310
+ // Check for task list syntax
311
+ const taskMatch = content.match(/^\[([x ])\]\s*(.*)$/i);
312
+ if (taskMatch && !isOrdered) {
313
+ itemNode.checked = taskMatch[1].toLowerCase() === 'x';
314
+ itemNode.children = parseInline(taskMatch[2]);
315
+ } else {
316
+ itemNode.children = parseInline(content);
317
+ }
318
+
319
+ items.push(itemNode);
320
+ i++;
321
+ }
322
+
323
+ return {
324
+ node: {
325
+ type: 'list',
326
+ ordered: isOrdered,
327
+ items
328
+ },
329
+ nextIndex: i
330
+ };
331
+ }
332
+
333
+ /**
334
+ * Parse inline elements
335
+ */
336
+ function parseInline(text, options) {
337
+ if (!text) return [];
338
+
339
+ const nodes = [];
340
+ let remaining = text;
341
+
342
+ while (remaining.length > 0) {
343
+ let matched = false;
344
+
345
+ // Line break (1+ trailing spaces or explicit \n after processing)
346
+ // Handle inline line breaks (two spaces at end of line or backslash before newline)
347
+ const brMatch = remaining.match(/^(.+?)(?: {2}|\\\n|\n)/);
348
+ if (brMatch && remaining.includes('\n')) {
349
+ const beforeBr = remaining.indexOf('\n');
350
+ const beforeText = remaining.slice(0, beforeBr);
351
+ const afterText = remaining.slice(beforeBr + 1);
352
+
353
+ // Check if line break is significant (2+ trailing spaces or backslash)
354
+ if (beforeText.endsWith(' ') || beforeText.endsWith('\\')) {
355
+ const cleanText = beforeText.replace(/\\$/, '').replace(/ +$/, '');
356
+ if (cleanText) {
357
+ nodes.push(...parseInlineContent(cleanText));
358
+ }
359
+ nodes.push({ type: 'br' });
360
+ remaining = afterText;
361
+ matched = true;
362
+ continue;
363
+ }
364
+ }
365
+
366
+ // Images: ![alt](url)
367
+ const imgMatch = remaining.match(/^!\[([^\]]*)\]\(\s*([^)\s]+)\s*\)/);
368
+ if (imgMatch) {
369
+ nodes.push({
370
+ type: 'image',
371
+ alt: imgMatch[1],
372
+ url: imgMatch[2].trim() // Forgiving: trim whitespace in URL
373
+ });
374
+ remaining = remaining.slice(imgMatch[0].length);
375
+ matched = true;
376
+ continue;
377
+ }
378
+
379
+ // Links: [text](url)
380
+ const linkMatch = remaining.match(/^\[([^\]]+)\]\(\s*([^)\s]+)\s*\)/);
381
+ if (linkMatch) {
382
+ nodes.push({
383
+ type: 'link',
384
+ url: linkMatch[2].trim(), // Forgiving: trim whitespace in URL
385
+ children: parseInlineContent(linkMatch[1])
386
+ });
387
+ remaining = remaining.slice(linkMatch[0].length);
388
+ matched = true;
389
+ continue;
390
+ }
391
+
392
+ // Inline code: `code`
393
+ const codeMatch = remaining.match(/^`([^`]+)`/);
394
+ if (codeMatch) {
395
+ nodes.push({
396
+ type: 'code',
397
+ value: codeMatch[1]
398
+ });
399
+ remaining = remaining.slice(codeMatch[0].length);
400
+ matched = true;
401
+ continue;
402
+ }
403
+
404
+ // Bold: **text** or __text__
405
+ const boldMatch = remaining.match(/^(\*\*|__)(.+?)\1/);
406
+ if (boldMatch) {
407
+ nodes.push({
408
+ type: 'strong',
409
+ children: parseInlineContent(boldMatch[2])
410
+ });
411
+ remaining = remaining.slice(boldMatch[0].length);
412
+ matched = true;
413
+ continue;
414
+ }
415
+
416
+ // Strikethrough: ~~text~~
417
+ const strikeMatch = remaining.match(/^~~(.+?)~~/);
418
+ if (strikeMatch) {
419
+ nodes.push({
420
+ type: 'del',
421
+ children: parseInlineContent(strikeMatch[1])
422
+ });
423
+ remaining = remaining.slice(strikeMatch[0].length);
424
+ matched = true;
425
+ continue;
426
+ }
427
+
428
+ // Italic: *text* or _text_ (not at word boundary for underscores)
429
+ const emMatch = remaining.match(/^(\*|_)(?!\1)(.+?)(?<!\1)\1(?!\1)/);
430
+ if (emMatch) {
431
+ nodes.push({
432
+ type: 'em',
433
+ children: parseInlineContent(emMatch[2])
434
+ });
435
+ remaining = remaining.slice(emMatch[0].length);
436
+ matched = true;
437
+ continue;
438
+ }
439
+
440
+ // Autolinks: URLs starting with http:// or https://
441
+ const urlMatch = remaining.match(/^(https?:\/\/[^\s<>[\]]+)/);
442
+ if (urlMatch) {
443
+ nodes.push({
444
+ type: 'link',
445
+ url: urlMatch[1],
446
+ children: [{ type: 'text', value: urlMatch[1] }]
447
+ });
448
+ remaining = remaining.slice(urlMatch[0].length);
449
+ matched = true;
450
+ continue;
451
+ }
452
+
453
+ // Plain text - consume until next potential inline element or end
454
+ if (!matched) {
455
+ // Find next potential inline marker
456
+ const nextMarker = remaining.search(/[`*_~![\\n]|https?:\/\//);
457
+ if (nextMarker === -1) {
458
+ // No more markers, consume rest as text
459
+ nodes.push({ type: 'text', value: remaining });
460
+ break;
461
+ } else if (nextMarker === 0) {
462
+ // Current char is a marker but didn't match - consume it as text
463
+ nodes.push({ type: 'text', value: remaining[0] });
464
+ remaining = remaining.slice(1);
465
+ } else {
466
+ // Consume text up to next marker
467
+ nodes.push({ type: 'text', value: remaining.slice(0, nextMarker) });
468
+ remaining = remaining.slice(nextMarker);
469
+ }
470
+ }
471
+ }
472
+
473
+ // Merge adjacent text nodes
474
+ return mergeTextNodes(nodes);
475
+ }
476
+
477
+ /**
478
+ * Parse inline content (recursive helper for nested inline elements)
479
+ */
480
+ function parseInlineContent(text, options) {
481
+ // For simple nested content, use parseInline
482
+ // But handle newlines as spaces for inline content
483
+ const normalized = text.replace(/\n/g, ' ');
484
+ return parseInline(normalized);
485
+ }
486
+
487
+ /**
488
+ * Merge adjacent text nodes
489
+ */
490
+ function mergeTextNodes(nodes) {
491
+ const merged = [];
492
+ for (const node of nodes) {
493
+ if (node.type === 'text' && merged.length > 0 && merged[merged.length - 1].type === 'text') {
494
+ merged[merged.length - 1].value += node.value;
495
+ } else {
496
+ merged.push(node);
497
+ }
498
+ }
499
+ return merged;
500
+ }
501
+
502
+ // Attach version
503
+ quikdown_ast.version = quikdownVersion$1;
504
+
505
+ // Export for both CommonJS and ES6
506
+ /* istanbul ignore next */
507
+ if (typeof module !== 'undefined' && module.exports) {
508
+ module.exports = quikdown_ast;
509
+ }
510
+
511
+ // For browser global
512
+ /* istanbul ignore next */
513
+ if (typeof window !== 'undefined') {
514
+ window.quikdown_ast = quikdown_ast;
515
+ }
516
+
517
+ /**
518
+ * quikdown_yaml - Markdown to YAML converter
519
+ * Converts markdown to YAML via AST
520
+ * @param {string} markdown - The markdown source text
521
+ * @param {Object} options - Optional configuration object
522
+ * @returns {string} - YAML string representation of the AST
523
+ */
524
+
525
+
526
+ // Version will be injected at build time
527
+ const quikdownVersion = '1.2.3';
528
+
529
+ /**
530
+ * Convert markdown to YAML
531
+ * @param {string} markdown - The markdown source text
532
+ * @param {Object} options - Optional configuration object
533
+ * @returns {string} - YAML string
534
+ */
535
+ function quikdown_yaml(markdown, options = {}) {
536
+ const ast = quikdown_ast(markdown, options);
537
+ return astToYaml(ast, 0);
538
+ }
539
+
540
+ /**
541
+ * Convert an AST node to YAML string
542
+ * Minimal YAML serializer - no external dependencies
543
+ * @param {Object|Array|string|number|boolean|null} node - The value to serialize
544
+ * @param {number} indent - Current indentation level
545
+ * @returns {string} - YAML string
546
+ */
547
+ function astToYaml(node, indent) {
548
+ const spaces = ' '.repeat(indent);
549
+
550
+ if (node === null || node === undefined) {
551
+ return 'null';
552
+ }
553
+
554
+ if (typeof node === 'boolean') {
555
+ return node ? 'true' : 'false';
556
+ }
557
+
558
+ if (typeof node === 'number') {
559
+ return String(node);
560
+ }
561
+
562
+ if (typeof node === 'string') {
563
+ return formatYamlString(node);
564
+ }
565
+
566
+ if (Array.isArray(node)) {
567
+ if (node.length === 0) {
568
+ return '[]';
569
+ }
570
+
571
+ const lines = [];
572
+ for (const item of node) {
573
+ if (typeof item === 'object' && item !== null && !Array.isArray(item)) {
574
+ // Object item - inline the first property on the same line as dash
575
+ const props = Object.entries(item);
576
+ if (props.length > 0) {
577
+ const [firstKey, firstValue] = props[0];
578
+ const firstValueStr = formatValue(firstValue, indent + 1);
579
+ lines.push(`${spaces}- ${firstKey}: ${firstValueStr}`);
580
+
581
+ // Remaining properties
582
+ for (let i = 1; i < props.length; i++) {
583
+ const [key, value] = props[i];
584
+ const valueStr = formatValue(value, indent + 1);
585
+ lines.push(`${spaces} ${key}: ${valueStr}`);
586
+ }
587
+ } else {
588
+ lines.push(`${spaces}- {}`);
589
+ }
590
+ } else {
591
+ // Simple value
592
+ const valueStr = astToYaml(item, indent + 1);
593
+ lines.push(`${spaces}- ${valueStr}`);
594
+ }
595
+ }
596
+ return '\n' + lines.join('\n');
597
+ }
598
+
599
+ if (typeof node === 'object') {
600
+ const entries = Object.entries(node);
601
+ if (entries.length === 0) {
602
+ return '{}';
603
+ }
604
+
605
+ const lines = [];
606
+ for (const [key, value] of entries) {
607
+ const valueStr = formatValue(value, indent);
608
+ lines.push(`${spaces}${key}: ${valueStr}`);
609
+ }
610
+ return lines.join('\n');
611
+ }
612
+
613
+ return String(node);
614
+ }
615
+
616
+ /**
617
+ * Format a value for YAML (handles nested objects/arrays)
618
+ */
619
+ function formatValue(value, indent) {
620
+ if (value === null || value === undefined) {
621
+ return 'null';
622
+ }
623
+
624
+ if (typeof value === 'boolean') {
625
+ return value ? 'true' : 'false';
626
+ }
627
+
628
+ if (typeof value === 'number') {
629
+ return String(value);
630
+ }
631
+
632
+ if (typeof value === 'string') {
633
+ return formatYamlString(value);
634
+ }
635
+
636
+ if (Array.isArray(value)) {
637
+ if (value.length === 0) {
638
+ return '[]';
639
+ }
640
+ return astToYaml(value, indent + 1);
641
+ }
642
+
643
+ if (typeof value === 'object') {
644
+ // Nested object - format on new lines with increased indent
645
+ const entries = Object.entries(value);
646
+ if (entries.length === 0) {
647
+ return '{}';
648
+ }
649
+ return '\n' + astToYaml(value, indent + 1);
650
+ }
651
+
652
+ return String(value);
653
+ }
654
+
655
+ /**
656
+ * Format a string for YAML (handle quoting and escaping)
657
+ */
658
+ function formatYamlString(str) {
659
+ // Check if string needs quoting
660
+ const needsQuoting = (
661
+ str === '' ||
662
+ str.includes('\n') ||
663
+ str.includes(':') ||
664
+ str.includes('#') ||
665
+ str.includes("'") ||
666
+ str.includes('"') ||
667
+ str.startsWith(' ') ||
668
+ str.endsWith(' ') ||
669
+ str.startsWith('-') ||
670
+ str.startsWith('[') ||
671
+ str.startsWith('{') ||
672
+ str === 'true' ||
673
+ str === 'false' ||
674
+ str === 'null' ||
675
+ str === 'yes' ||
676
+ str === 'no' ||
677
+ str === 'on' ||
678
+ str === 'off' ||
679
+ /^\d+$/.test(str) ||
680
+ /^\d+\.\d+$/.test(str)
681
+ );
682
+
683
+ if (!needsQuoting) {
684
+ return str;
685
+ }
686
+
687
+ // Use double quotes and escape special characters
688
+ if (str.includes('\n')) {
689
+ // For multiline strings, use literal block style
690
+ const escaped = str.replace(/\n/g, '\\n');
691
+ return `"${escaped.replace(/"/g, '\\"')}"`;
692
+ }
693
+
694
+ // Simple string quoting
695
+ return `"${str.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
696
+ }
697
+
698
+ // Expose the AST parser for direct access
699
+ quikdown_yaml.parse = quikdown_ast;
700
+
701
+ // Expose the YAML serializer for direct AST-to-YAML conversion
702
+ quikdown_yaml.stringify = function(ast) {
703
+ return astToYaml(ast, 0);
704
+ };
705
+
706
+ // Attach version
707
+ quikdown_yaml.version = quikdownVersion;
708
+
709
+ // Export for both CommonJS and ES6
710
+ /* istanbul ignore next */
711
+ if (typeof module !== 'undefined' && module.exports) {
712
+ module.exports = quikdown_yaml;
713
+ }
714
+
715
+ // For browser global
716
+ /* istanbul ignore next */
717
+ if (typeof window !== 'undefined') {
718
+ window.quikdown_yaml = quikdown_yaml;
719
+ }
720
+
721
+ return quikdown_yaml;
722
+
723
+ }));