@redocly/realm-plugin-asciidoc 0.8.0-next.14 → 0.8.0-next.16

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.
@@ -0,0 +1,833 @@
1
+ import path from 'node:path';
2
+ import Asciidoctor from '@asciidoctor/core';
3
+ const asciidoctor = Asciidoctor();
4
+ const DIAGRAM_TYPES = ['mermaid', 'excalidraw', 'plantuml'];
5
+ export function parseAsciidoc(content, relativePath, contentRoot) {
6
+ var _a;
7
+ const absolutePath = path.resolve(contentRoot, relativePath);
8
+ const doc = asciidoctor.load(content, {
9
+ safe: 'safe',
10
+ base_dir: path.dirname(absolutePath),
11
+ docfile: absolutePath,
12
+ attributes: {
13
+ showtitle: true,
14
+ 'source-highlighter': 'highlight.js',
15
+ icons: 'font',
16
+ idprefix: '',
17
+ idseparator: '-',
18
+ },
19
+ });
20
+ const title = ((_a = doc.getDocumentTitle()) === null || _a === void 0 ? void 0 : _a.toString()) || '';
21
+ const rawAttributes = doc.getAttributes();
22
+ const attributes = {};
23
+ for (const [key, value] of Object.entries(rawAttributes)) {
24
+ if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
25
+ attributes[key] = value;
26
+ }
27
+ }
28
+ // Walk the AST to produce serializable content nodes
29
+ const topBlocks = doc.getBlocks();
30
+ const contentNodes = [];
31
+ // AsciiDoc `= Title` (level 0) is the document title — asciidoctor excludes it from
32
+ // getBlocks()/getSections(). Prepend it as an H1 heading so it renders on the page.
33
+ if (title) {
34
+ const { html: titleHtml, icons: titleIcons } = extractInlineIcons(title);
35
+ contentNodes.push({
36
+ type: 'section',
37
+ id: '',
38
+ title: stripInlineHtml(title),
39
+ titleHtml,
40
+ titleIcons,
41
+ level: 1,
42
+ children: [],
43
+ });
44
+ }
45
+ contentNodes.push(...convertBlocks(topBlocks, relativePath));
46
+ // Build section tree for TOC, search, and AI docs
47
+ const sections = buildSectionTree(doc.getSections());
48
+ // Extract links from converted HTML (used for sitemap / link checking)
49
+ const convertedHtml = doc.convert();
50
+ const links = extractLinks(convertedHtml, relativePath);
51
+ return {
52
+ title,
53
+ attributes,
54
+ sections,
55
+ contentNodes,
56
+ links,
57
+ sourceContent: content,
58
+ };
59
+ }
60
+ function isDiagramType(value) {
61
+ if (!value)
62
+ return false;
63
+ return DIAGRAM_TYPES.includes(value);
64
+ }
65
+ function convertBlocks(blocks, relativePath) {
66
+ const nodes = [];
67
+ for (let i = 0; i < blocks.length; i++) {
68
+ const block = blocks[i];
69
+ const context = block.getContext();
70
+ switch (context) {
71
+ case 'section':
72
+ nodes.push(convertSection(block, relativePath));
73
+ break;
74
+ case 'paragraph':
75
+ nodes.push(convertParagraph(block));
76
+ break;
77
+ case 'listing': {
78
+ const codeNode = convertListing(block, blocks[i + 1]);
79
+ // If we consumed a colist, skip it
80
+ if (codeNode.type === 'codeBlock' &&
81
+ codeNode.calloutDescriptions &&
82
+ codeNode.calloutDescriptions.length > 0) {
83
+ const next = blocks[i + 1];
84
+ if (next && next.getContext() === 'colist') {
85
+ i++;
86
+ }
87
+ }
88
+ nodes.push(codeNode);
89
+ break;
90
+ }
91
+ case 'literal':
92
+ nodes.push(convertLiteral(block));
93
+ break;
94
+ case 'admonition':
95
+ nodes.push(convertAdmonition(block, relativePath));
96
+ break;
97
+ case 'ulist':
98
+ nodes.push(convertList(block, false, relativePath));
99
+ break;
100
+ case 'olist':
101
+ nodes.push(convertList(block, true, relativePath));
102
+ break;
103
+ case 'dlist':
104
+ nodes.push(convertDescriptionList(block, relativePath));
105
+ break;
106
+ case 'table':
107
+ nodes.push(convertTable(block));
108
+ break;
109
+ case 'image':
110
+ nodes.push(convertImage(block, relativePath));
111
+ break;
112
+ case 'quote':
113
+ case 'verse':
114
+ nodes.push(convertQuote(block, relativePath));
115
+ break;
116
+ case 'sidebar':
117
+ nodes.push(convertSidebar(block, relativePath));
118
+ break;
119
+ case 'example':
120
+ nodes.push(convertExample(block, relativePath));
121
+ break;
122
+ case 'open':
123
+ nodes.push(convertOpen(block, relativePath));
124
+ break;
125
+ case 'thematic_break':
126
+ nodes.push({ type: 'thematicBreak' });
127
+ break;
128
+ case 'pass':
129
+ case 'stem':
130
+ nodes.push(convertPassthrough(block));
131
+ break;
132
+ case 'preamble':
133
+ // Preamble is a wrapper; recurse into its children
134
+ nodes.push(...convertBlocks(block.getBlocks(), relativePath));
135
+ break;
136
+ case 'toc':
137
+ // Skip generated TOC — we use our own
138
+ break;
139
+ case 'colist':
140
+ // Colist is consumed alongside listing blocks; if orphaned, skip
141
+ break;
142
+ default:
143
+ console.warn(`[asciidoc-plugin] Unsupported block type "${context}" — skipping.`);
144
+ break;
145
+ }
146
+ }
147
+ return nodes;
148
+ }
149
+ function convertSection(section, relativePath) {
150
+ const id = section.getId() || slugify(section.getTitle() || '');
151
+ const rawTitle = section.getTitle() || '';
152
+ const level = Math.min(section.getLevel() + 1, 6); // +1 because section level is 0-based in AsciiDoc
153
+ // Section titles can contain inline icons (e.g. `== icon:flag[] Heading`)
154
+ const { html: titleHtml, icons: titleIcons } = extractInlineIcons(rawTitle);
155
+ // Plain text title (strip HTML) for TOC and search
156
+ const title = stripInlineHtml(rawTitle);
157
+ const children = convertBlocks(section.getBlocks(), relativePath);
158
+ return {
159
+ type: 'section',
160
+ id,
161
+ title,
162
+ titleHtml,
163
+ titleIcons,
164
+ level,
165
+ children,
166
+ };
167
+ }
168
+ function convertParagraph(block) {
169
+ const html = block.getContent() || '';
170
+ const { html: processedHtml, icons } = extractInlineIcons(html);
171
+ const role = block.getRole() || undefined;
172
+ return {
173
+ type: 'paragraph',
174
+ contentHtml: processedHtml,
175
+ icons,
176
+ role,
177
+ };
178
+ }
179
+ function convertListing(block, nextBlock) {
180
+ const lang = (block.getAttribute('language') || '').toLowerCase();
181
+ const style = (block.getStyle() || '').toLowerCase();
182
+ const rawSource = block.getSource() || '';
183
+ // Diagram type can come either from the language (e.g. `[source,plantuml]`)
184
+ // or the block style (e.g. `[plantuml]` directly).
185
+ const diagramType = isDiagramType(lang) ? lang : isDiagramType(style) ? style : null;
186
+ if (diagramType) {
187
+ return {
188
+ type: 'diagram',
189
+ diagramType,
190
+ source: rawSource,
191
+ id: block.getId() || undefined,
192
+ };
193
+ }
194
+ const title = block.getTitle() || undefined;
195
+ // Parse callout markers from source: <1>, <2>, etc.
196
+ const { cleaned, callouts } = parseCallouts(rawSource);
197
+ // Check if next block is a colist (callout list)
198
+ let calloutDescriptions;
199
+ if (nextBlock && nextBlock.getContext() === 'colist') {
200
+ calloutDescriptions = extractColistDescriptions(nextBlock);
201
+ }
202
+ return {
203
+ type: 'codeBlock',
204
+ lang,
205
+ source: cleaned,
206
+ title,
207
+ callouts: callouts.length > 0 ? callouts : undefined,
208
+ calloutDescriptions: calloutDescriptions && calloutDescriptions.length > 0 ? calloutDescriptions : undefined,
209
+ };
210
+ }
211
+ function convertLiteral(block) {
212
+ const source = block.getSource() || '';
213
+ const style = (block.getStyle() || '').toLowerCase();
214
+ // Diagram styles with the literal delimiter (`....`) — e.g. `[plantuml]....`
215
+ if (isDiagramType(style)) {
216
+ return {
217
+ type: 'diagram',
218
+ diagramType: style,
219
+ source,
220
+ id: block.getId() || undefined,
221
+ };
222
+ }
223
+ return {
224
+ type: 'codeBlock',
225
+ lang: '',
226
+ source,
227
+ title: block.getTitle() || undefined,
228
+ };
229
+ }
230
+ const ADMONITION_TYPE_MAP = {
231
+ note: 'info',
232
+ tip: 'success',
233
+ warning: 'warning',
234
+ caution: 'warning',
235
+ important: 'danger',
236
+ };
237
+ function convertAdmonition(block, relativePath) {
238
+ const style = (block.getStyle() || 'note').toLowerCase();
239
+ const admonitionType = ADMONITION_TYPE_MAP[style] || 'info';
240
+ const name = style.charAt(0).toUpperCase() + style.slice(1);
241
+ const innerBlocks = block.getBlocks();
242
+ const children = convertBlocks(innerBlocks, relativePath);
243
+ return {
244
+ type: 'admonition',
245
+ admonitionType,
246
+ name,
247
+ children,
248
+ };
249
+ }
250
+ function convertList(block, ordered, relativePath) {
251
+ const items = [];
252
+ for (const item of block.getItems()) {
253
+ const textHtml = item.getText() || '';
254
+ const { html: processedHtml, icons } = extractInlineIcons(textHtml);
255
+ const childBlocks = item.hasBlocks() ? item.getBlocks() : [];
256
+ const children = convertBlocks(childBlocks, relativePath);
257
+ items.push({
258
+ contentHtml: processedHtml,
259
+ icons,
260
+ children,
261
+ });
262
+ }
263
+ return {
264
+ type: 'list',
265
+ ordered,
266
+ items,
267
+ };
268
+ }
269
+ function convertDescriptionList(block, relativePath) {
270
+ const items = [];
271
+ const rawItems = block.getItems();
272
+ for (const pair of rawItems) {
273
+ if (!Array.isArray(pair) || pair.length < 2)
274
+ continue;
275
+ const [terms, definition] = pair;
276
+ const termsHtml = [];
277
+ if (Array.isArray(terms)) {
278
+ for (const term of terms) {
279
+ const text = typeof term.getText === 'function' ? term.getText() : String(term);
280
+ termsHtml.push(text);
281
+ }
282
+ }
283
+ let descriptionHtml = '';
284
+ let descriptionIcons = [];
285
+ let children = [];
286
+ if (definition && typeof definition.getText === 'function') {
287
+ const rawHtml = definition.getText() || '';
288
+ const extracted = extractInlineIcons(rawHtml);
289
+ descriptionHtml = extracted.html;
290
+ descriptionIcons = extracted.icons;
291
+ if (typeof definition.hasBlocks === 'function' && definition.hasBlocks()) {
292
+ children = convertBlocks(definition.getBlocks(), relativePath);
293
+ }
294
+ }
295
+ items.push({
296
+ termsHtml,
297
+ descriptionHtml,
298
+ descriptionIcons,
299
+ children,
300
+ });
301
+ }
302
+ return {
303
+ type: 'descriptionList',
304
+ items,
305
+ };
306
+ }
307
+ function convertTable(block) {
308
+ const rows = block.getRows();
309
+ const title = block.getTitle() || undefined;
310
+ const convertRow = (row) => row.map((cell) => {
311
+ var _a;
312
+ const colSpan = cell.getColumnSpan();
313
+ const rowSpan = cell.getRowSpan();
314
+ // getContent() returns rendered HTML for asciidoc cells; getText() for plain text cells
315
+ const style = (_a = cell.getStyle) === null || _a === void 0 ? void 0 : _a.call(cell);
316
+ const contentHtml = (style === 'asciidoc' ? cell.getContent() : cell.getText()) || '';
317
+ return Object.assign(Object.assign({ contentHtml }, (colSpan > 1 ? { colSpan } : {})), (rowSpan > 1 ? { rowSpan } : {}));
318
+ });
319
+ return {
320
+ type: 'table',
321
+ title,
322
+ head: (rows.head || []).map(convertRow),
323
+ body: (rows.body || []).map(convertRow),
324
+ foot: (rows.foot || []).map(convertRow),
325
+ };
326
+ }
327
+ function convertImage(block, relativePath) {
328
+ const target = block.getAttribute('target') || '';
329
+ const alt = block.getAttribute('alt') || '';
330
+ const width = block.getAttribute('width');
331
+ const height = block.getAttribute('height');
332
+ const title = block.getTitle() || undefined;
333
+ const src = normalizeImageSrc(target, relativePath);
334
+ return Object.assign(Object.assign({ type: 'image', src,
335
+ alt,
336
+ title }, (width ? { width: String(width) } : {})), (height ? { height: String(height) } : {}));
337
+ }
338
+ function convertQuote(block, relativePath) {
339
+ const attribution = block.getAttribute('attribution') || undefined;
340
+ const citeTitle = block.getAttribute('citetitle') || undefined;
341
+ const innerBlocks = block.getBlocks();
342
+ const children = innerBlocks.length > 0
343
+ ? convertBlocks(innerBlocks, relativePath)
344
+ : block.getContent()
345
+ ? [
346
+ {
347
+ type: 'paragraph',
348
+ contentHtml: block.getContent() || '',
349
+ icons: [],
350
+ },
351
+ ]
352
+ : [];
353
+ return {
354
+ type: 'quote',
355
+ attribution,
356
+ citeTitle,
357
+ children,
358
+ };
359
+ }
360
+ function convertSidebar(block, relativePath) {
361
+ return {
362
+ type: 'sidebar',
363
+ title: block.getTitle() || undefined,
364
+ children: convertBlocks(block.getBlocks(), relativePath),
365
+ };
366
+ }
367
+ function convertExample(block, relativePath) {
368
+ const isCollapsible = block.isOption('collapsible');
369
+ if (isCollapsible) {
370
+ return {
371
+ type: 'collapsible',
372
+ title: block.getTitle() || undefined,
373
+ children: convertBlocks(block.getBlocks(), relativePath),
374
+ };
375
+ }
376
+ return {
377
+ type: 'example',
378
+ title: block.getTitle() || undefined,
379
+ children: convertBlocks(block.getBlocks(), relativePath),
380
+ };
381
+ }
382
+ function convertOpen(block, relativePath) {
383
+ const style = (block.getStyle() || '').toLowerCase();
384
+ if (isDiagramType(style)) {
385
+ const source = extractSourceFromBlocks(block.getBlocks()).trim();
386
+ if (source) {
387
+ return {
388
+ type: 'diagram',
389
+ diagramType: style,
390
+ source,
391
+ id: block.getId() || undefined,
392
+ };
393
+ }
394
+ }
395
+ // An open block is a generic container — just recurse into children
396
+ const children = convertBlocks(block.getBlocks(), relativePath);
397
+ if (children.length === 1)
398
+ return children[0];
399
+ // Return as an example-like container if it has a role
400
+ const role = block.getRole();
401
+ if (role) {
402
+ return {
403
+ type: 'paragraph',
404
+ contentHtml: block.getContent() || '',
405
+ icons: [],
406
+ role,
407
+ };
408
+ }
409
+ // Just flatten open blocks
410
+ return {
411
+ type: 'example',
412
+ title: undefined,
413
+ children,
414
+ };
415
+ }
416
+ function extractSourceFromBlocks(blocks) {
417
+ var _a, _b;
418
+ const parts = [];
419
+ for (const block of blocks) {
420
+ const source = (_a = block.getSource) === null || _a === void 0 ? void 0 : _a.call(block);
421
+ if (source) {
422
+ parts.push(source);
423
+ continue;
424
+ }
425
+ const childBlocks = ((_b = block.getBlocks) === null || _b === void 0 ? void 0 : _b.call(block)) || [];
426
+ if (childBlocks.length > 0) {
427
+ const childSource = extractSourceFromBlocks(childBlocks);
428
+ if (childSource) {
429
+ parts.push(childSource);
430
+ }
431
+ }
432
+ }
433
+ return parts.join('\n\n');
434
+ }
435
+ function convertPassthrough(block) {
436
+ return {
437
+ type: 'htmlPassthrough',
438
+ html: block.getContent() || '',
439
+ };
440
+ }
441
+ /**
442
+ * Parses raw AsciiDoc source callout markers like `<1>` `<2>` from code.
443
+ * Returns cleaned source and callout positions.
444
+ */
445
+ function parseCallouts(source) {
446
+ const callouts = [];
447
+ const calloutRegex = /(?<!\s)\s*<(\d+)>\s*$/;
448
+ const lines = source.split('\n');
449
+ const cleanedLines = [];
450
+ for (let i = 0; i < lines.length; i++) {
451
+ let line = lines[i];
452
+ const match = line.match(calloutRegex);
453
+ if (match) {
454
+ callouts.push({ line: i + 1, number: parseInt(match[1], 10) });
455
+ line = line.replace(calloutRegex, '');
456
+ }
457
+ cleanedLines.push(line);
458
+ }
459
+ return { cleaned: cleanedLines.join('\n'), callouts };
460
+ }
461
+ /**
462
+ * Extracts callout descriptions from a colist (callout list) block.
463
+ */
464
+ function extractColistDescriptions(colist) {
465
+ const descriptions = [];
466
+ try {
467
+ const items = colist.getItems();
468
+ if (items && Array.isArray(items)) {
469
+ for (const item of items) {
470
+ if (typeof item.getText === 'function') {
471
+ descriptions.push(stripInlineHtml(item.getText()));
472
+ }
473
+ }
474
+ }
475
+ }
476
+ catch (_a) {
477
+ // Colist may not support getItems on all versions
478
+ }
479
+ return descriptions;
480
+ }
481
+ /**
482
+ * Known Font Awesome brand icon names that live under the `brands/` style.
483
+ */
484
+ const FA_BRAND_ICONS = new Set([
485
+ 'github',
486
+ 'twitter',
487
+ 'facebook',
488
+ 'linkedin',
489
+ 'youtube',
490
+ 'google',
491
+ 'apple',
492
+ 'android',
493
+ 'windows',
494
+ 'docker',
495
+ 'npm',
496
+ 'node-js',
497
+ 'node',
498
+ 'react',
499
+ 'angular',
500
+ 'vuejs',
501
+ 'python',
502
+ 'java',
503
+ 'php',
504
+ 'rust',
505
+ 'golang',
506
+ 'aws',
507
+ 'slack',
508
+ 'discord',
509
+ 'reddit',
510
+ 'stackoverflow',
511
+ 'stack-overflow',
512
+ 'gitlab',
513
+ 'bitbucket',
514
+ 'codepen',
515
+ 'jsfiddle',
516
+ 'bootstrap',
517
+ 'sass',
518
+ 'less',
519
+ 'css3',
520
+ 'html5',
521
+ 'js',
522
+ 'markdown',
523
+ 'stripe',
524
+ 'paypal',
525
+ 'shopify',
526
+ 'wordpress',
527
+ 'medium',
528
+ 'dev',
529
+ 'figma',
530
+ 'sketch',
531
+ 'dribbble',
532
+ 'behance',
533
+ 'instagram',
534
+ 'pinterest',
535
+ 'tiktok',
536
+ 'whatsapp',
537
+ 'telegram',
538
+ 'x-twitter',
539
+ ]);
540
+ /**
541
+ * Extracts `<i class="fa fa-*">` icons from inline HTML and replaces them
542
+ * with `<!--icon:N-->` placeholders. The extracted icon data is stored in
543
+ * the InlineIcon array so the template can render CDNIcon components.
544
+ */
545
+ function extractInlineIcons(html) {
546
+ const icons = [];
547
+ const processed = html.replace(/<i class="fa\s+fa-([a-z0-9-]+)((?:[^"a-z0-9-][^"]*)?)"[^>]*><\/i>/gi, (_match, name, extraClasses) => {
548
+ let iconName = name;
549
+ let style = 'solid';
550
+ if (iconName.endsWith('-o')) {
551
+ iconName = iconName.slice(0, -2);
552
+ style = 'regular';
553
+ }
554
+ if (FA_BRAND_ICONS.has(iconName)) {
555
+ style = 'brands';
556
+ }
557
+ const size = extraClasses.includes('fa-2x') ? '2em' : undefined;
558
+ const idx = icons.length;
559
+ icons.push({ name: iconName, style, size });
560
+ return `<!--icon:${idx}-->`;
561
+ });
562
+ return { html: processed, icons };
563
+ }
564
+ function buildSectionTree(sections) {
565
+ return sections.map((section) => {
566
+ const id = section.getId() || slugify(section.getTitle() || '');
567
+ const title = section.getTitle() || '';
568
+ const level = section.getLevel();
569
+ // Extract plain text from the section's non-section blocks
570
+ const textContent = extractTextFromBlocks(section.getBlocks());
571
+ // Recurse into child sections
572
+ const childSections = section
573
+ .getBlocks()
574
+ .filter((b) => b.getContext() === 'section');
575
+ return {
576
+ id,
577
+ title,
578
+ level,
579
+ textContent,
580
+ children: buildSectionTree(childSections),
581
+ };
582
+ });
583
+ }
584
+ function extractTextFromBlocks(blocks) {
585
+ const parts = [];
586
+ for (const block of blocks) {
587
+ const context = block.getContext();
588
+ if (context === 'section')
589
+ continue;
590
+ if (context === 'paragraph' || context === 'verse') {
591
+ const html = block.getContent();
592
+ if (html)
593
+ parts.push(stripInlineHtml(html));
594
+ }
595
+ else if (context === 'literal') {
596
+ const source = block.getSource();
597
+ if (source)
598
+ parts.push(normalizeUnicode(source));
599
+ }
600
+ else if (context === 'listing') {
601
+ const lang = block.getAttribute('language') || '';
602
+ const source = block.getSource();
603
+ if (source)
604
+ parts.push('```' + lang + '\n' + normalizeUnicode(source) + '\n```');
605
+ }
606
+ else if (context === 'open') {
607
+ const text = extractOpenBlockText(block);
608
+ if (text)
609
+ parts.push(text);
610
+ }
611
+ else if (context === 'ulist' || context === 'olist') {
612
+ const text = extractListText(block, context === 'olist');
613
+ if (text)
614
+ parts.push(text);
615
+ }
616
+ else if (context === 'dlist') {
617
+ const text = extractDlistText(block);
618
+ if (text)
619
+ parts.push(text);
620
+ }
621
+ else if (context === 'table') {
622
+ try {
623
+ parts.push(tableToMarkdown(block));
624
+ }
625
+ catch (_a) {
626
+ /* skip */
627
+ }
628
+ }
629
+ else if (context === 'admonition') {
630
+ const text = extractAdmonitionText(block);
631
+ if (text)
632
+ parts.push(text);
633
+ }
634
+ else if (context === 'quote' ||
635
+ context === 'example' ||
636
+ context === 'sidebar' ||
637
+ context === 'preamble') {
638
+ const text = extractContainerText(block);
639
+ if (text)
640
+ parts.push(text);
641
+ }
642
+ }
643
+ return parts.filter(Boolean).join('\n\n');
644
+ }
645
+ function extractOpenBlockText(block) {
646
+ const style = (block.getStyle() || '').toLowerCase();
647
+ if (isDiagramType(style)) {
648
+ const source = extractSourceFromBlocks(block.getBlocks());
649
+ return source ? '```' + style + '\n' + normalizeUnicode(source) + '\n```' : '';
650
+ }
651
+ // Not a diagram — fall through to generic nested-block extraction, same as
652
+ // quote/example/sidebar/preamble. (#25965: this used to be unreachable because
653
+ // 'open' was matched earlier in the if/else-if chain by the diagram-only branch.)
654
+ return extractContainerText(block);
655
+ }
656
+ function extractListText(block, isOrdered) {
657
+ try {
658
+ const items = block.getItems();
659
+ if (!items || !Array.isArray(items))
660
+ return '';
661
+ const lines = [];
662
+ for (let idx = 0; idx < items.length; idx++) {
663
+ const item = items[idx];
664
+ if (typeof item.getText === 'function') {
665
+ const prefix = isOrdered ? `${idx + 1}. ` : '- ';
666
+ lines.push(prefix + stripInlineHtml(item.getText()));
667
+ }
668
+ }
669
+ return lines.join('\n');
670
+ }
671
+ catch (_a) {
672
+ return '';
673
+ }
674
+ }
675
+ function extractDlistText(block) {
676
+ try {
677
+ const items = block.getItems();
678
+ if (!items || !Array.isArray(items))
679
+ return '';
680
+ const lines = [];
681
+ for (const pair of items) {
682
+ if (!Array.isArray(pair))
683
+ continue;
684
+ const [terms, definition] = pair;
685
+ if (Array.isArray(terms)) {
686
+ for (const t of terms) {
687
+ if (typeof t.getText === 'function') {
688
+ lines.push('**' + stripInlineHtml(t.getText()) + '**');
689
+ }
690
+ }
691
+ }
692
+ if (definition && typeof definition.getText === 'function') {
693
+ lines.push(': ' + stripInlineHtml(definition.getText()));
694
+ }
695
+ }
696
+ return lines.join('\n');
697
+ }
698
+ catch (_a) {
699
+ return '';
700
+ }
701
+ }
702
+ function extractAdmonitionText(block) {
703
+ var _a;
704
+ const style = (((_a = block.getStyle) === null || _a === void 0 ? void 0 : _a.call(block)) || 'note').toUpperCase();
705
+ const innerBlocks = block.getBlocks();
706
+ const inner = (innerBlocks === null || innerBlocks === void 0 ? void 0 : innerBlocks.length)
707
+ ? extractTextFromBlocks(innerBlocks)
708
+ : stripInlineHtml(block.getContent() || '');
709
+ return inner ? `> [!${style}]\n> ${inner.replace(/\n/g, '\n> ')}` : '';
710
+ }
711
+ function extractContainerText(block) {
712
+ const innerBlocks = block.getBlocks();
713
+ if (innerBlocks && innerBlocks.length > 0) {
714
+ return extractTextFromBlocks(innerBlocks);
715
+ }
716
+ const html = block.getContent();
717
+ return html ? stripInlineHtml(html) : '';
718
+ }
719
+ function tableToMarkdown(block) {
720
+ const rows = block.getRows();
721
+ const headRows = [];
722
+ const bodyRows = [];
723
+ const footRows = [];
724
+ const extractRow = (row) => row.map((cell) => { var _a; return stripInlineHtml(((_a = cell.getText) === null || _a === void 0 ? void 0 : _a.call(cell)) || '').replace(/\|/g, '\\|'); });
725
+ for (const row of rows.head || []) {
726
+ if (Array.isArray(row))
727
+ headRows.push(extractRow(row));
728
+ }
729
+ for (const row of rows.body || []) {
730
+ if (Array.isArray(row))
731
+ bodyRows.push(extractRow(row));
732
+ }
733
+ for (const row of rows.foot || []) {
734
+ if (Array.isArray(row))
735
+ footRows.push(extractRow(row));
736
+ }
737
+ const allRows = [...headRows, ...bodyRows, ...footRows];
738
+ if (allRows.length === 0)
739
+ return '';
740
+ const colCount = Math.max(...allRows.map((r) => r.length));
741
+ const pad = (row) => {
742
+ while (row.length < colCount)
743
+ row.push('');
744
+ return row;
745
+ };
746
+ const lines = [];
747
+ if (headRows.length > 0) {
748
+ for (const row of headRows) {
749
+ lines.push('| ' + pad(row).join(' | ') + ' |');
750
+ }
751
+ }
752
+ else {
753
+ // Use first body row as header if no explicit header
754
+ const firstRow = bodyRows.shift();
755
+ if (firstRow) {
756
+ lines.push('| ' + pad(firstRow).join(' | ') + ' |');
757
+ }
758
+ }
759
+ // Separator row
760
+ lines.push('| ' + Array(colCount).fill('---').join(' | ') + ' |');
761
+ for (const row of bodyRows) {
762
+ lines.push('| ' + pad(row).join(' | ') + ' |');
763
+ }
764
+ for (const row of footRows) {
765
+ lines.push('| ' + pad(row).join(' | ') + ' |');
766
+ }
767
+ return lines.join('\n');
768
+ }
769
+ function extractLinks(html, _relativePath) {
770
+ const links = [];
771
+ const linkRegex = /<a\s[^>]*href="([^"]*)"[^>]*>([^<]*)<\/a>/gi;
772
+ let match;
773
+ while ((match = linkRegex.exec(html)) !== null) {
774
+ const href = match[1];
775
+ const text = match[2];
776
+ let type = 'link';
777
+ if (href.startsWith('#')) {
778
+ type = 'xref';
779
+ }
780
+ else if (href.startsWith('http://') || href.startsWith('https://')) {
781
+ type = 'url';
782
+ }
783
+ links.push({ href, text, type });
784
+ }
785
+ return links;
786
+ }
787
+ function normalizeImageSrc(src, relativePath) {
788
+ if (!src)
789
+ return src;
790
+ if (src.startsWith('/') ||
791
+ src.startsWith('//') ||
792
+ src.startsWith('data:') ||
793
+ /^[a-z][a-z\d+\-.]*:/i.test(src)) {
794
+ return src;
795
+ }
796
+ const baseDir = path.posix.dirname(relativePath);
797
+ const resolvedPath = path.posix.normalize(path.posix.join(baseDir, src));
798
+ return `/${resolvedPath.replace(/^\/+/, '')}`;
799
+ }
800
+ function stripInlineHtml(text) {
801
+ return decodeHtmlEntities(text
802
+ .replace(/<span class="icon"><i class="fa[^"]*fa-([^"\s]+)"[^>]*><\/i><\/span>/gi, (_m, name) => `:${name}:`)
803
+ .replace(/<i class="fa[^"]*fa-([^"\s]+)"[^>]*><\/i>/gi, (_m, name) => `:${name}:`)
804
+ .replace(/<(?<!<[^>]*?<)[^>]+>/g, ''));
805
+ }
806
+ function decodeHtmlEntities(text) {
807
+ return normalizeUnicode(text
808
+ .replace(/&#(\d+);/g, (_m, code) => String.fromCodePoint(Number(code)))
809
+ .replace(/&#x([0-9a-f]+);/gi, (_m, hex) => String.fromCodePoint(parseInt(hex, 16)))
810
+ .replace(/&amp;/g, '&')
811
+ .replace(/&lt;/g, '<')
812
+ .replace(/&gt;/g, '>')
813
+ .replace(/&quot;/g, '"')
814
+ .replace(/&apos;/g, "'"));
815
+ }
816
+ function normalizeUnicode(text) {
817
+ return text
818
+ .replace(/\u2014/g, '--')
819
+ .replace(/\u2013/g, '-')
820
+ .replace(/\u2018|\u2019/g, "'")
821
+ .replace(/\u201C|\u201D/g, '"')
822
+ .replace(/\u2026/g, '...')
823
+ .replace(/\u00A0/g, ' ');
824
+ }
825
+ function slugify(text) {
826
+ return text
827
+ .toLowerCase()
828
+ .replace(/[^\w\s-]/g, '')
829
+ .replace(/\s+/g, '-')
830
+ .replace(/-+/g, '-')
831
+ .replace(/^-|-$/g, '');
832
+ }
833
+ //# sourceMappingURL=asciidoc-parser.js.map