Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gatsby-remark-images): support markdownCaptions in mdx + fix for remark #21188

Merged
merged 7 commits into from
Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/gatsby-plugin-mdx/utils/gen-mdx.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ export const _frontmatter = ${JSON.stringify(data)}`
reporter,
cache,
pathPrefix,
compiler: {
parseString: compiler.parse.bind(compiler),
generateHTML: ast => mdx(ast, options),
},
...helpers,
}
)
Expand Down
6 changes: 3 additions & 3 deletions packages/gatsby-remark-images/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ module.exports = (
}
}

const getImageCaption = (node, overWrites) => {
const getImageCaption = async (node, overWrites) => {
const getCaptionString = () => {
const captionOptions = Array.isArray(options.showCaptions)
? options.showCaptions
Expand Down Expand Up @@ -115,7 +115,7 @@ module.exports = (
return _.escape(captionString)
}

return compiler.generateHTML(compiler.parseString(captionString))
return compiler.generateHTML(await compiler.parseString(captionString))
}

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

// Construct new image node w/ aspect ratio placeholder
const imageCaption =
options.showCaptions && getImageCaption(node, overWrites)
options.showCaptions && (await getImageCaption(node, overWrites))

let removeBgImage = false
if (options.disableBgImageOnAlpha) {
Expand Down
46 changes: 27 additions & 19 deletions packages/gatsby-transformer-remark/src/extend-node-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ module.exports = (
// We are already generating AST, so let's wait for it
return await ASTPromiseMap.get(cacheKey)
} else {
const ASTGenerationPromise = getMarkdownAST(markdownNode)
const ASTGenerationPromise = getMarkdownAST(
markdownNode.internal.content,
markdownNode
)
ASTGenerationPromise.then(markdownAST => {
ASTPromiseMap.delete(cacheKey)
return cache.set(cacheKey, markdownAST)
Expand All @@ -165,10 +168,22 @@ module.exports = (
}
}

async function getMarkdownAST(markdownNode) {
async function getMarkdownAST(content, markdownNode) {
if (process.env.NODE_ENV !== `production` || !fileNodes) {
fileNodes = getNodesByType(`File`)
}

// compiler to inject in the remark plugins
// so that they can use our parser/generator
// with all the options and plugins from the user
const compiler = {
parseString: string => getMarkdownAST(string, markdownNode),
generateHTML: ast =>
hastToHTML(ast, {
allowDangerousHTML: true,
}),
}

// Use Bluebird's Promise function "each" to run remark plugins serially.
await Promise.each(pluginOptions.plugins, plugin => {
const requiredPlugin = require(plugin.resolve)
Expand All @@ -181,10 +196,7 @@ module.exports = (
reporter,
cache: getCache(plugin.name),
getCache,
compiler: {
parseString: remark.parse.bind(remark),
generateHTML: getHTML,
},
compiler,
...rest,
},
plugin.pluginOptions
Expand All @@ -193,7 +205,7 @@ module.exports = (
return Promise.resolve()
}
})
const markdownAST = remark.parse(markdownNode.internal.content)
const markdownAST = remark.parse(content)

if (basePath) {
// Ensure relative links include `pathPrefix`
Expand Down Expand Up @@ -232,10 +244,7 @@ module.exports = (
reporter,
cache: getCache(plugin.name),
getCache,
compiler: {
parseString: remark.parse.bind(remark),
generateHTML: getHTML,
},
compiler,
...rest,
},
plugin.pluginOptions
Expand Down Expand Up @@ -341,9 +350,7 @@ module.exports = (
}

async function getHTML(markdownNode) {
const shouldCache = markdownNode && markdownNode.internal
const cachedHTML =
shouldCache && (await cache.get(htmlCacheKey(markdownNode)))
const cachedHTML = await cache.get(htmlCacheKey(markdownNode))
if (cachedHTML) {
return cachedHTML
} else {
Expand All @@ -353,10 +360,8 @@ module.exports = (
allowDangerousHTML: true,
})

if (shouldCache) {
// Save new HTML to cache
cache.set(htmlCacheKey(markdownNode), html)
}
// Save new HTML to cache
cache.set(htmlCacheKey(markdownNode), html)

return html
}
Expand Down Expand Up @@ -437,7 +442,10 @@ module.exports = (
if (excerptSeparator && markdownNode.excerpt !== ``) {
return markdownNode.excerpt
}
const ast = await getMarkdownAST(markdownNode)
const ast = await getMarkdownAST(
markdownNode.internal.content,
markdownNode
)
const excerptAST = await getExcerptAst(ast, markdownNode, {
pruneLength,
truncate,
Expand Down