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

fix(gatsby-transformer-remark): restore behavior of serializing date-like fields to string #11716

Merged
merged 6 commits into from
Feb 12, 2019
Original file line number Diff line number Diff line change
@@ -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(() => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some light refactoring here to make this easier to use in the future

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() }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we do:

let markdownNode
beforeEach(() => { markdownNode = null})
createNode: jest.fn(markdownNode => createNode = node)

to avoid those returns in source? return {} is very confusing to read (without looking at tests)

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`, () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth mentioning here (maybe in comment) that this is only needed because there is bug in gatsby core that doesn't really allow sorting mix of date object and strings?

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
Original file line number Diff line number Diff line change
@@ -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
m-allanson marked this conversation as resolved.
Show resolved Hide resolved
} catch (err) {
reporter.panicOnBuild(
`Error processing Markdown ${
node.absolutePath ? `file ${node.absolutePath}` : `in node ${node.id}`
}:\n
${err.message}`
)

return {} // eslint
}
}