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

[gatsby-transformer-remark] support frontmatter default values plugin option #5495

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/gatsby-transformer-remark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,19 @@ Parses Markdown files using [Remark](http://remark.js.org/).

```javascript
// In your gatsby-config.js
plugins: [`gatsby-transformer-remark`];
plugins: [
{
resolve: `gatsby-transformer-remark`,
options: {
// define default values for frontmatter variables here (optional)
frontmatter: {
defaultValues: {
category: `Uncategorised`
}
}
}
}
];
```

A full explanation of how to use markdown in Gatsby can be found here:
Expand Down
9 changes: 8 additions & 1 deletion packages/gatsby-transformer-remark/src/on-node-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const _ = require(`lodash`)

module.exports = async function onCreateNode(
{ node, getNode, loadNodeContent, boundActionCreators },
pluginOptions
pluginOptions = {}
) {
const { createNode, createParentChildLink } = boundActionCreators

Expand All @@ -18,6 +18,13 @@ module.exports = async function onCreateNode(

const content = await loadNodeContent(node)
let data = grayMatter(content, pluginOptions)
const frontmatterDefaults = pluginOptions.frontmatter && pluginOptions.frontmatter.defaultValues
if(frontmatterDefaults) {
const defaultKeys = _.keys(frontmatterDefaults)
for(const key of defaultKeys) {
data.data[key] = data.data[key] || frontmatterDefaults[key]
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

How about using lodash _.merge here? It would be less verbose and it would allow deep merge (which imo would be useful because current implementation just take care of first level frontmatter fields )


// Convert date objects to string. Otherwise there's type mismatches
// during inference as some dates are strings and others date objects.
Expand Down