Skip to content

Commit

Permalink
fix(gatsby-transformer-remark): restore behavior of serializing date-…
Browse files Browse the repository at this point in the history
…like fields to string (#11716)

* chore: commit

* chore: get tests failing (intentionally) to then fix

* chore: de-dry

* chore: finish tests

* chore: revert relative import
  • Loading branch information
DSchau committed Feb 12, 2019
1 parent 128b156 commit 29dee3f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 31 deletions.
@@ -1,7 +1,7 @@
const Promise = require(`bluebird`)
const _ = require(`lodash`)

const { onCreateNode } = require(`../gatsby-node`)
const onCreateNode = require(`../on-node-create`)

const {
graphql,
Expand All @@ -13,8 +13,11 @@ const {
inferObjectStructureFromNodes,
} = require(`../../../gatsby/src/schema/infer-graphql-type`)

describe(`Process markdown content correctly`, () => {
const node = {
let node
let actions
let createNodeId
beforeEach(() => {
node = {
id: `whatever`,
children: [],
internal: {
Expand All @@ -23,9 +26,13 @@ describe(`Process markdown content correctly`, () => {
},
}

// Make some fake functions its expecting.
const loadNodeContent = node => Promise.resolve(node.content)
actions = { createNode: jest.fn(), createParentChildLink: jest.fn() }
createNodeId = jest.fn().mockReturnValue(`uuid-from-gatsby`)
})

const loadNodeContent = mdNode => Promise.resolve(mdNode.content)

describe(`Process markdown content correctly`, () => {
describe(`Process generated markdown node correctly`, () => {
it(`Correctly creates a new MarkdownRemark node`, async () => {
const content = `---
Expand All @@ -36,25 +43,19 @@ Where oh where is my little pony?
`
node.content = content

const createNode = jest.fn()
const createParentChildLink = jest.fn()
const actions = { createNode, createParentChildLink }
const createNodeId = jest.fn()
createNodeId.mockReturnValue(`uuid-from-gatsby`)

await onCreateNode({
node,
loadNodeContent,
actions,
createNodeId,
}).then(() => {
expect(createNode.mock.calls).toMatchSnapshot()
expect(actions.createNode.mock.calls).toMatchSnapshot()
expect(
_.isString(createNode.mock.calls[0][0].frontmatter.date)
_.isString(actions.createNode.mock.calls[0][0].frontmatter.date)
).toBeTruthy()
expect(createParentChildLink.mock.calls).toMatchSnapshot()
expect(createNode).toHaveBeenCalledTimes(1)
expect(createParentChildLink).toHaveBeenCalledTimes(1)
expect(actions.createParentChildLink.mock.calls).toMatchSnapshot()
expect(actions.createNode).toHaveBeenCalledTimes(1)
expect(actions.createParentChildLink).toHaveBeenCalledTimes(1)
})
})

Expand All @@ -78,12 +79,6 @@ Sed bibendum sem iaculis, pellentesque leo sed, imperdiet ante. Sed consequat ma

node.content = content

const createNode = jest.fn()
const createParentChildLink = jest.fn()
const actions = { createNode, createParentChildLink }
const createNodeId = jest.fn()
createNodeId.mockReturnValue(`uuid-from-gatsby`)

await onCreateNode(
{
node,
Expand All @@ -93,13 +88,37 @@ Sed bibendum sem iaculis, pellentesque leo sed, imperdiet ante. Sed consequat ma
},
{ excerpt_separator: `<!-- end -->` }
).then(() => {
expect(createNode.mock.calls).toMatchSnapshot()
expect(_.isString(createNode.mock.calls[0][0].excerpt)).toBeTruthy()
expect(createNode.mock.calls[0][0].excerpt).not.toEqual(0)
expect(createParentChildLink.mock.calls).toMatchSnapshot()
expect(createNode).toHaveBeenCalledTimes(1)
expect(createParentChildLink).toHaveBeenCalledTimes(1)
expect(actions.createNode.mock.calls).toMatchSnapshot()
expect(
_.isString(actions.createNode.mock.calls[0][0].excerpt)
).toBeTruthy()
expect(actions.createNode.mock.calls[0][0].excerpt).not.toEqual(0)
expect(actions.createParentChildLink.mock.calls).toMatchSnapshot()
expect(actions.createNode).toHaveBeenCalledTimes(1)
expect(actions.createParentChildLink).toHaveBeenCalledTimes(1)
})
})
})

describe(`date formatting`, () => {
const getContent = date => `---
date: ${date}
---
yadda yadda
`

it(`coerces a date object into a date string`, async () => {
const date = `2019-01-01`
node.content = getContent(date)
const parsed = await onCreateNode({
node,
actions,
createNodeId,
loadNodeContent,
})

expect(parsed.frontmatter.date).toEqual(new Date(date).toJSON())
})
})

Expand Down
20 changes: 17 additions & 3 deletions packages/gatsby-transformer-remark/src/on-node-create.js
@@ -1,5 +1,6 @@
const grayMatter = require(`gray-matter`)
const crypto = require(`crypto`)
const _ = require(`lodash`)

module.exports = async function onCreateNode(
{ node, loadNodeContent, actions, createNodeId, reporter },
Expand All @@ -12,15 +13,24 @@ module.exports = async function onCreateNode(
node.internal.mediaType !== `text/markdown` &&
node.internal.mediaType !== `text/x-markdown`
) {
return
return {}
}

const content = await loadNodeContent(node)

try {
const data = grayMatter(content, pluginOptions)
let data = grayMatter(content, pluginOptions)

const markdownNode = {
if (data.data) {
data.data = _.mapValues(data.data, value => {
if (_.isDate(value)) {
return value.toJSON()
}
return value
})
}

let markdownNode = {
id: createNodeId(`${node.id} >>> MarkdownRemark`),
children: [],
parent: node.id,
Expand Down Expand Up @@ -50,12 +60,16 @@ module.exports = async function onCreateNode(

createNode(markdownNode)
createParentChildLink({ parent: node, child: markdownNode })

return markdownNode
} catch (err) {
reporter.panicOnBuild(
`Error processing Markdown ${
node.absolutePath ? `file ${node.absolutePath}` : `in node ${node.id}`
}:\n
${err.message}`
)

return {} // eslint
}
}

0 comments on commit 29dee3f

Please sign in to comment.