Skip to content

Commit

Permalink
feat: add error message with filename on Markdown error, fix bug in p…
Browse files Browse the repository at this point in the history
…anicOnBuild (#8866)

* Fixes bug in panicOnBuild and adds better error handling for markdown

* Removes console.log

* Only shows path in the error if it's a file

* Moved the word 'file' to the condition

Made the word 'file' conditional as well

* Shows node.id when absolutePath is not available

As discussed in the PR, it might help people to show the node id when no absolutePath is available (e.g. when not using a file to supply the Markdown)
  • Loading branch information
mslooten authored and pieh committed Oct 9, 2018
1 parent 79ab2f3 commit bbff3be
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 40 deletions.
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}`
)
}
}

0 comments on commit bbff3be

Please # to comment.