Skip to content

Commit e2614fd

Browse files
authored
feat: add grayMatterOptions (#75)
* feat: add `grayMatterOptions` * docs: update the docs * tests: remove optional chaining for node 12 * tests: use c8 to generate coverage
1 parent ce60b58 commit e2614fd

File tree

5 files changed

+82
-5
lines changed

5 files changed

+82
-5
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,33 @@ const result = await bundleMDX(mdxSource, {
451451
const {code, frontmatter} = result
452452
```
453453

454+
#### grayMatterOptions
455+
456+
This allows you to configure the
457+
[gray-matter options](https://github.com/jonschlinkert/gray-matter#options).
458+
459+
Your function is passed the current gray-matter configuration for you to modify.
460+
Return your modified configuration object for gray matter.
461+
462+
```js
463+
bundleMDX(mdxString, {
464+
grayMatterOptions: options => {
465+
options.excerpt = true
466+
467+
return options
468+
},
469+
})
470+
```
471+
472+
### Returns
473+
474+
`bundleMDX` returns a promise for an object with the following properties.
475+
476+
- `code` - The bundle of your mdx as a `string`.
477+
- `frontmatter` - The frontmatter `object` from gray-matter.
478+
- `matter` - The whole
479+
[object returned by gray-matter](https://github.com/jonschlinkert/gray-matter#returned-object)
480+
454481
### Component Substitution
455482

456483
MDX Bundler passes on

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"postbuild": "node ./other/cjs-ify.js",
3636
"lint": "kcd-scripts lint",
3737
"setup": "npm install && npm run validate -s",
38-
"test": "uvu -i setup-tests.js src/__tests__",
38+
"test": "c8 -r text -r lcov uvu -i setup-tests.js src/__tests__",
3939
"typecheck": "kcd-scripts typecheck",
4040
"validate": "kcd-scripts validate"
4141
},
@@ -58,6 +58,7 @@
5858
"@types/react": "^17.0.14",
5959
"@types/react-dom": "^17.0.9",
6060
"@types/uuid": "^8.3.1",
61+
"c8": "^7.8.0",
6162
"cross-env": "^7.0.3",
6263
"esbuild": "^0.12.15",
6364
"jsdom": "^16.6.0",

src/__tests__/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,4 +474,30 @@ export default Demo
474474
assert.match(container.innerHTML, 'Portal!')
475475
})
476476

477+
test('should allow gray matter options to be accessed', async () => {
478+
const mdxSource = `
479+
---
480+
title: Sample
481+
date: 2021-07-27
482+
---
483+
484+
Some excerpt
485+
486+
---
487+
488+
This is the rest of the content
489+
490+
`.trim()
491+
492+
const {matter} = await bundleMDX(mdxSource, {
493+
grayMatterOptions: options => {
494+
options.excerpt = true
495+
496+
return options
497+
},
498+
})
499+
500+
assert.equal((matter.excerpt ? matter.excerpt : '').trim(), 'Some excerpt')
501+
})
502+
477503
test.run()

src/index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'path'
33
import {StringDecoder} from 'string_decoder'
44
import remarkFrontmatter from 'remark-frontmatter'
55
import {remarkMdxFrontmatter} from 'remark-mdx-frontmatter'
6-
import matter from 'gray-matter'
6+
import grayMatter from 'gray-matter'
77
import * as esbuild from 'esbuild'
88
import {NodeResolvePlugin} from '@esbuild-plugins/node-resolve'
99
import {globalExternals} from '@fal-works/esbuild-plugin-global-externals'
@@ -26,21 +26,24 @@ async function bundleMDX(
2626
esbuildOptions = options => options,
2727
globals = {},
2828
cwd = path.join(process.cwd(), `__mdx_bundler_fake_dir__`),
29+
grayMatterOptions = options => options
2930
} = {},
3031
) {
32+
/* c8 ignore start */
3133
if (dirnameMessedUp && !process.env.ESBUILD_BINARY_PATH) {
3234
console.warn(
3335
`mdx-bundler warning: esbuild maybe unable to find its binary, if your build fails you'll need to set ESBUILD_BINARY_PATH. Learn more: https://github.com/kentcdodds/mdx-bundler/blob/main/README.md#nextjs-esbuild-enoent`,
3436
)
3537
}
38+
/* c8 ignore stop */
3639

3740
// xdm is a native ESM, and we're running in a CJS context. This is the
3841
// only way to import ESM within CJS
3942
const [{default: xdmESBuild}] = await Promise.all([
4043
await import('xdm/esbuild.js'),
4144
])
4245
// extract the frontmatter
43-
const {data: frontmatter} = matter(mdxSource)
46+
const matter = grayMatter(mdxSource, grayMatterOptions({}))
4447

4548
const entryPath = path.join(cwd, `./_mdx_bundler_entry_point-${uuid()}.mdx`)
4649

@@ -174,8 +177,9 @@ async function bundleMDX(
174177

175178
return {
176179
code: `${code};return Component.default;`,
177-
frontmatter,
180+
frontmatter: matter.data,
178181
errors: bundled.errors,
182+
matter
179183
}
180184
}
181185

@@ -192,8 +196,9 @@ async function bundleMDX(
192196

193197
return {
194198
code: `${code};return Component.default;`,
195-
frontmatter,
199+
frontmatter: matter.data,
196200
errors: bundled.errors,
201+
matter
197202
}
198203
}
199204

src/types.d.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import type {Plugin, BuildOptions, Loader} from 'esbuild'
88
import type {ModuleInfo} from '@fal-works/esbuild-plugin-global-externals'
99
import type {CoreProcessorOptions} from 'xdm/lib/compile'
10+
import type {GrayMatterOption, Input} from 'gray-matter'
1011

1112
type ESBuildOptions = BuildOptions
1213

@@ -117,4 +118,21 @@ type BundleMDXOptions = {
117118
* ```
118119
*/
119120
cwd?: string
121+
/**
122+
* This allows you to configure the gray matter options.
123+
*
124+
* @example
125+
* ```
126+
* bundleMDX(mdxString, {
127+
* grayMatterOptions: (options) => {
128+
* options.excerpt = true
129+
*
130+
* return options
131+
* }
132+
* })
133+
* ```
134+
*/
135+
grayMatterOptions?: <I extends Input>(
136+
options: GrayMatterOption<I, any>,
137+
) => GrayMatterOption<I, any>
120138
}

0 commit comments

Comments
 (0)