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

Adds error message with filename on Markdown error, fixes bug in panicOnBuild #8866

Merged
merged 5 commits into from
Oct 9, 2018
2 changes: 1 addition & 1 deletion packages/gatsby-cli/src/reporter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module.exports = Object.assign(reporter, {

panicOnBuild(...args) {
this.error(...args)
if (process.env.gatsby_executing_command !== `build`) {
if (process.env.gatsby_executing_command === `build`) {
process.exit(1)
}
},
Expand Down
87 changes: 48 additions & 39 deletions packages/gatsby-transformer-remark/src/on-node-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const crypto = require(`crypto`)
const _ = require(`lodash`)

module.exports = async function onCreateNode(
{ node, getNode, loadNodeContent, actions, createNodeId },
{ node, getNode, loadNodeContent, actions, createNodeId, reporter },
pluginOptions
) {
const { createNode, createParentChildLink } = actions
Expand All @@ -17,49 +17,58 @@ module.exports = async function onCreateNode(
}

const content = await loadNodeContent(node)
let data = grayMatter(content, pluginOptions)

// Convert date objects to string. Otherwise there's type mismatches
// during inference as some dates are strings and others date objects.
if (data.data) {
data.data = _.mapValues(data.data, v => {
if (_.isDate(v)) {
return v.toJSON()
} else {
return v
}
})
}
try {
let data = grayMatter(content, pluginOptions)
// Convert date objects to string. Otherwise there's type mismatches
// during inference as some dates are strings and others date objects.
if (data.data) {
data.data = _.mapValues(data.data, v => {
if (_.isDate(v)) {
return v.toJSON()
} else {
return v
}
})
}

const markdownNode = {
id: createNodeId(`${node.id} >>> MarkdownRemark`),
children: [],
parent: node.id,
internal: {
content: data.content,
type: `MarkdownRemark`,
},
}
const markdownNode = {
id: createNodeId(`${node.id} >>> MarkdownRemark`),
children: [],
parent: node.id,
internal: {
content: data.content,
type: `MarkdownRemark`,
},
}

markdownNode.frontmatter = {
title: ``, // always include a title
...data.data,
_PARENT: node.id,
}
markdownNode.frontmatter = {
title: ``, // always include a title
...data.data,
_PARENT: node.id,
}

markdownNode.excerpt = data.excerpt
markdownNode.rawMarkdownBody = data.content
markdownNode.excerpt = data.excerpt
markdownNode.rawMarkdownBody = data.content

// Add path to the markdown file path
if (node.internal.type === `File`) {
markdownNode.fileAbsolutePath = node.absolutePath
}
// Add path to the markdown file path
if (node.internal.type === `File`) {
markdownNode.fileAbsolutePath = node.absolutePath
}

markdownNode.internal.contentDigest = crypto
.createHash(`md5`)
.update(JSON.stringify(markdownNode))
.digest(`hex`)
markdownNode.internal.contentDigest = crypto
.createHash(`md5`)
.update(JSON.stringify(markdownNode))
.digest(`hex`)

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