1
0
mirror of synced 2025-12-23 11:54:18 -05:00

Code-headers plugin rewrite (#37654)

Co-authored-by: Peter Bengtsson <peterbe@github.com>
This commit is contained in:
Kevin Heis
2023-06-12 08:04:49 -07:00
committed by GitHub
parent 76af8ce4ab
commit 0c34f6d648
9 changed files with 75 additions and 166 deletions

View File

@@ -7,17 +7,26 @@
import { visit } from 'unist-util-visit'
const matcher = (node) => node.type === 'code' && node.lang && node.meta
const matcher = (node) => node.type === 'code' && node.lang
export default function parseInfoString() {
return (tree) => {
visit(tree, matcher, (node) => {
node.meta = strToObj(node.meta)
// Temporary, change {:copy} to ` copy` to avoid conflict in styles.
// We may end up enabling copy on all code examples later.
const copyTag = '{:copy}'
if (node.lang.includes(copyTag)) {
node.meta.copy = true
node.lang = node.lang.replace(copyTag, '')
}
})
}
}
function strToObj(str) {
if (!str) return {}
return Object.fromEntries(
str
.split(/\s+/g)