-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MDX] Support YAML frontmatter (#3995)
* chore: remove old comment * deps: add remark-frontmatter * deps: add remark-mdx-frontmatter * fix: handle null or undefined frontmatter key * feat: configure frontmatter plugins with defaults * test: frontmatter and custom frontmatter name * docs: add frontmatterOptions config * docs: add "variables" and "frontmatter" docs * chore: excessible -> accessible * chore: changeset * chore: remove bad mdx comment
- Loading branch information
1 parent
40a45e3
commit b2b367c
Showing
11 changed files
with
348 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@astrojs/mdx': minor | ||
'astro': patch | ||
--- | ||
|
||
Support YAML frontmatter in MDX files |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
packages/integrations/mdx/test/fixtures/mdx-custom-frontmatter-name/src/pages/glob.json.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export async function get() { | ||
const mdxPages = await import.meta.glob('./*.mdx', { eager: true }); | ||
|
||
return { | ||
body: JSON.stringify({ | ||
titles: Object.values(mdxPages ?? {}).map(v => v?.customFrontmatter?.title), | ||
}) | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
.../integrations/mdx/test/fixtures/mdx-custom-frontmatter-name/src/pages/index.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
title: 'Using YAML frontmatter' | ||
illThrowIfIDontExist: "Oh no, that's scary!" | ||
--- | ||
|
||
# {customFrontmatter.illThrowIfIDontExist} |
9 changes: 9 additions & 0 deletions
9
packages/integrations/mdx/test/fixtures/mdx-frontmatter/src/pages/glob.json.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export async function get() { | ||
const mdxPages = await import.meta.glob('./*.mdx', { eager: true }); | ||
|
||
return { | ||
body: JSON.stringify({ | ||
titles: Object.values(mdxPages ?? {}).map(v => v?.frontmatter?.title), | ||
}) | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/integrations/mdx/test/fixtures/mdx-frontmatter/src/pages/index.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
title: 'Using YAML frontmatter' | ||
illThrowIfIDontExist: "Oh no, that's scary!" | ||
--- | ||
|
||
# {frontmatter.illThrowIfIDontExist} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import mdx from '@astrojs/mdx'; | ||
|
||
import { expect } from 'chai'; | ||
import { loadFixture } from '../../../astro/test/test-utils.js'; | ||
|
||
const FIXTURE_ROOT = new URL('./fixtures/mdx-frontmatter/', import.meta.url); | ||
|
||
describe('MDX frontmatter', () => { | ||
it('builds when "frontmatter.property" is in JSX expression', async () => { | ||
const fixture = await loadFixture({ | ||
root: FIXTURE_ROOT, | ||
integrations: [mdx()], | ||
}); | ||
await fixture.build(); | ||
expect(true).to.equal(true); | ||
}); | ||
|
||
it('extracts frontmatter to "frontmatter" export', async () => { | ||
const fixture = await loadFixture({ | ||
root: FIXTURE_ROOT, | ||
integrations: [mdx()], | ||
}); | ||
await fixture.build(); | ||
|
||
const { titles } = JSON.parse(await fixture.readFile('/glob.json')); | ||
expect(titles).to.include('Using YAML frontmatter'); | ||
}); | ||
|
||
it('extracts frontmatter to "customFrontmatter" export when configured', async () => { | ||
const fixture = await loadFixture({ | ||
root: new URL('./fixtures/mdx-custom-frontmatter-name/', import.meta.url), | ||
integrations: [mdx({ | ||
frontmatterOptions: { | ||
name: 'customFrontmatter', | ||
}, | ||
})], | ||
}); | ||
await fixture.build(); | ||
|
||
const { titles } = JSON.parse(await fixture.readFile('/glob.json')); | ||
expect(titles).to.include('Using YAML frontmatter'); | ||
}); | ||
}); |
Oops, something went wrong.