@redocly/realm-plugin-asciidoc 0.8.0-next.9 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/template.tsx DELETED
@@ -1,687 +0,0 @@
1
- import React, { createContext, useCallback, useContext, useMemo, useState } from 'react';
2
- import styled from 'styled-components';
3
- import { Admonition, CodeBlock, Markdown, useThemeHooks } from '@redocly/theme';
4
-
5
- import type {
6
- AsciidocFrontmatter,
7
- AsciidocSection,
8
- CodeBlockNode,
9
- ContentNode,
10
- InlineIcon,
11
- ListItemNode,
12
- DescriptionListItemNode,
13
- TableCellNode,
14
- } from './types.js';
15
-
16
- import { DocumentationLayout } from '@redocly/theme/layouts/DocumentationLayout';
17
- import { TableOfContent } from '@redocly/theme/components/TableOfContent/TableOfContent';
18
- import { Heading } from '@redocly/theme/markdoc/components/Heading/Heading';
19
- import { Diagram } from '@redocly/theme/markdoc/components/Diagram/Diagram';
20
- import { CDNIcon } from '@redocly/theme/icons/CDNIcon/CDNIcon';
21
-
22
- type PageProps = {
23
- title: string;
24
- contentNodes: ContentNode[];
25
- sections: AsciidocSection[];
26
- links: Array<{ href: string; text: string; type: string }>;
27
- frontmatter: AsciidocFrontmatter;
28
- };
29
-
30
- type AsciidocDocsProps = {
31
- pageProps: PageProps;
32
- };
33
-
34
- function buildTocHeadings(
35
- sections: AsciidocSection[],
36
- ): Array<{ id: string; value: string; depth: number }> {
37
- const items: Array<{ id: string; value: string; depth: number }> = [];
38
- for (const section of sections) {
39
- items.push({ id: section.id, value: section.title, depth: section.level });
40
- if (section.children.length > 0) {
41
- items.push(...buildTocHeadings(section.children));
42
- }
43
- }
44
- return items;
45
- }
46
-
47
- /**
48
- * Renders an HTML string that may contain `<!--icon:N-->` placeholders,
49
- * splitting the HTML and interleaving CDNIcon React components.
50
- */
51
- function renderHtmlWithIcons(
52
- html: string,
53
- icons: InlineIcon[],
54
- keyPrefix: string,
55
- ): React.ReactNode {
56
- if (!icons.length || !html.includes('<!--icon:')) {
57
- return <span key={keyPrefix} dangerouslySetInnerHTML={{ __html: html }} />;
58
- }
59
-
60
- const parts = html.split(/(<!--icon:\d+-->)/);
61
- const nodes: React.ReactNode[] = [];
62
-
63
- for (let j = 0; j < parts.length; j++) {
64
- const fragment = parts[j];
65
- const iconMatch = fragment.match(/<!--icon:(\d+)-->/);
66
- if (iconMatch) {
67
- const idx = parseInt(iconMatch[1], 10);
68
- const icon = icons[idx];
69
- if (icon) {
70
- nodes.push(
71
- <CDNIcon
72
- key={`${keyPrefix}-icon-${idx}`}
73
- name={icon.name}
74
- type={icon.style}
75
- size={icon.size}
76
- />,
77
- );
78
- }
79
- } else if (fragment) {
80
- nodes.push(
81
- <span key={`${keyPrefix}-f-${j}`} dangerouslySetInnerHTML={{ __html: fragment }} />,
82
- );
83
- }
84
- }
85
-
86
- return <>{nodes}</>;
87
- }
88
-
89
- function injectCalloutBadges(html: string, callouts: { line: number; number: number }[]): string {
90
- if (!callouts.length) return html;
91
-
92
- const calloutByLine = new Map(callouts.map((c) => [c.line, c.number]));
93
- let lineNumber = 0;
94
-
95
- return html.replace(/<span class="line">(.*?)<\/span>/g, (match, inner) => {
96
- lineNumber++;
97
- const calloutNumber = calloutByLine.get(lineNumber);
98
- if (calloutNumber != null) {
99
- return `<span class="line">${inner}<span class="code-callout-badge" data-callout="${calloutNumber}">${calloutNumber}</span></span>`;
100
- }
101
- return match;
102
- });
103
- }
104
-
105
- function AsciidocCodeBlock({ node }: { node: CodeBlockNode }): React.ReactElement {
106
- const { useCodeHighlight } = useThemeHooks();
107
- const { highlight } = useCodeHighlight() || {};
108
-
109
- const highlightLines = node.callouts?.map((c) => String(c.line)).join(',');
110
-
111
- const highlightedHtml = useMemo(() => {
112
- if (!highlight) return undefined;
113
-
114
- let html = highlight(node.source, node.lang, {
115
- highlight: highlightLines,
116
- });
117
-
118
- if (html && node.callouts?.length) {
119
- html = injectCalloutBadges(html, node.callouts);
120
- }
121
-
122
- return html || undefined;
123
- }, [highlight, node.source, node.lang, highlightLines, node.callouts]);
124
-
125
- // Asciidoctor returns HTML-converted titles (e.g. `Sample <code>/path</code>`),
126
- // so render via `dangerouslySetInnerHTML` rather than as a plain string.
127
- const titleNode = node.title ? (
128
- <span dangerouslySetInnerHTML={{ __html: node.title }} />
129
- ) : node.lang ? (
130
- node.lang
131
- ) : undefined;
132
-
133
- return (
134
- <CodeBlock
135
- lang={node.lang}
136
- source={node.source}
137
- highlightedHtml={highlightedHtml}
138
- header={
139
- titleNode
140
- ? {
141
- title: titleNode,
142
- controls: { copy: {} },
143
- }
144
- : { controls: { copy: {} } }
145
- }
146
- />
147
- );
148
- }
149
-
150
- const FirstHeadingContext = createContext<ContentNode | null>(null);
151
-
152
- function childNodes(node: ContentNode): ContentNode[] {
153
- if ('children' in node) return node.children;
154
- if (node.type === 'list' || node.type === 'descriptionList') {
155
- return node.items.flatMap((item) => item.children);
156
- }
157
- return [];
158
- }
159
-
160
- function findFirstHeading(nodes: ContentNode[]): ContentNode | null {
161
- for (const node of nodes) {
162
- if (node.type === 'section' || node.type === 'heading') return node;
163
- const nested = findFirstHeading(childNodes(node));
164
- if (nested) return nested;
165
- }
166
- return null;
167
- }
168
-
169
- function RenderNode({
170
- node,
171
- index,
172
- }: {
173
- node: ContentNode;
174
- index: number;
175
- }): React.ReactElement | null {
176
- const key = `node-${index}`;
177
- const firstHeading = useContext(FirstHeadingContext);
178
- const __idx = node === firstHeading ? 0 : undefined;
179
-
180
- switch (node.type) {
181
- case 'section': {
182
- return (
183
- <React.Fragment key={key}>
184
- <Heading level={node.level} id={node.id} __idx={__idx}>
185
- {renderHtmlWithIcons(node.titleHtml, node.titleIcons, `${key}-title`)}
186
- </Heading>
187
- <RenderNodes nodes={node.children} keyPrefix={key} />
188
- </React.Fragment>
189
- );
190
- }
191
-
192
- case 'heading': {
193
- return (
194
- <Heading key={key} level={node.level} id={node.id} __idx={__idx}>
195
- <span dangerouslySetInnerHTML={{ __html: node.contentHtml }} />
196
- </Heading>
197
- );
198
- }
199
-
200
- case 'paragraph': {
201
- const Tag = node.role ? 'div' : 'p';
202
- const className = node.role || undefined;
203
- return (
204
- <Tag key={key} className={className}>
205
- {renderHtmlWithIcons(node.contentHtml, node.icons, key)}
206
- </Tag>
207
- );
208
- }
209
-
210
- case 'codeBlock': {
211
- return (
212
- <React.Fragment key={key}>
213
- <AsciidocCodeBlock node={node} />
214
- {node.calloutDescriptions && node.calloutDescriptions.length > 0 && (
215
- <ol className="callout-list">
216
- {node.calloutDescriptions.map((desc, i) => (
217
- <li key={`${key}-co-${i}`}>
218
- <span className="callout-badge">{i + 1}</span>
219
- <span dangerouslySetInnerHTML={{ __html: desc }} />
220
- </li>
221
- ))}
222
- </ol>
223
- )}
224
- </React.Fragment>
225
- );
226
- }
227
-
228
- case 'admonition':
229
- return (
230
- <Admonition key={key} type={node.admonitionType} name={node.name}>
231
- <RenderNodes nodes={node.children} keyPrefix={key} />
232
- </Admonition>
233
- );
234
-
235
- case 'list': {
236
- const ListTag = node.ordered ? 'ol' : 'ul';
237
- return (
238
- <ListTag key={key} className="md">
239
- {node.items.map((item, i) => (
240
- <RenderListItem key={`${key}-li-${i}`} item={item} keyPrefix={`${key}-li-${i}`} />
241
- ))}
242
- </ListTag>
243
- );
244
- }
245
-
246
- case 'descriptionList':
247
- return (
248
- <dl key={key} className="dlist">
249
- {node.items.map((item, i) => (
250
- <RenderDlistItem key={`${key}-dl-${i}`} item={item} keyPrefix={`${key}-dl-${i}`} />
251
- ))}
252
- </dl>
253
- );
254
-
255
- case 'table':
256
- return (
257
- <div key={key} className="md-table-wrapper">
258
- <table className="md">
259
- {node.title && <caption dangerouslySetInnerHTML={{ __html: node.title }} />}
260
- {node.head.length > 0 && (
261
- <thead>
262
- {node.head.map((row, ri) => (
263
- <tr key={`${key}-th-${ri}`}>
264
- {row.map((cell, ci) => (
265
- <RenderTableCell key={`${key}-th-${ri}-${ci}`} cell={cell} tag="th" />
266
- ))}
267
- </tr>
268
- ))}
269
- </thead>
270
- )}
271
- <tbody>
272
- {node.body.map((row, ri) => (
273
- <tr key={`${key}-tb-${ri}`}>
274
- {row.map((cell, ci) => (
275
- <RenderTableCell key={`${key}-tb-${ri}-${ci}`} cell={cell} tag="td" />
276
- ))}
277
- </tr>
278
- ))}
279
- </tbody>
280
- {node.foot.length > 0 && (
281
- <tfoot>
282
- {node.foot.map((row, ri) => (
283
- <tr key={`${key}-tf-${ri}`}>
284
- {row.map((cell, ci) => (
285
- <RenderTableCell key={`${key}-tf-${ri}-${ci}`} cell={cell} tag="td" />
286
- ))}
287
- </tr>
288
- ))}
289
- </tfoot>
290
- )}
291
- </table>
292
- </div>
293
- );
294
-
295
- case 'image':
296
- return (
297
- <div key={key} className="imageblock">
298
- <img
299
- src={node.src}
300
- alt={node.alt}
301
- {...(node.width ? { width: node.width } : {})}
302
- {...(node.height ? { height: node.height } : {})}
303
- />
304
- {node.title && <div className="title" dangerouslySetInnerHTML={{ __html: node.title }} />}
305
- </div>
306
- );
307
-
308
- case 'quote':
309
- return (
310
- <div key={key} className="quoteblock">
311
- <blockquote>
312
- <RenderNodes nodes={node.children} keyPrefix={key} />
313
- </blockquote>
314
- {node.attribution && (
315
- <div className="attribution">
316
- — {node.attribution}
317
- {node.citeTitle && (
318
- <>
319
- , <cite>{node.citeTitle}</cite>
320
- </>
321
- )}
322
- </div>
323
- )}
324
- </div>
325
- );
326
-
327
- case 'sidebar':
328
- return (
329
- <div key={key} className="sidebarblock">
330
- <div className="content">
331
- {node.title && (
332
- <div className="title" dangerouslySetInnerHTML={{ __html: node.title }} />
333
- )}
334
- <RenderNodes nodes={node.children} keyPrefix={key} />
335
- </div>
336
- </div>
337
- );
338
-
339
- case 'example':
340
- return (
341
- <div key={key} className="exampleblock">
342
- {node.title && <div className="title" dangerouslySetInnerHTML={{ __html: node.title }} />}
343
- <div className="content">
344
- <RenderNodes nodes={node.children} keyPrefix={key} />
345
- </div>
346
- </div>
347
- );
348
-
349
- case 'collapsible':
350
- return (
351
- <details key={key}>
352
- {node.title && <summary dangerouslySetInnerHTML={{ __html: node.title }} />}
353
- <RenderNodes nodes={node.children} keyPrefix={key} />
354
- </details>
355
- );
356
-
357
- case 'thematicBreak':
358
- return <hr key={key} />;
359
-
360
- case 'htmlPassthrough':
361
- return <div key={key} dangerouslySetInnerHTML={{ __html: node.html }} />;
362
-
363
- case 'diagram': {
364
- if (!node.diagramHtml && !node.diagramError) {
365
- return (
366
- <div key={key} className="diagram-error">
367
- <p>Diagram rendering unavailable.</p>
368
- </div>
369
- );
370
- }
371
- return (
372
- <div key={key} id={node.id}>
373
- <Diagram
374
- diagramType={node.diagramType}
375
- diagramHtml={node.diagramHtml || ''}
376
- diagramHtmlDark={node.diagramHtmlDark}
377
- diagramError={node.diagramError}
378
- />
379
- </div>
380
- );
381
- }
382
-
383
- default:
384
- return null;
385
- }
386
- }
387
-
388
- function RenderNodes({
389
- nodes,
390
- keyPrefix,
391
- }: {
392
- nodes: ContentNode[];
393
- keyPrefix: string;
394
- }): React.ReactElement {
395
- return (
396
- <>
397
- {nodes.map((node, i) => (
398
- <RenderNode key={`${keyPrefix}-${i}`} node={node} index={i} />
399
- ))}
400
- </>
401
- );
402
- }
403
-
404
- function RenderListItem({
405
- item,
406
- keyPrefix,
407
- }: {
408
- item: ListItemNode;
409
- keyPrefix: string;
410
- }): React.ReactElement {
411
- return (
412
- <li>
413
- {renderHtmlWithIcons(item.contentHtml, item.icons, keyPrefix)}
414
- {item.children.length > 0 && <RenderNodes nodes={item.children} keyPrefix={keyPrefix} />}
415
- </li>
416
- );
417
- }
418
-
419
- function RenderDlistItem({
420
- item,
421
- keyPrefix,
422
- }: {
423
- item: DescriptionListItemNode;
424
- keyPrefix: string;
425
- }): React.ReactElement {
426
- return (
427
- <>
428
- {item.termsHtml.map((termHtml, ti) => (
429
- <dt key={`${keyPrefix}-dt-${ti}`} dangerouslySetInnerHTML={{ __html: termHtml }} />
430
- ))}
431
- <dd>
432
- {renderHtmlWithIcons(item.descriptionHtml, item.descriptionIcons, `${keyPrefix}-dd`)}
433
- {item.children.length > 0 && (
434
- <RenderNodes nodes={item.children} keyPrefix={`${keyPrefix}-dd`} />
435
- )}
436
- </dd>
437
- </>
438
- );
439
- }
440
-
441
- function RenderTableCell({
442
- cell,
443
- tag: Tag,
444
- }: {
445
- cell: TableCellNode;
446
- tag: 'th' | 'td';
447
- }): React.ReactElement {
448
- return (
449
- <Tag
450
- {...(cell.colSpan ? { colSpan: cell.colSpan } : {})}
451
- {...(cell.rowSpan ? { rowSpan: cell.rowSpan } : {})}
452
- dangerouslySetInnerHTML={{ __html: cell.contentHtml }}
453
- />
454
- );
455
- }
456
-
457
- const Template: React.FC<AsciidocDocsProps> = ({ pageProps }) => {
458
- const { useSidebarSiblingsData } = useThemeHooks();
459
- const { nextPage, prevPage } = useSidebarSiblingsData() || {};
460
- const [wrapperElement, setWrapperElement] = useState<HTMLDivElement | null>(null);
461
-
462
- const wrapperRefCb = useCallback((node: HTMLDivElement) => {
463
- if (!node) return;
464
- setWrapperElement(node);
465
- }, []);
466
-
467
- const tocHeadings = useMemo(() => {
468
- const items = buildTocHeadings(pageProps.sections);
469
- return items.filter((item) => Boolean(item.id) && Boolean(item.value));
470
- }, [pageProps.sections]);
471
-
472
- const tableOfContent = <TableOfContent headings={tocHeadings} contentWrapper={wrapperElement} />;
473
-
474
- const firstHeading = useMemo(
475
- () => findFirstHeading(pageProps.contentNodes),
476
- [pageProps.contentNodes],
477
- );
478
-
479
- return (
480
- <DocumentationLayout
481
- tableOfContent={tableOfContent}
482
- feedback={undefined}
483
- nextPage={nextPage}
484
- prevPage={prevPage}
485
- >
486
- <AsciidocWrapper ref={wrapperRefCb} as="div" className="asciidoc-content">
487
- <FirstHeadingContext.Provider value={firstHeading}>
488
- <RenderNodes nodes={pageProps.contentNodes} keyPrefix="root" />
489
- </FirstHeadingContext.Provider>
490
- </AsciidocWrapper>
491
- </DocumentationLayout>
492
- );
493
- };
494
-
495
- export default Template;
496
-
497
- const AsciidocWrapper = styled(Markdown)`
498
- /* === Fix list item <p> margins === */
499
- li > p:only-child {
500
- margin: 0;
501
- }
502
-
503
- li > .paragraph:first-child > p:first-child {
504
- margin-top: 0;
505
- }
506
-
507
- li > .paragraph:last-child > p:last-child {
508
- margin-bottom: 0;
509
- }
510
-
511
- li > p:first-child {
512
- margin-top: 0;
513
- }
514
-
515
- li > p:last-child {
516
- margin-bottom: 0;
517
- }
518
-
519
- /* === AsciiDoc description lists === */
520
- .dlist dt,
521
- dl.dlist dt {
522
- font-weight: var(--font-weight-semibold, 600);
523
- margin-top: var(--spacing-xs, 0.5em);
524
- }
525
-
526
- .dlist dd,
527
- dl.dlist dd {
528
- margin-left: var(--md-list-left-padding, 1.25rem);
529
- margin-bottom: calc(var(--spacing-xxs, 0.25em) / 2);
530
- }
531
-
532
- /* === AsciiDoc images === */
533
- .imageblock {
534
- margin: var(--spacing-base, 1em) 0;
535
- text-align: center;
536
- }
537
-
538
- .imageblock img {
539
- max-width: 100%;
540
- height: auto;
541
- }
542
-
543
- .imageblock .title {
544
- font-style: italic;
545
- margin-top: var(--spacing-xxs, 0.25em);
546
- color: var(--text-color-secondary, #666);
547
- font-size: var(--font-size-sm, 0.875em);
548
- }
549
-
550
- /* === AsciiDoc quote blocks === */
551
- .quoteblock {
552
- margin: var(--md-blockquote-margin-vertical, 1em) var(--md-blockquote-margin-horizontal, 0);
553
- padding: var(--md-blockquote-padding-vertical, 0.5em) var(--md-blockquote-padding-horizontal, 1em);
554
- border-left: var(--md-blockquote-border-left, 3px solid var(--border-color-secondary, #ddd));
555
- background-color: var(--md-blockquote-bg-color, transparent);
556
- color: var(--md-blockquote-text-color, var(--text-color-secondary, #666));
557
- }
558
-
559
- .quoteblock blockquote {
560
- margin: 0;
561
- padding: 0;
562
- border: none;
563
- background: transparent;
564
- }
565
-
566
- .quoteblock .attribution {
567
- margin-top: var(--spacing-xs, 0.5em);
568
- font-style: italic;
569
- }
570
-
571
- /* === AsciiDoc sidebar blocks === */
572
- .sidebarblock {
573
- margin: var(--spacing-base, 1em) 0;
574
- padding: var(--spacing-sm, 0.75em) var(--spacing-base, 1em);
575
- background-color: var(--layer-color, #f8f8f8);
576
- border-radius: var(--border-radius-lg, 6px);
577
- border: 1px solid var(--border-color-secondary, #e0e0e0);
578
- }
579
-
580
- .sidebarblock > .content > .title {
581
- font-weight: var(--font-weight-semibold, 600);
582
- margin-bottom: var(--spacing-xs, 0.5em);
583
- }
584
-
585
- /* === AsciiDoc example blocks === */
586
- .exampleblock {
587
- margin: var(--spacing-base) 0;
588
- }
589
-
590
- .exampleblock > .title {
591
- font-weight: var(--font-weight-semibold);
592
- margin-bottom: var(--spacing-sm);
593
- }
594
-
595
- .exampleblock > .content {
596
- padding: var(--spacing-sm, 0.75em) var(--spacing-base, 1em);
597
- border: 1px solid var(--border-color-secondary, #e0e0e0);
598
- border-radius: var(--border-radius-lg, 6px);
599
- }
600
-
601
- /* === Callout badges in code blocks === */
602
- .code-callout-badge {
603
- display: inline-flex;
604
- align-items: center;
605
- justify-content: center;
606
- min-width: 1.25em;
607
- height: 1.25em;
608
- border-radius: 50%;
609
- background-color: var(--callout-badge-bg, #333);
610
- color: var(--callout-badge-color, #fff);
611
- font-size: 0.75em;
612
- font-weight: 600;
613
- margin-left: 0.5em;
614
- vertical-align: middle;
615
- user-select: none;
616
- }
617
-
618
- /* === Callout lists === */
619
- .callout-list {
620
- margin-top: var(--spacing-xs, 0.5em);
621
- margin-bottom: var(--spacing-base, 1em);
622
- padding-left: 0;
623
- list-style: none;
624
- }
625
-
626
- .callout-list li {
627
- display: flex;
628
- align-items: baseline;
629
- gap: 0.5em;
630
- margin-bottom: 0.25em;
631
- }
632
-
633
- .callout-badge {
634
- display: inline-flex;
635
- align-items: center;
636
- justify-content: center;
637
- min-width: 1.25em;
638
- height: 1.25em;
639
- border-radius: 50%;
640
- background-color: var(--callout-badge-bg, #333);
641
- color: var(--callout-badge-color, #fff);
642
- font-size: 0.75em;
643
- font-weight: var(--font-weight-semibold, 600);
644
- flex-shrink: 0;
645
- }
646
-
647
- /* === AsciiDoc roles (custom CSS classes) === */
648
- .underline {
649
- text-decoration: underline;
650
- }
651
-
652
- .line-through {
653
- text-decoration: line-through;
654
- }
655
-
656
- .text-center {
657
- text-align: center;
658
- }
659
-
660
- .text-right {
661
- text-align: right;
662
- }
663
-
664
- /* === AsciiDoc footer rows === */
665
- table tfoot {
666
- font-weight: var(--font-weight-semibold, 600);
667
- background-color: var(--layer-color, #f8f8f8);
668
- }
669
-
670
- /* === Collapsible blocks === */
671
- details {
672
- margin: var(--spacing-base, 1em) 0;
673
- border: 1px solid var(--border-color-secondary, #e0e0e0);
674
- border-radius: var(--border-radius-lg, 6px);
675
- padding: var(--spacing-sm, 0.75em) var(--spacing-base, 1em);
676
- }
677
-
678
- details summary {
679
- cursor: pointer;
680
- font-weight: var(--font-weight-semibold, 600);
681
- margin-bottom: var(--spacing-xs, 0.5em);
682
- }
683
-
684
- details[open] summary {
685
- margin-bottom: var(--spacing-sm, 0.75em);
686
- }
687
- `;