Skip to content

Commit

Permalink
[1.0] [source-contentful] Use field type to determine if it's a 'Text…
Browse files Browse the repository at this point in the history
…' field (#1212)

* An array of linked nodes linking to heterogeneous node types is now converted to a union type

* Use field type to determine if it's a 'Text' field
  • Loading branch information
KyleAMathews authored Jun 20, 2017
1 parent ce300da commit a119564
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/gatsby-source-contentful/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
"bluebird": "^3.4.6",
"contentful": "^4.3.0",
"json-stringify-safe": "^5.0.1",
"lodash": "^4.17.2"
"lodash": "^4.17.4"
}
}
21 changes: 16 additions & 5 deletions packages/gatsby-source-contentful/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const contentful = require(`contentful`)
const crypto = require(`crypto`)
const stringify = require(`json-stringify-safe`)
const _ = require(`lodash`)

const digest = str => crypto.createHash(`md5`).update(str).digest(`hex`)

Expand Down Expand Up @@ -140,14 +141,14 @@ exports.sourceNodes = async (
})
})

function createTextNode(node, text, createNode) {
function createTextNode(node, key, text, createNode) {
const textNode = {
id: `${node.id}TextNode`,
id: `${node.id}${key}TextNode`,
parent: node.id,
children: [],
text,
[key]: text,
internal: {
type: `ComponentDescription`,
type: _.camelCase(`${node.internal.type} ${key} TextNode`),
mediaType: `text/x-markdown`,
content: text,
contentDigest: digest(text),
Expand Down Expand Up @@ -243,9 +244,19 @@ exports.sourceNodes = async (
// Replace text fields with text nodes so we can process their markdown
// into HTML.
Object.keys(entryItemFields).forEach(entryItemFieldKey => {
if (entryItemFieldKey === `text`) {
// Ignore fields with "___node" as they're already handled
// and won't be a text field.
if (entryItemFieldKey.split(`___`).length > 1) {
return
}

const fieldType = contentTypeItem.fields.find(
f => f.id === entryItemFieldKey
).type
if (fieldType === `Text`) {
entryItemFields[`${entryItemFieldKey}___NODE`] = createTextNode(
entryNode,
entryItemFieldKey,
entryItemFields[entryItemFieldKey],
createNode
)
Expand Down

0 comments on commit a119564

Please sign in to comment.