Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gatsby) : add createContentDigest helper #8687

Merged
merged 7 commits into from
Oct 2, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Array [
"content": "",
"id": "bk101",
"internal": Object {
"contentDigest": "4fa0c7a57664d1566dba2f85176f62cf",
"contentDigest": "contentDigest",
"type": "NodeNameXml",
},
"name": "book",
Expand Down Expand Up @@ -66,7 +66,7 @@ Array [
"content": "",
"id": "bk102",
"internal": Object {
"contentDigest": "d01ca541dbe1148c59883a673342df9b",
"contentDigest": "contentDigest",
"type": "NodeNameXml",
},
"name": "book",
Expand Down Expand Up @@ -128,7 +128,7 @@ Array [
"content": "",
"id": "bk101",
"internal": Object {
"contentDigest": "4fa0c7a57664d1566dba2f85176f62cf",
"contentDigest": "contentDigest",
"type": "NodeNameXml",
},
"name": "book",
Expand Down Expand Up @@ -220,7 +220,7 @@ Array [
"content": "",
"id": "bk102",
"internal": Object {
"contentDigest": "d01ca541dbe1148c59883a673342df9b",
"contentDigest": "contentDigest",
"type": "NodeNameXml",
},
"name": "book",
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby-transformer-xml/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ describe(`Process XML nodes correctly`, () => {
const actions = { createNode, createParentChildLink }
const createNodeId = jest.fn()
createNodeId.mockReturnValue(`uuid-from-gatsby`)
const createContentDigest = jest.fn().mockReturnValue(`contentDigest`)

await onCreateNode({
node,
loadNodeContent,
actions,
createNodeId,
createContentDigest,
}).then(() => {
expect(createNode.mock.calls).toMatchSnapshot()
expect(createParentChildLink.mock.calls).toMatchSnapshot()
Expand Down
9 changes: 2 additions & 7 deletions packages/gatsby-transformer-xml/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const parseXml = require(`xml-parser`)
const crypto = require(`crypto`)
rbadr marked this conversation as resolved.
Show resolved Hide resolved
const _ = require(`lodash`)

async function onCreateNode({ node, actions, loadNodeContent, createNodeId }) {
async function onCreateNode({ node, actions, loadNodeContent, createNodeId, createContentDigest }) {
const { createNode, createParentChildLink } = actions

// We only care about XML content.
Expand All @@ -12,11 +12,6 @@ async function onCreateNode({ node, actions, loadNodeContent, createNodeId }) {
const rawXml = await loadNodeContent(node)
const parsedXml = parseXml(rawXml)
const nodeArray = parsedXml.root.children.map((obj, i) => {
const objStr = JSON.stringify(obj)
const contentDigest = crypto
.createHash(`md5`)
.update(objStr)
.digest(`hex`)
if (obj.children) {
obj.xmlChildren = obj.children
delete obj.children
Expand All @@ -29,7 +24,7 @@ async function onCreateNode({ node, actions, loadNodeContent, createNodeId }) {
parent: node.id,
children: [],
internal: {
contentDigest,
contentDigest: createContentDigest(obj),
type: _.upperFirst(_.camelCase(`${node.name} xml`)),
},
}
Expand Down