|
| 1 | +/** |
| 2 | + * Rehype plugin that adds anchor links to option names in documentation tables. |
| 3 | + * |
| 4 | + * Targets tables whose first header cell contains "Option". For each data row, |
| 5 | + * it extracts the option name from the first cell's `<code>` element, generates |
| 6 | + * an anchor slug, sets an `id` on the `<tr>`, and wraps the `<code>` in an |
| 7 | + * `<a>` link with the Starlight-style anchor icon. |
| 8 | + */ |
| 9 | + |
| 10 | +import type { Element, ElementContent, Root, Text } from 'hast'; |
| 11 | +import { |
| 12 | + LINK_ICON_PATH, |
| 13 | + optionSlug, |
| 14 | + TABLE_HEADERS_TO_MATCH, |
| 15 | +} from './option-slug'; |
| 16 | + |
| 17 | +function getTextContent(node: ElementContent | Root): string { |
| 18 | + if (node.type === 'text') return (node as Text).value; |
| 19 | + if ('children' in node) { |
| 20 | + return (node.children as ElementContent[]).map(getTextContent).join(''); |
| 21 | + } |
| 22 | + return ''; |
| 23 | +} |
| 24 | + |
| 25 | +function isElement(node: ElementContent, tag?: string): node is Element { |
| 26 | + return node.type === 'element' && (!tag || node.tagName === tag); |
| 27 | +} |
| 28 | + |
| 29 | +function findElement( |
| 30 | + children: ElementContent[], |
| 31 | + tag: string |
| 32 | +): Element | undefined { |
| 33 | + for (const child of children) { |
| 34 | + if (isElement(child)) { |
| 35 | + if (child.tagName === tag) return child; |
| 36 | + const found = findElement(child.children, tag); |
| 37 | + if (found) return found; |
| 38 | + } |
| 39 | + } |
| 40 | + return undefined; |
| 41 | +} |
| 42 | + |
| 43 | +function findAllElements(children: ElementContent[], tag: string): Element[] { |
| 44 | + const results: Element[] = []; |
| 45 | + for (const child of children) { |
| 46 | + if (isElement(child, tag)) results.push(child); |
| 47 | + if (isElement(child)) { |
| 48 | + results.push(...findAllElements(child.children, tag)); |
| 49 | + } |
| 50 | + } |
| 51 | + return results; |
| 52 | +} |
| 53 | + |
| 54 | +function makeAnchorIcon(): Element { |
| 55 | + return { |
| 56 | + type: 'element', |
| 57 | + tagName: 'span', |
| 58 | + properties: { ariaHidden: 'true', className: ['sl-anchor-icon'] }, |
| 59 | + children: [ |
| 60 | + { |
| 61 | + type: 'element', |
| 62 | + tagName: 'svg', |
| 63 | + properties: { |
| 64 | + width: '16', |
| 65 | + height: '16', |
| 66 | + viewBox: '0 0 24 24', |
| 67 | + fill: 'currentcolor', |
| 68 | + }, |
| 69 | + children: [ |
| 70 | + { |
| 71 | + type: 'element', |
| 72 | + tagName: 'path', |
| 73 | + properties: { d: LINK_ICON_PATH }, |
| 74 | + children: [], |
| 75 | + }, |
| 76 | + ], |
| 77 | + }, |
| 78 | + ], |
| 79 | + }; |
| 80 | +} |
| 81 | + |
| 82 | +function isOptionsTable(table: Element): boolean { |
| 83 | + const thead = findElement(table.children as ElementContent[], 'thead'); |
| 84 | + if (!thead) return false; |
| 85 | + const firstTh = findElement(thead.children as ElementContent[], 'th'); |
| 86 | + if (!firstTh) return false; |
| 87 | + const text = getTextContent(firstTh).trim(); |
| 88 | + return TABLE_HEADERS_TO_MATCH.includes(text.toLowerCase()); |
| 89 | +} |
| 90 | + |
| 91 | +function processOptionsTable(table: Element): void { |
| 92 | + const tbody = findElement(table.children as ElementContent[], 'tbody'); |
| 93 | + if (!tbody) return; |
| 94 | + |
| 95 | + const rows = findAllElements(tbody.children as ElementContent[], 'tr'); |
| 96 | + |
| 97 | + for (const row of rows) { |
| 98 | + const cells = row.children.filter((c) => isElement(c, 'td')) as Element[]; |
| 99 | + if (cells.length === 0) continue; |
| 100 | + |
| 101 | + const firstCell = cells[0]; |
| 102 | + const codeEl = findElement(firstCell.children as ElementContent[], 'code'); |
| 103 | + if (!codeEl) continue; |
| 104 | + |
| 105 | + const optionText = getTextContent(codeEl).trim(); |
| 106 | + if (!optionText) continue; |
| 107 | + |
| 108 | + const slug = optionSlug(optionText); |
| 109 | + if (!slug) continue; |
| 110 | + |
| 111 | + // Set id on the row for anchor targeting |
| 112 | + row.properties = row.properties || {}; |
| 113 | + row.properties.id = slug; |
| 114 | + |
| 115 | + // Find the index of the code element in the cell's children |
| 116 | + const codeIndex = firstCell.children.indexOf(codeEl); |
| 117 | + if (codeIndex === -1) continue; |
| 118 | + |
| 119 | + // Wrap the <code> element in an <a> link |
| 120 | + const anchorLink: Element = { |
| 121 | + type: 'element', |
| 122 | + tagName: 'a', |
| 123 | + properties: { |
| 124 | + href: `#${slug}`, |
| 125 | + className: ['sl-option-link'], |
| 126 | + }, |
| 127 | + children: [codeEl, makeAnchorIcon()], |
| 128 | + }; |
| 129 | + |
| 130 | + firstCell.children[codeIndex] = anchorLink; |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +export default function rehypeTableOptionLinks() { |
| 135 | + return (tree: Root) => { |
| 136 | + const tables = findAllElements(tree.children as ElementContent[], 'table'); |
| 137 | + for (const table of tables) { |
| 138 | + if (isOptionsTable(table)) { |
| 139 | + processOptionsTable(table); |
| 140 | + } |
| 141 | + } |
| 142 | + }; |
| 143 | +} |
0 commit comments