-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure file and url are always present in MDX for Astro.glob
- Loading branch information
Showing
6 changed files
with
100 additions
and
5 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,5 @@ | ||
--- | ||
'@astrojs/mdx': patch | ||
--- | ||
|
||
Ensure file and url are always present in MDX for Astro.glob |
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
5 changes: 5 additions & 0 deletions
5
packages/integrations/mdx/test/fixtures/mdx-get-static-paths/src/content/1.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,5 @@ | ||
--- | ||
one: hello | ||
slug: one | ||
--- | ||
First mdx file |
34 changes: 34 additions & 0 deletions
34
packages/integrations/mdx/test/fixtures/mdx-get-static-paths/src/pages/[slug].astro
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,34 @@ | ||
--- | ||
export const getStaticPaths = async () => { | ||
const content = await Astro.glob('../content/*.mdx'); | ||
return content | ||
.filter((page) => !page.frontmatter.draft) // skip drafts | ||
.map(({ default: MdxContent, frontmatter, url, file }) => { | ||
return { | ||
params: { slug: frontmatter.slug || "index" }, | ||
props: { | ||
MdxContent, | ||
file, | ||
frontmatter, | ||
url | ||
} | ||
} | ||
}) | ||
} | ||
const { MdxContent, frontmatter, url, file } = Astro.props; | ||
--- | ||
|
||
<html> | ||
<head> | ||
<title>Page</title> | ||
</head> | ||
<body> | ||
<MdxContent /> | ||
|
||
<div id="one">{frontmatter.one}</div> | ||
<div id="url">{url}</div> | ||
<div id="file">{file}</div> | ||
</body> | ||
</html> |
29 changes: 29 additions & 0 deletions
29
packages/integrations/mdx/test/mdx-get-static-paths.test.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,29 @@ | ||
import mdx from '@astrojs/mdx'; | ||
|
||
import { expect } from 'chai'; | ||
import { loadFixture } from '../../../astro/test/test-utils.js'; | ||
import * as cheerio from 'cheerio'; | ||
|
||
const FIXTURE_ROOT = new URL('./fixtures/mdx-get-static-paths', import.meta.url); | ||
|
||
describe('getStaticPaths', () => { | ||
/** @type {import('astro/test/test-utils').Fixture} */ | ||
let fixture; | ||
before(async () => { | ||
fixture = await loadFixture({ | ||
root: FIXTURE_ROOT, | ||
integrations: [mdx()], | ||
}); | ||
await fixture.build(); | ||
}); | ||
|
||
it('Provides file and url', async () => { | ||
const html = await fixture.readFile('/one/index.html'); | ||
|
||
const $ = cheerio.load(html); | ||
expect($('p').text()).to.equal('First mdx file'); | ||
expect($('#one').text()).to.equal('hello', 'Frontmatter included'); | ||
expect($('#url').text()).to.equal('/src/content/1.mdx', 'url is included'); | ||
expect($('#file').text()).to.contain('fixtures/mdx-get-static-paths/src/content/1.mdx', 'file is included'); | ||
}); | ||
}); |