Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
Include frontMatter in extendFrontMatter callback
Browse files Browse the repository at this point in the history
  • Loading branch information
leebyron authored and jescalan committed Dec 12, 2019
1 parent 22f48c9 commit e9a5c0f
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ module.exports = withMdxEnhanced({
remarkPlugins: [],
rehypePlugins: [],
extendFrontMatter: {
process: mdxContent => {},
process: (mdxContent, frontMatter) => {},
phase: 'prebuild|loader|both'
}
})(/* your normal nextjs config */)
Expand Down Expand Up @@ -103,7 +103,7 @@ Array of [rehype plugins](https://mdxjs.com/advanced/plugins#using-remark-and-re
| Property | Type | Description |
| --------- | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `process` | `function` | A hook function whose return value will be appended to the processed front matter. This function is given access to the source `.mdx` content as the first parameter. |
| `process` | `function` | A hook function whose return value will be appended to the processed front matter. This function is given access to the source `.mdx` content as the first parameter and the processed front matter as the second parameter. |
| `phase` | `string` | Used to specify when to run the `process` function. Eligible values are `prebuild`, `loader`, `both`. Defaults to `both` if not specified. |

### scan
Expand Down
3 changes: 3 additions & 0 deletions __tests__/fixtures/extend-frontmatter/layouts/docs-page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export default frontMatter => {
<>
<p>LAYOUT TEMPLATE</p>
<h1>{frontMatter.title}</h1>
<p>
All frontMatter: {JSON.stringify(frontMatter)}
</p>
<p>
Other docs: {introData.title}, {advancedData.title}
</p>
Expand Down
8 changes: 5 additions & 3 deletions __tests__/fixtures/extend-frontmatter/next.config.async.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ const withMdxEnhanced = require('../../..')

module.exports = withMdxEnhanced({
extendFrontMatter: {
process: mdxContent => {
process: (mdxContent, frontMatter) => {
return new Promise(resolve =>
setTimeout(() => {
return resolve({ __async: 'this data is async' })
return resolve({
__async: 'this data is async',
reversePath: frontMatter.__resourcePath.split('').reverse().join('')
})
}, 50)
)
},
phase: 'prebuild',
},
})()
3 changes: 2 additions & 1 deletion __tests__/fixtures/extend-frontmatter/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ const withMdxEnhanced = require('../../..')

module.exports = withMdxEnhanced({
extendFrontMatter: {
process: mdxContent => {
process: (mdxContent, frontMatter) => {
return {
__outline: 'outline stuff',
reversePath: frontMatter.__resourcePath.split('').reverse().join('')
}
},
},
Expand Down
2 changes: 2 additions & 0 deletions __tests__/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ describe('options.extendFrontMatter', () => {
const extendFmFixture = path.join(__dirname, 'fixtures/extend-frontmatter')
const outPath = await compileNextjs(extendFmFixture)
expectContentMatch(outPath, 'index.html', /Hello world/)
expectContentMatch(outPath, 'docs/intro.html', /ortni\/scod/)
})

it('should work with an async process fn', async () => {
const extendFmFixture = path.join(__dirname, 'fixtures/extend-frontmatter')
const outPath = await compileNextjs(extendFmFixture, 'next.config.async.js')
expectContentMatch(outPath, 'index.html', /Hello world/)
expectContentMatch(outPath, 'docs/intro.html', /ortni\/scod/)
})
})

Expand Down
22 changes: 14 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +105,29 @@ async function extractFrontMatter(pluginOptions, files, root) {
debug('start: frontmatter extensions')
const frontMatter = await Promise.all(
fileContents.map(async (content, idx) => {
const extendedFm = await extendFrontMatter({
content,
phase: 'prebuild',
extendFm: pluginOptions.extendFrontMatter
})
const __resourcePath = files[idx]
.replace(path.join(root, 'pages'), '')
.substring(1)

const { data } = matter(content, {
safeLoad: true,
filename: files[idx]
})

const extendedFm = await extendFrontMatter({
content,
frontMatter: {
...data,
__resourcePath
},
phase: 'prebuild',
extendFm: pluginOptions.extendFrontMatter
})

return {
...data,
...extendedFm,
__resourcePath: files[idx]
.replace(path.join(root, 'pages'), '')
.substring(1)
__resourcePath
}
})
).catch(console.error)
Expand Down
5 changes: 5 additions & 0 deletions loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ async function processLayout(options, frontMatter, content, resourcePath, scans)

const extendedFm = await extendFrontMatter({
content,
frontMatter: {
...frontMatter,
__resourcePath: resourcePath,
__scans: scans
},
phase: 'loader',
extendFm: pluginOpts.extendFrontMatter
})
Expand Down
3 changes: 2 additions & 1 deletion util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ const crypto = require('crypto')

async function extendFrontMatter({
content,
frontMatter,
phase,
extendFm
} = {}) {
if (!extendFm || !extendFm.process) return {}
if (extendFm.phase !== 'both' && extendFm.phase !== phase) return {}

return extendFm.process(content)
return extendFm.process(content, frontMatter)
}
module.exports.extendFrontMatter = extendFrontMatter

Expand Down

0 comments on commit e9a5c0f

Please sign in to comment.