1
0
mirror of synced 2025-12-23 21:07:12 -05:00

Refactor Thead Th scope Markdown plugin (#37299)

This commit is contained in:
Peter Bengtsson
2023-05-30 11:06:08 -04:00
committed by GitHub
parent 63959daf83
commit 76ddf3a70c
4 changed files with 39 additions and 26 deletions

View File

@@ -0,0 +1,35 @@
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)
}