|
1 | 1 | import { visit } from 'unist-util-visit' |
2 | | -import type { Root, Element } from 'hast' |
| 2 | +import type { Root, Element, ElementContent, Text } from 'hast' |
| 3 | + |
| 4 | +const FOOTNOTES_HEADING_TEXT = 'Footnotes' |
| 5 | +const FOOTNOTES_HEADING_ID = 'footnote-label' |
| 6 | + |
| 7 | +function isElementNode(node: ElementContent): node is Element { |
| 8 | + return node.type === 'element' |
| 9 | +} |
| 10 | + |
| 11 | +function getNodeText(node: Element): string { |
| 12 | + let textContent = '' |
| 13 | + |
| 14 | + for (const child of node.children) { |
| 15 | + if (child.type === 'text') { |
| 16 | + textContent += (child as Text).value |
| 17 | + continue |
| 18 | + } |
| 19 | + |
| 20 | + if (isElementNode(child)) { |
| 21 | + textContent += getNodeText(child) |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + return textContent |
| 26 | +} |
| 27 | + |
| 28 | +function findFootnotesHeading(section: Element): Element | null { |
| 29 | + for (const child of section.children) { |
| 30 | + if (!isElementNode(child) || child.tagName !== 'h2') continue |
| 31 | + |
| 32 | + if (getNodeText(child).trim() === FOOTNOTES_HEADING_TEXT) { |
| 33 | + return child |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + return null |
| 38 | +} |
| 39 | + |
| 40 | +function labelFootnotesSection(section: Element): void { |
| 41 | + const heading = findFootnotesHeading(section) |
| 42 | + if (!heading) return |
| 43 | + |
| 44 | + const headingProperties = heading.properties || {} |
| 45 | + |
| 46 | + const headingClasses = headingProperties['className'] |
| 47 | + if (Array.isArray(headingClasses)) { |
| 48 | + headingProperties['className'] = headingClasses.filter(className => className !== 'sr-only') |
| 49 | + } |
| 50 | + |
| 51 | + const headingId = typeof headingProperties['id'] === 'string' |
| 52 | + ? headingProperties['id'] |
| 53 | + : FOOTNOTES_HEADING_ID |
| 54 | + |
| 55 | + headingProperties['id'] = headingId |
| 56 | + heading.properties = headingProperties |
| 57 | + |
| 58 | + const sectionProperties = section.properties || {} |
| 59 | + sectionProperties['aria-labelledby'] = headingId |
| 60 | + section.properties = sectionProperties |
| 61 | +} |
| 62 | + |
| 63 | +function isFootnotesSection(node: Element): boolean { |
| 64 | + const properties = node.properties || {} |
| 65 | + return 'dataFootnotes' in properties || 'data-footnotes' in properties |
| 66 | +} |
3 | 67 |
|
4 | 68 | function getIdentifierFromBackrefHref(href: unknown): string | null { |
5 | 69 | if (typeof href !== 'string') return null |
@@ -27,6 +91,10 @@ export type RehypeFootnotesTitleOptions = string |
27 | 91 | export function rehypeFootnotesTitle(titleTemplate: RehypeFootnotesTitleOptions = '') { |
28 | 92 | return (tree: Root) => { |
29 | 93 | visit(tree, 'element', (node: Element) => { |
| 94 | + if (node.tagName === 'section' && isFootnotesSection(node)) { |
| 95 | + labelFootnotesSection(node) |
| 96 | + } |
| 97 | + |
30 | 98 | if (node.tagName !== 'a') return |
31 | 99 |
|
32 | 100 | const properties = node.properties || {} |
|
0 commit comments