@@ -165,35 +165,21 @@ module.exports = (
165
165
}
166
166
}
167
167
168
- async function getMarkdownAST ( markdownNode ) {
169
- if ( process . env . NODE_ENV !== `production` || ! fileNodes ) {
170
- fileNodes = getNodesByType ( `File` )
168
+ // Parse a markdown string and its AST representation,
169
+ // applying the remark plugins if necesserary
170
+ async function parseString ( string , markdownNode ) {
171
+ // compiler to inject in the remark plugins
172
+ // so that they can use our parser/generator
173
+ // with all the options and plugins from the user
174
+ const compiler = {
175
+ parseString : string => parseString ( string , markdownNode ) ,
176
+ generateHTML : ast =>
177
+ hastToHTML ( markdownASTToHTMLAst ( ast ) , {
178
+ allowDangerousHTML : true ,
179
+ } ) ,
171
180
}
172
- // Use Bluebird's Promise function "each" to run remark plugins serially.
173
- await Promise . each ( pluginOptions . plugins , plugin => {
174
- const requiredPlugin = require ( plugin . resolve )
175
- if ( _ . isFunction ( requiredPlugin . mutateSource ) ) {
176
- return requiredPlugin . mutateSource (
177
- {
178
- markdownNode,
179
- files : fileNodes ,
180
- getNode,
181
- reporter,
182
- cache : getCache ( plugin . name ) ,
183
- getCache,
184
- compiler : {
185
- parseString : remark . parse . bind ( remark ) ,
186
- generateHTML : getHTML ,
187
- } ,
188
- ...rest ,
189
- } ,
190
- plugin . pluginOptions
191
- )
192
- } else {
193
- return Promise . resolve ( )
194
- }
195
- } )
196
- const markdownAST = remark . parse ( markdownNode . internal . content )
181
+
182
+ const markdownAST = remark . parse ( string )
197
183
198
184
if ( basePath ) {
199
185
// Ensure relative links include `pathPrefix`
@@ -232,10 +218,7 @@ module.exports = (
232
218
reporter,
233
219
cache : getCache ( plugin . name ) ,
234
220
getCache,
235
- compiler : {
236
- parseString : remark . parse . bind ( remark ) ,
237
- generateHTML : getHTML ,
238
- } ,
221
+ compiler,
239
222
...rest ,
240
223
} ,
241
224
plugin . pluginOptions
@@ -248,6 +231,38 @@ module.exports = (
248
231
return markdownAST
249
232
}
250
233
234
+ async function getMarkdownAST ( markdownNode ) {
235
+ if ( process . env . NODE_ENV !== `production` || ! fileNodes ) {
236
+ fileNodes = getNodesByType ( `File` )
237
+ }
238
+
239
+ // Execute the remark plugins that can mutate the node
240
+ // before parsing its content
241
+ //
242
+ // Use Bluebird's Promise function "each" to run remark plugins serially.
243
+ await Promise . each ( pluginOptions . plugins , plugin => {
244
+ const requiredPlugin = require ( plugin . resolve )
245
+ if ( _ . isFunction ( requiredPlugin . mutateSource ) ) {
246
+ return requiredPlugin . mutateSource (
247
+ {
248
+ markdownNode,
249
+ files : fileNodes ,
250
+ getNode,
251
+ reporter,
252
+ cache : getCache ( plugin . name ) ,
253
+ getCache,
254
+ ...rest ,
255
+ } ,
256
+ plugin . pluginOptions
257
+ )
258
+ } else {
259
+ return Promise . resolve ( )
260
+ }
261
+ } )
262
+
263
+ return parseString ( markdownNode . internal . content , markdownNode )
264
+ }
265
+
251
266
async function getHeadings ( markdownNode ) {
252
267
const cachedHeadings = await cache . get ( headingsCacheKey ( markdownNode ) )
253
268
if ( cachedHeadings ) {
@@ -323,16 +338,20 @@ module.exports = (
323
338
}
324
339
}
325
340
341
+ async function markdownASTToHTMLAst ( ast ) {
342
+ return toHAST ( ast , {
343
+ allowDangerousHTML : true ,
344
+ handlers : { code : codeHandler } ,
345
+ } )
346
+ }
347
+
326
348
async function getHTMLAst ( markdownNode ) {
327
349
const cachedAst = await cache . get ( htmlAstCacheKey ( markdownNode ) )
328
350
if ( cachedAst ) {
329
351
return cachedAst
330
352
} else {
331
353
const ast = await getAST ( markdownNode )
332
- const htmlAst = toHAST ( ast , {
333
- allowDangerousHTML : true ,
334
- handlers : { code : codeHandler } ,
335
- } )
354
+ const htmlAst = markdownASTToHTMLAst ( ast )
336
355
337
356
// Save new HTML AST to cache and return
338
357
cache . set ( htmlAstCacheKey ( markdownNode ) , htmlAst )
@@ -341,9 +360,7 @@ module.exports = (
341
360
}
342
361
343
362
async function getHTML ( markdownNode ) {
344
- const shouldCache = markdownNode && markdownNode . internal
345
- const cachedHTML =
346
- shouldCache && ( await cache . get ( htmlCacheKey ( markdownNode ) ) )
363
+ const cachedHTML = await cache . get ( htmlCacheKey ( markdownNode ) )
347
364
if ( cachedHTML ) {
348
365
return cachedHTML
349
366
} else {
@@ -353,10 +370,8 @@ module.exports = (
353
370
allowDangerousHTML : true ,
354
371
} )
355
372
356
- if ( shouldCache ) {
357
- // Save new HTML to cache
358
- cache . set ( htmlCacheKey ( markdownNode ) , html )
359
- }
373
+ // Save new HTML to cache
374
+ cache . set ( htmlCacheKey ( markdownNode ) , html )
360
375
361
376
return html
362
377
}
0 commit comments