1

NetlifyCMS offers a markdown editor that allows the insertion of markdown (and code blocks) within frontmatter.

The resulting markdown file might have content like:

---
featureSubtitle: |-
  ![fdsaf](/img/cdc-w9keokhajkw-unsplash.jpg "blah")

  # Markdown H1

  ## Markdown H2


  <h1>Test</h1>
  <p>Paragraph</p>
---

This is then loaded onto the page from frontmatter via a graphql query like:

export const pageQuery = graphql`
  query FeaturePageByID($id: String!) {
    markdownRemark(id: { eq: $id }) {
      id
      html
      frontmatter {
        title
        description
        featureSubtitle
      }
    }
  }
`;

and

{featureSubtitle && featureSubtitle ? (
                  <div
                    dangerouslySetInnerHTML={{ __html: featureSubtitle }}
                  />
                ) : null}

This however appears to render the HTML correctly, but not the markdown.

enter image description here

larpo
  • 1,073
  • 1
  • 10
  • 18

1 Answers1

2

The gatsby transformer remark plugin does convert markdown to html in the body of your markdown file and gets the frontmatter data at the top of your markdown file. The frontmatter is used mainly for metadata. You can however convert the frontmatter field yourself to html. See this link.

Or make sure you use the body for the markdown field like this in your config.yml for netlify cms:

fields:
      - { label: "featureSubtitle", name: "body", widget: "markdown" }

Then it can be retrieved as html with the html field in your graphql query and be used like this:

<div dangerouslySetInnerHTML={{ __html: html}} />
A. van Hugten
  • 715
  • 5
  • 16
  • the link in the thread had a solution that was perfect for my needs. Thanks for that. – larpo Apr 02 '20 at 17:06
  • can you actually expand on how this is achieved? That github issue link isn't really clear on what works with the latest version on Gatsby. – Edward Apr 15 '22 at 18:03