Skip to content

Commit 6844673

Browse files
committedMar 22, 2025··
Use unist-util-mdx-define
Since this has options to handle naming conflicts, we now also support these options.
1 parent 88c7b21 commit 6844673

4 files changed

+95
-113
lines changed
 

‎package-lock.json

+87-64
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535
},
3636
"dependencies": {
3737
"@types/mdast": "^4.0.0",
38-
"estree-util-is-identifier-name": "^3.0.0",
3938
"estree-util-value-to-estree": "^3.0.0",
4039
"toml": "^3.0.0",
4140
"unified": "^11.0.0",
41+
"unist-util-mdx-define": "^1.0.0",
4242
"yaml": "^2.0.0"
4343
},
4444
"devDependencies": {

‎src/remark-mdx-frontmatter.test.ts

+1-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import assert from 'node:assert/strict'
21
import { test } from 'node:test'
32

4-
import { compile, compileSync } from '@mdx-js/mdx'
3+
import { compile } from '@mdx-js/mdx'
54
import remarkFrontmatter from 'remark-frontmatter'
65
import remarkMdxFrontmatter from 'remark-mdx-frontmatter'
76
import { assertEqual, testFixturesDirectory } from 'snapshot-fixtures'
@@ -49,14 +48,3 @@ export default function MDXContent(props = {}) {
4948
`
5049
)
5150
})
52-
53-
test('invalid name', () => {
54-
assert.throws(
55-
() =>
56-
compileSync('---\n\n---\n', {
57-
remarkPlugins: [remarkFrontmatter, [remarkMdxFrontmatter, { name: 'Not valid' }]],
58-
jsx: true
59-
}),
60-
/Name should be a valid identifier, got: "Not valid"/
61-
)
62-
})

‎src/remark-mdx-frontmatter.ts

+6-35
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { name as isIdentifierName } from 'estree-util-is-identifier-name'
21
import { valueToEstree } from 'estree-util-value-to-estree'
32
import { type Literal, type Root } from 'mdast'
43
import { parse as parseToml } from 'toml'
54
import { type Plugin } from 'unified'
5+
import { define } from 'unist-util-mdx-define'
66
import { parse as parseYaml } from 'yaml'
77

88
type FrontmatterParsers = Record<string, (value: string) => unknown>
99

10-
export interface RemarkMdxFrontmatterOptions {
10+
export interface RemarkMdxFrontmatterOptions extends define.Options {
1111
/**
1212
* If specified, the YAML data is exported using this name. Otherwise, each
1313
* object key will be used as an export name.
@@ -34,19 +34,16 @@ export interface RemarkMdxFrontmatterOptions {
3434
*/
3535
const remarkMdxFrontmatter: Plugin<[RemarkMdxFrontmatterOptions?], Root> = ({
3636
name = 'frontmatter',
37-
parsers
37+
parsers,
38+
...options
3839
} = {}) => {
39-
if (!isIdentifierName(name)) {
40-
throw new Error(`Name should be a valid identifier, got: ${JSON.stringify(name)}`)
41-
}
42-
4340
const allParsers: FrontmatterParsers = {
4441
yaml: parseYaml,
4542
toml: parseToml,
4643
...parsers
4744
}
4845

49-
return (ast) => {
46+
return (ast, file) => {
5047
let data: unknown
5148
const node = ast.children.find((child) => Object.hasOwn(allParsers, child.type))
5249

@@ -57,33 +54,7 @@ const remarkMdxFrontmatter: Plugin<[RemarkMdxFrontmatterOptions?], Root> = ({
5754
data = parser(value)
5855
}
5956

60-
ast.children.unshift({
61-
type: 'mdxjsEsm',
62-
value: '',
63-
data: {
64-
estree: {
65-
type: 'Program',
66-
sourceType: 'module',
67-
body: [
68-
{
69-
type: 'ExportNamedDeclaration',
70-
specifiers: [],
71-
declaration: {
72-
type: 'VariableDeclaration',
73-
kind: 'const',
74-
declarations: [
75-
{
76-
type: 'VariableDeclarator',
77-
id: { type: 'Identifier', name },
78-
init: valueToEstree(data, { preserveReferences: true })
79-
}
80-
]
81-
}
82-
}
83-
]
84-
}
85-
}
86-
})
57+
define(ast, file, { [name]: valueToEstree(data, { preserveReferences: true }) }, options)
8758
}
8859
}
8960

0 commit comments

Comments
 (0)
Please sign in to comment.