1
0
mirror of synced 2025-12-23 11:54:18 -05:00
Files
docs/lib/render-content/plugins/parse-info-string.js
2023-06-26 13:55:16 +00:00

32 lines
874 B
JavaScript

// Based on https://spec.commonmark.org/0.30/#info-string
// Parse out info strings on fenced code blocks, example:
// ```javascript lineNumbers:left copy:all annotate
// becomes...
// node.lang = javascript
// node.meta = { lineNumbers: 'left', copy: 'all', annotate: true }
import { visit } from 'unist-util-visit'
const matcher = (node) => node.type === 'code' && node.lang
export default function parseInfoString() {
return (tree) => {
visit(tree, matcher, (node) => {
node.meta = strToObj(node.meta)
// Temporary, remove {:copy} to avoid highlight parse error in translations.
node.lang = node.lang.replace('{:copy}', '')
})
}
}
function strToObj(str) {
if (!str) return {}
return Object.fromEntries(
str
.split(/\s+/g)
.map((k) => k.split(':'))
.map(([k, ...v]) => [k, v.length ? v.join(':') : true])
)
}