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

Support extraction of content on build with onContent function #26

Merged
merged 1 commit into from
Oct 21, 2019
Merged
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ Object of scan objects containing the following parameters

See ["Scanning MDX Content"](#scanning-mdx-content) for more details.

### onContent

> `function` | optional

A function that will run on build for each MDX page. All metadata and full text content are passed to this function as its only argument. Useful for indexing your content for site search or any other purpose where you'd like to capture content on build.

jmfury marked this conversation as resolved.
Show resolved Hide resolved
## Layouts

We can specify a layout for a given `.mdx` file using its front matter, as such:
Expand Down
5 changes: 5 additions & 0 deletions __tests__/fixtures/on-content/layouts/docs-page.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default () => {
return function docsPageLayout({ children }) {
return <>{children}</>
}
}
6 changes: 6 additions & 0 deletions __tests__/fixtures/on-content/next.config-mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const withMdxEnhanced = require('../../..')

module.exports = configFn =>
withMdxEnhanced({
onContent: mdxContent => configFn(mdxContent)
})()
9 changes: 9 additions & 0 deletions __tests__/fixtures/on-content/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const withMdxEnhanced = require('../../..')

module.exports = withMdxEnhanced({
onContent: mdxContent => processContent(mdxContent)
})()

function processContent(content) {
console.log(content)
}
jmfury marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 6 additions & 0 deletions __tests__/fixtures/on-content/pages/docs/advanced.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
layout: 'docs-page'
title: 'Advanced Docs'
---

A piece of content that should trigger a function call for `onContent`
6 changes: 6 additions & 0 deletions __tests__/fixtures/on-content/pages/docs/intro.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
layout: 'docs-page'
title: 'Intro Docs'
---

A piece of content that should trigger a function call for `onContent`
1 change: 1 addition & 0 deletions __tests__/fixtures/on-content/pages/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => <p>Hello world</p>
32 changes: 32 additions & 0 deletions __tests__/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const fs = require('fs')
const rmfr = require('rmfr')
const nextBuild = require('next/dist/build').default
const nextExport = require('next/dist/export').default
const glob = require('glob')

// increase timeout since these are integration tests
jest.setTimeout(20000)
Expand Down Expand Up @@ -70,6 +71,22 @@ test('options.scan', async () => {
)
})

test('options.onContent', async () => {
const onContentFixture = path.join(__dirname, 'fixtures/on-content')
let mdxPageCount = 0
glob('**/**/*.mdx', { cwd: onContentFixture }, (err, files) => {
if (err) throw err
mdxPageCount = files.length
})
const mockCallback = jest.fn(content => console.log(content))
const compile = await compileNextjsWithMockFunction(
onContentFixture,
'next.config-mock.js',
mockCallback
)
expect(compile.mock.calls.length).toBe(mdxPageCount)
})

// Remove artifacts
afterAll(() => {
return Promise.all([
Expand Down Expand Up @@ -98,3 +115,18 @@ function expectContentMatch(outPath, filePath, matcher) {
const content = fs.readFileSync(path.join(outPath, filePath), 'utf8')
return expect(content).toMatch(matcher)
}

function compileNextjsWithMockFunction(
projectPath,
configPath = 'next.config.js',
mockFn
) {
const config = require(path.join(projectPath, configPath))
const outPath = path.join(projectPath, 'out')
return nextBuild(projectPath, config(mockFn)).then(() => {
return nextExport(projectPath, {
outdir: outPath,
silent: true
}).then(() => mockFn)
})
}
12 changes: 11 additions & 1 deletion loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,19 @@ function processLayout(options, frontMatter, content, resourcePath, scans) {
)
}

const { onContent } = pluginOpts
if (onContent && this._compiler.name === 'server') {
onContent({
...frontMatter,
...extendedFm,
...{ __resourcePath: resourcePath },
content
})
}

// Import the layout, export the layout-wrapped content, pass front matter into layout
return resolve(`import layout from '${normalizeToUnixPath(layoutPath)}'

export default layout(${stringifyObject({
...frontMatter,
...extendedFm,
Expand Down