Gatsby: feat(gatsby-transformer-remark): Add a filter to preprocess raw content

Created on 18 Jul 2019  路  3Comments  路  Source: gatsbyjs/gatsby

Summary

Add a way to pre-process content that then will be processed by gatsby-transformer-remark.

Basic example

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

Motivation

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

question or discussion

Most helpful comment

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']
  }
}

All 3 comments

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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dustinhorton picture dustinhorton  路  3Comments

jimfilippou picture jimfilippou  路  3Comments

rossPatton picture rossPatton  路  3Comments

magicly picture magicly  路  3Comments

brandonmp picture brandonmp  路  3Comments