Skip to content

Commit

Permalink
fix: Decode markdown field returned from GraphCMS (#99)
Browse files Browse the repository at this point in the history
* chore(deps): Add he dependency

* fix: Decode markdown fields from GraphCMS

HTML characters are escaped in JSON response, can't render components from CMS
  • Loading branch information
Jonathan Steele authored Aug 6, 2020
1 parent fb77241 commit 40400db
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
7 changes: 5 additions & 2 deletions gatsby-source-graphcms/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {
sourceNodeChanges,
} = require('gatsby-graphql-source-toolkit')
const { createRemoteFileNode } = require('gatsby-source-filesystem')
const he = require('he')
const fetch = require('node-fetch')

exports.onPreBootstrap = ({ reporter }, pluginOptions) => {
Expand Down Expand Up @@ -149,16 +150,18 @@ exports.onCreateNode = async (

if (fields.length) {
fields.forEach((field) => {
const decodedMarkdown = he.decode(field.markdown)

const markdownNode = {
id: `MarkdownNode:${createNodeId(node.id)}`,
parent: node.id,
internal: {
type: `GraphCMS_MarkdownNode`,
mediaType: 'text/markdown',
content: field.markdown,
content: decodedMarkdown,
contentDigest: crypto
.createHash(`md5`)
.update(field.markdown)
.update(decodedMarkdown)
.digest(`hex`),
},
}
Expand Down
1 change: 1 addition & 0 deletions gatsby-source-graphcms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"dependencies": {
"gatsby-graphql-source-toolkit": "0.2.2",
"gatsby-source-filesystem": "2.3.18",
"he": "1.2.0",
"node-fetch": "2.6.0"
}
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7179,6 +7179,11 @@ hastscript@^5.0.0:
property-information "^5.0.0"
space-separated-tokens "^1.0.0"

he@1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/he/-/he-1.2.0.tgz#84ae65fa7eafb165fddb61566ae14baf05664f0f"
integrity sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==

header-case@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/header-case/-/header-case-1.0.1.tgz#9535973197c144b09613cd65d317ef19963bd02d"
Expand Down

0 comments on commit 40400db

Please sign in to comment.