Move annotate from Liquid to info string (#37651)
Co-authored-by: Peter Bengtsson <peterbe@github.com>
This commit is contained in:
27
lib/render-content/plugins/parse-info-string.js
Normal file
27
lib/render-content/plugins/parse-info-string.js
Normal file
@@ -0,0 +1,27 @@
|
||||
// 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 && node.meta
|
||||
|
||||
export default function parseInfoString() {
|
||||
return (tree) => {
|
||||
visit(tree, matcher, (node) => {
|
||||
node.meta = strToObj(node.meta)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function strToObj(str) {
|
||||
return Object.fromEntries(
|
||||
str
|
||||
.split(/\s+/g)
|
||||
.map((k) => k.split(':'))
|
||||
.map(([k, ...v]) => [k, v.length ? v.join(':') : true])
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user