-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[MDX] Support YAML frontmatter #3995
Merged
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a5c6340
chore: remove old comment
bholmesdev ce04644
deps: add remark-frontmatter
bholmesdev 6fb86b0
deps: add remark-mdx-frontmatter
bholmesdev cc03b73
fix: handle null or undefined frontmatter key
bholmesdev 986a56b
feat: configure frontmatter plugins with defaults
bholmesdev 5133fa2
test: frontmatter and custom frontmatter name
bholmesdev 3e2f555
docs: add frontmatterOptions config
bholmesdev c446b5c
docs: add "variables" and "frontmatter" docs
bholmesdev 2d9e05c
chore: excessible -> accessible
bholmesdev 13f01f7
chore: changeset
bholmesdev 63e3074
chore: remove bad mdx comment
bholmesdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remark-mdx-frontmatter applies
export const frontmatter = undefined
when no frontmatter is present. This handles that case!