-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
parse.js
217 lines (189 loc) · 6.21 KB
/
parse.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/** @module */
import MarkdownItFrontMatter from 'markdown-it-front-matter'
import marpitPlugin from '../../plugin'
import { markAsParsed } from '../comment'
import * as directives from './directives'
import { yaml } from './yaml'
const isDirectiveComment = (token) =>
token.type === 'marpit_comment' && token.meta.marpitParsedDirectives
/**
* Parse Marpit directives and store result to the slide token meta.
*
* Marpit comment plugin ans slide plugin requires already loaded to
* markdown-it instance.
*
* @function parse
* @param {MarkdownIt} md markdown-it instance.
* @param {Object} [opts]
* @param {boolean} [opts.frontMatter=true] Switch feature to support YAML
* front-matter. If true, you can use Jekyll style directive setting to the
* first page.
*/
function _parse(md, opts = {}) {
const { marpit } = md
const applyBuiltinDirectives = (newProps, builtinDirectives) => {
let ret = {}
for (const prop of Object.keys(newProps)) {
if (builtinDirectives[prop]) {
ret = { ...ret, ...builtinDirectives[prop](newProps[prop], marpit) }
} else {
ret[prop] = newProps[prop]
}
}
return ret
}
// Front-matter support
const frontMatter = opts.frontMatter === undefined ? true : !!opts.frontMatter
let frontMatterObject = {}
if (frontMatter) {
md.core.ruler.before('block', 'marpit_directives_front_matter', (state) => {
frontMatterObject = {}
if (!state.inlineMode) marpit.lastGlobalDirectives = {}
})
md.use(MarkdownItFrontMatter, (fm) => {
frontMatterObject.text = fm
const parsed = yaml(
fm,
marpit.options.looseYAML
? [
...Object.keys(marpit.customDirectives.global),
...Object.keys(marpit.customDirectives.local),
]
: false,
)
if (parsed !== false) frontMatterObject.yaml = parsed
})
}
// Parse global directives
md.core.ruler.after('inline', 'marpit_directives_global_parse', (state) => {
if (state.inlineMode) return
let globalDirectives = {}
const applyDirectives = (obj) => {
let recognized = false
for (const key of Object.keys(obj)) {
if (directives.globals[key]) {
recognized = true
globalDirectives = {
...globalDirectives,
...directives.globals[key](obj[key], marpit),
}
} else if (marpit.customDirectives.global[key]) {
recognized = true
globalDirectives = {
...globalDirectives,
...applyBuiltinDirectives(
marpit.customDirectives.global[key](obj[key], marpit),
directives.globals,
),
}
}
}
return recognized
}
if (frontMatterObject.yaml) applyDirectives(frontMatterObject.yaml)
for (const token of state.tokens) {
if (
isDirectiveComment(token) &&
applyDirectives(token.meta.marpitParsedDirectives)
) {
markAsParsed(token, 'directive')
} else if (token.type === 'inline') {
for (const t of token.children) {
if (
isDirectiveComment(t) &&
applyDirectives(t.meta.marpitParsedDirectives)
)
markAsParsed(t, 'directive')
}
}
}
marpit.lastGlobalDirectives = { ...globalDirectives }
})
// Parse local directives and apply meta to slide
md.core.ruler.after('marpit_slide', 'marpit_directives_parse', (state) => {
if (state.inlineMode) return
const slides = []
const cursor = { slide: undefined, local: {}, spot: {} }
const applyDirectives = (obj) => {
let recognized = false
for (const key of Object.keys(obj)) {
if (directives.locals[key]) {
recognized = true
cursor.local = {
...cursor.local,
...directives.locals[key](obj[key], marpit),
}
} else if (marpit.customDirectives.local[key]) {
recognized = true
cursor.local = {
...cursor.local,
...applyBuiltinDirectives(
marpit.customDirectives.local[key](obj[key], marpit),
directives.locals,
),
}
}
// Spot directives
// (Apply local directive to only current slide by prefix "_")
if (key.startsWith('_')) {
const spotKey = key.slice(1)
if (directives.locals[spotKey]) {
recognized = true
cursor.spot = {
...cursor.spot,
...directives.locals[spotKey](obj[key], marpit),
}
} else if (marpit.customDirectives.local[spotKey]) {
recognized = true
cursor.spot = {
...cursor.spot,
...applyBuiltinDirectives(
marpit.customDirectives.local[spotKey](obj[key], marpit),
directives.locals,
),
}
}
}
}
return recognized
}
if (frontMatterObject.yaml) applyDirectives(frontMatterObject.yaml)
for (const token of state.tokens) {
if (token.meta && token.meta.marpitSlideElement === 1) {
// Initialize Marpit directives meta
token.meta.marpitDirectives = {}
slides.push(token)
cursor.slide = token
} else if (token.meta && token.meta.marpitSlideElement === -1) {
// Assign local and spot directives to meta
cursor.slide.meta.marpitDirectives = {
...cursor.slide.meta.marpitDirectives,
...cursor.local,
...cursor.spot,
}
cursor.spot = {}
} else if (
isDirectiveComment(token) &&
applyDirectives(token.meta.marpitParsedDirectives)
) {
markAsParsed(token, 'directive')
} else if (token.type === 'inline') {
for (const t of token.children) {
if (
isDirectiveComment(t) &&
applyDirectives(t.meta.marpitParsedDirectives)
)
markAsParsed(t, 'directive')
}
}
}
// Assign global directives to meta
for (const token of slides)
token.meta.marpitDirectives = {
...token.meta.marpitDirectives,
...marpit.lastGlobalDirectives,
}
})
}
export const parse = marpitPlugin(_parse)
export default parse