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
Next Next commit
Fixes bug in panicOnBuild and adds better error handling for markdown
  • Loading branch information
mslooten committed Oct 6, 2018
commit 2e6b354b33b1003eb9d1fe008e53bea80fcb7160
3 changes: 2 additions & 1 deletion packages/gatsby-cli/src/reporter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ module.exports = Object.assign(reporter, {

panicOnBuild(...args) {
this.error(...args)
if (process.env.gatsby_executing_command !== `build`) {
console.log(process.env)
if (process.env.gatsby_executing_command === `build`) {
process.exit(1)
}
},
Expand Down
85 changes: 46 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,56 @@ 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 file ${node.absolutePath}:\n
Copy link
Contributor

Choose a reason for hiding this comment

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

I forgot about something earlier - markdown doesn't always come from file, so we will need to check if node.absolutePath is set before using it. For example:

`Error processing Markdown ${node.absolutePath ? `file: ${node.absolutePath}` : ``}:\n`
${err.message}`
)
}
}