1
0
mirror of synced 2025-12-23 11:54:18 -05:00
Files
docs/lib/render-content/plugins/rewrite-thead-th-scope.js
2023-05-30 15:06:08 +00:00

36 lines
777 B
JavaScript

import { visitParents } from 'unist-util-visit-parents'
/**
* Where it can mutate the AST to swap from:
*
* <thead>
* <tr>
* <th>...</th>
* <th>...</th>
*
* to:
* <thead>
* <tr>
* <th scope="col">...</th>
* <th scope="col">...</th>
*
* */
function matcher(node) {
return node.type === 'element' && node.tagName === 'th' && !('scope' in node.properties)
}
function visitor(node, ancestors) {
const parent = ancestors.at(-1)
if (parent && parent.tagName === 'tr') {
const grandParent = ancestors.at(-2)
if (grandParent && grandParent.tagName === 'thead') {
node.properties.scope = 'col'
}
}
}
export default function rewriteTheadThScope() {
return (tree) => visitParents(tree, matcher, visitor)
}