hygraph / gatsby-source-graphcms

The official Gatsby source plugin for GraphCMS projects

Home Page:https://graphcms.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build markdown nodes from markdown fields

ynnoj opened this issue · comments

Currently the source plugin can build markdown nodes for RichText fields from the CMS (for subsequent use with gatsby-plugin-mdx).

We should look to extend this functionality to also work with markdown fields.

This would be a great addition.

Leaving some notes here from our discussion earlier.

  • Currently GraphCMS returns markdown fields as String, so there might be some additional config here.
  • To prevent a breaking change in the product, it might be useful to add both remoteTypeName and fieldName to the plugin options.

Having the same problem here, I think we should have a GraphCMS_MarkdownNode existing on the base schema

Any ETA on this to be added in the GraphCMS?

Thanks,

Phil

@phil-lgr Thanks for the feedback! This is something we're reviewing internally, hopefully something to share on this in the near future.

@ynnoj we are kinda stuck on this as we were planning to heavily use Markdown fields in our apps

If you could offer some guidance on how / where to add the code, I could try to add the feature

Since we don't have a Markdown type coming from the API, we would need the plugin to handle the special case, maybe have the option to mark certain String field to be processed as Markdown node?

Let me know!

I'm thinking the way to proceed with this (at least for the time being) is to extend the schema in your local Gatsby site.

I'll share an example soon.

@phil-lgr I would recommend handling this inside your application gatsby-node.js. You could essentially mimic the code from the source plugin to build markdown nodes for the required fields. Something like:

exports.onCreateNode = async (
  { node, actions: { createNode }, createNodeId },
) => {
  if (node.internal.type === 'GraphCMS_Model') {
    ['markdownField'].map((field) => {
      if (node[field]) {
        const decodedMarkdown = he.decode(node[field])

        const markdownNode = {
          id: `MarkdownNode:${createNodeId(`${node.id}-${field}`)}`,
          parent: node.id,
          internal: {
            type: `YourApp_MarkdownNode`,
            mediaType: 'text/markdown',
            content: decodedMarkdown,
            contentDigest: crypto
              .createHash(`md5`)
              .update(decodedMarkdown)
              .digest(`hex`),
          },
        }

        createNode(markdownNode)

        node[`${field}Node`] = markdownNode.id
      }
    })
  }
}

We'd like to add first-class support for nodes from markdown fields to the plugin, however it would add some considerable overhead to the configuration without a Markdown type from the CMS.

Hope this helps!

Thanks it does! I think we can close this one for now

Will keep it open as something we'd like to handle in future.

Closing this. We'll reopen if anything here changes in the future.