Gatsby: [v2] [gatsby-transformer-remark] Generate fields that don't take any args using onCreateNode so they can be accessed by other plugins.

Created on 5 Jul 2018  Â·  4Comments  Â·  Source: gatsbyjs/gatsby

Summary

Currently, gatsby-transformer-remark adds several fields using setFieldsOnGraphQLNodeType. If these fields do not take any args, it would be better to add them using onCreateNode in order to make them available to other plugins.

In particular, the fields that could (should?) be moved are:

  • html
  • htmlAst
  • tableOfContents
  • wordCount

Basic example

The following, untested, pseudo-code should give you the gist.

Current Behaviour

// hello.md
# Hello World

// gatsby-node.js 
exports.onCreateNode = ({ node }) => {
  if(node.internal.type === 'MarkdownRemark') {
    console.log(node.html) // undefined
  }
}

Suggested Behaviour

// hello.md
# Hello World

// gatsby-node.js 
exports.onCreateNode = ({ node }) => {
  if(node.internal.type === 'MarkdownRemark') {
    console.log(node.html) // <h1>Hello World</h1>
  }
}

Motivation

This would allow plugins (including local use of gatsby-node.js) to access, use, and potentially extend the fields in question via onCreateNode.

This would also align gatsby-transformer-remark's behaviour with other plugins.

help wanted

Most helpful comment

This issue was create at @KyleAMathews request. Based on a Conversation on Discord.

Ohhh, ok let me label this up as a feature then :D

All 4 comments

@KyleAMathews do you have any thoughts on this?

This issue was created at @KyleAMathews request. Based on a Conversation on Discord. See quotes below...

Kyle: html/excerpt are generated in response to queries — arguably we shouldn't do that for html but it makes sense for excerpt as the user can pass an arg for the excerpt length, etc.

Me: Thanks for the quick reply. Yes ... "html" was the big one. I was surprised that I couldn't access it from onCreateNode. I can live without excerpt and some of the other fields that take params.

Kyle: yeah... _we should probably change it_ — could you file an issue about it? It'd be a breaking change kinda so we should probably do it for v2

This issue was create at @KyleAMathews request. Based on a Conversation on Discord.

Ohhh, ok let me label this up as a feature then :D

Due to the high volume of issues, we're closing out older ones without recent activity. Please open a new issue if you need help!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jimfilippou picture jimfilippou  Â·  3Comments

Oppenheimer1 picture Oppenheimer1  Â·  3Comments

totsteps picture totsteps  Â·  3Comments

signalwerk picture signalwerk  Â·  3Comments

timbrandin picture timbrandin  Â·  3Comments