hygraph / gatsby-source-graphcms

The official Gatsby source plugin for GraphCMS projects
https://graphcms.com
MIT License
145 stars 41 forks source link

Build markdown nodes from markdown fields #110

Closed ynnoj closed 2 years ago

ynnoj commented 4 years ago

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.

notrab commented 4 years ago

This would be a great addition.

Leaving some notes here from our discussion earlier.

phil-lgr commented 3 years ago

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

ynnoj commented 3 years ago

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

phil-lgr commented 3 years ago

@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!

ynnoj commented 3 years ago

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.

ynnoj commented 3 years ago

@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!

phil-lgr commented 3 years ago

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

ynnoj commented 3 years ago

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

notrab commented 2 years ago

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