-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransformation.cjs
48 lines (39 loc) · 1.37 KB
/
transformation.cjs
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// ------------------------------------------------------------- //
// This example shows how parameters can be passed to a transformer.
// ------------------------------------------------------------- //
function template(ast, api, options) {
const optKeys = Object.keys(options)
if (optKeys.length === 0) {
process.stderr.write(`Pass options to this example to test its behaviour, e.g. :
yarn example params --root-heading 2
This example will read \`params.rootHeading\` to exploit the value.
`)
} else {
process.stderr.write(`
Received options:
${JSON.stringify(options, null, 2)}
`)
if (options.rootHeading) {
const newTopLevel = options.rootHeading
if (typeof newTopLevel !== 'number' || newTopLevel < 1 || newTopLevel > 4) {
throw new Error('Invalid option --root-heading: value must be a number between 1 and 4.')
}
const headings = api.exploreAst(
ast,
({ tag, type }) => tag && tag.match(/h[1-6]/) && type === 1,
)
const oldTopLevel = headings
.map((h) => Number(h.tag.replace('h', '')))
.reduce((min, n) => Math.min(min, n), 6)
const topLevelDiff = newTopLevel - oldTopLevel
headings.forEach((h) => {
const currentLevel = Number(h.tag.replace('h', ''))
h.tag = `h${currentLevel + topLevelDiff}`
})
}
}
return ast
}
module.exports = {
template,
}