-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
30 lines (26 loc) · 868 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import markdoc from '@markdoc/markdoc'
import yaml from 'js-yaml'
export default function preprocessMarkdoc(config = {}) {
return (input) => {
if (isMarkdoc(input.filename)) {
return {
code: render(input.content, config)
}
}
}
}
function isMarkdoc(path) {
return /\.markdoc$/.test(path)
}
function render(source, config) {
const ast = markdoc.parse(source)
const configWithFrontmatter = addFrontmatter(ast, config)
const content = markdoc.transform(ast, configWithFrontmatter)
return markdoc.renderers.html(content)
}
function addFrontmatter(ast, config) {
const frontmatter = ast.attributes.frontmatter ? yaml.load(ast.attributes.frontmatter) : {}
const markdoc = { ...(config?.variables?.markdoc || {}), frontmatter }
const variables = { ...(config?.variables || {}), markdoc }
return { ...config, variables }
}