-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (44 loc) · 1.24 KB
/
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
exports.mutateSource = ({ markdownNode }) => {
if (!Object.keys(markdownNode.frontmatter).includes('reveal')) {
return
}
markdownNode.internal.content = mutate(markdownNode.internal.content)
}
/**
* mutate remark content
*
* @param {*} md markdown content
* @returns
*/
function mutate(md) {
let del = getDelimiterRegx('--h--')
return md.split(del).map(mutateContent).join('\n')
}
/**
* mutate remark content
*
* @param {*} md markdown content
* @returns
*/
function mutateContent(md) {
let noteDel = getDelimiterRegx('reveal-note:')
let verticalDel = getDelimiterRegx('--v--')
if (md.match(verticalDel)) {
md = md.split(verticalDel).map(mutateContent).join('\n')
}
if (md.match(noteDel)) {
let [content, ...notes] = md.split(noteDel)
let notesContent = notes.join('\n')
md = `${content}\n\n<aside class="notes">\n\n${notesContent}\n\n</aside>`
}
return `<section>\n\n${md}\n\n</section>`
}
function getDelimiterRegx(token) {
return new RegExp('^\\s*' + escapeStringRegxP(token) + '(?:$|\\s)', 'gmi')
}
function escapeStringRegxP(string) {
if (typeof string !== 'string') {
throw new TypeError('Expected a string')
}
return string.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d')
}