Add a way to pre-process content that then will be processed by gatsby-transformer-remark.
Currently, gatsby-transformer-remark exposes a way to access the markdown AST, but that is after parsing the text into markdown. There could be an easy way for gatsby-transformer-remark also allow to hook into the content before the AST parsing appears:
const content = await loadNodeContent(node)
// THIS IS WHERE THE NEW EXPOSED API WOULD FIT INTO
// content = applyPreProcessContentFilters(content)
try {
let data = grayMatter(content, pluginOptions)
if (data.data) {
data.data = _.mapValues(data.data, value => {
if (_.isDate(value)) {
return value.toJSON()
}
return value
})
}
Sometimes Markdown is combined with other template languages (i.e. mustache), so pre-processing the raw text is needed to avoid breakages in Markdown parsing.
As an example, we're using certain Mustache tokens to replace them with pricing information and URL links that may change quite often, without the need to actually update the markdown files (that never change). We need to call an external service, and replace that data into the raw text before Markdown parsing starts (otherwise links, for example, are broken, as they get interpreted as linkreferences).
It should be possible to do this with a gatsby-remark plugin that preprocesses the md source by exporting a mutateSource function and manipulating markdownNode.internal.content it seems.
You could do this as a local plugin, i.e.:
// plugins/gatsby-remark-preprocess/index.js
exports.mutateSource = ({ markdownNode }) => {
// Mutate markdownNode.internal.content
}
Make sure to also put a minimal package.json in plugins/gatsby-remark-preprocess and add it as a subplugin:
// gatsby-config.js
{
resolve: 'gatsby-transformer-remark',
options: {
plugins: ['gatsby-remark-preprocess']
}
}
It's seems, simply, perfect. We'll give it a try.
@stefanprobst: works perfectly. Thanks!
Most helpful comment
It should be possible to do this with a gatsby-remark plugin that preprocesses the md source by exporting a
mutateSourcefunction and manipulatingmarkdownNode.internal.contentit seems.You could do this as a local plugin, i.e.:
Make sure to also put a minimal
package.jsoninplugins/gatsby-remark-preprocessand add it as a subplugin: