Skip to content

Commit 82c05aa

Browse files
feat(gatsby-remark-images): support markdownCaptions in mdx + fix for remark (#21188)
* pass compiler to gatsby-remark-* plugins in the mdx plugin * make getImageCaption async * use async mdx compiler * fix for using compiler * don't mutate the node multiple times * convert markdown ast to html ast Co-authored-by: vladar
1 parent 3beca2e commit 82c05aa

File tree

3 files changed

+65
-46
lines changed

3 files changed

+65
-46
lines changed

packages/gatsby-plugin-mdx/utils/gen-mdx.js

+4
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ export const _frontmatter = ${JSON.stringify(data)}`
116116
reporter,
117117
cache,
118118
pathPrefix,
119+
compiler: {
120+
parseString: compiler.parse.bind(compiler),
121+
generateHTML: ast => mdx(ast, options),
122+
},
119123
...helpers,
120124
}
121125
)

packages/gatsby-remark-images/src/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ module.exports = (
7878
}
7979
}
8080

81-
const getImageCaption = (node, overWrites) => {
81+
const getImageCaption = async (node, overWrites) => {
8282
const getCaptionString = () => {
8383
const captionOptions = Array.isArray(options.showCaptions)
8484
? options.showCaptions
@@ -115,7 +115,7 @@ module.exports = (
115115
return _.escape(captionString)
116116
}
117117

118-
return compiler.generateHTML(compiler.parseString(captionString))
118+
return compiler.generateHTML(await compiler.parseString(captionString))
119119
}
120120

121121
// Takes a node and generates the needed images and then returns
@@ -288,7 +288,7 @@ module.exports = (
288288

289289
// Construct new image node w/ aspect ratio placeholder
290290
const imageCaption =
291-
options.showCaptions && getImageCaption(node, overWrites)
291+
options.showCaptions && (await getImageCaption(node, overWrites))
292292

293293
let removeBgImage = false
294294
if (options.disableBgImageOnAlpha) {

packages/gatsby-transformer-remark/src/extend-node-type.js

+58-43
Original file line numberDiff line numberDiff line change
@@ -165,35 +165,21 @@ module.exports = (
165165
}
166166
}
167167

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+
}),
171180
}
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)
197183

198184
if (basePath) {
199185
// Ensure relative links include `pathPrefix`
@@ -232,10 +218,7 @@ module.exports = (
232218
reporter,
233219
cache: getCache(plugin.name),
234220
getCache,
235-
compiler: {
236-
parseString: remark.parse.bind(remark),
237-
generateHTML: getHTML,
238-
},
221+
compiler,
239222
...rest,
240223
},
241224
plugin.pluginOptions
@@ -248,6 +231,38 @@ module.exports = (
248231
return markdownAST
249232
}
250233

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+
251266
async function getHeadings(markdownNode) {
252267
const cachedHeadings = await cache.get(headingsCacheKey(markdownNode))
253268
if (cachedHeadings) {
@@ -323,16 +338,20 @@ module.exports = (
323338
}
324339
}
325340

341+
async function markdownASTToHTMLAst(ast) {
342+
return toHAST(ast, {
343+
allowDangerousHTML: true,
344+
handlers: { code: codeHandler },
345+
})
346+
}
347+
326348
async function getHTMLAst(markdownNode) {
327349
const cachedAst = await cache.get(htmlAstCacheKey(markdownNode))
328350
if (cachedAst) {
329351
return cachedAst
330352
} else {
331353
const ast = await getAST(markdownNode)
332-
const htmlAst = toHAST(ast, {
333-
allowDangerousHTML: true,
334-
handlers: { code: codeHandler },
335-
})
354+
const htmlAst = markdownASTToHTMLAst(ast)
336355

337356
// Save new HTML AST to cache and return
338357
cache.set(htmlAstCacheKey(markdownNode), htmlAst)
@@ -341,9 +360,7 @@ module.exports = (
341360
}
342361

343362
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))
347364
if (cachedHTML) {
348365
return cachedHTML
349366
} else {
@@ -353,10 +370,8 @@ module.exports = (
353370
allowDangerousHTML: true,
354371
})
355372

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)
360375

361376
return html
362377
}

0 commit comments

Comments
 (0)