-
-
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] Include
url
in glob result (#3981)
* deps: add es-module-lexer * feat: inject url export on mdx files * fix: apply url transform in prod * test: page urls with overrides * fix: revert test skips * chore: changeset * fix: add newline before export
- Loading branch information
1 parent
eaf187f
commit 61fec63
Showing
10 changed files
with
91 additions
and
9 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 | ||
--- | ||
|
||
Include page url in MDX glob result |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import type { AstroConfig } from 'astro'; | ||
|
||
function appendForwardSlash(path: string) { | ||
return path.endsWith('/') ? path : path + '/'; | ||
} | ||
|
||
/** @see 'vite-plugin-utils' for source */ | ||
export function getFileInfo(id: string, config: AstroConfig) { | ||
const sitePathname = appendForwardSlash( | ||
config.site ? new URL(config.base, config.site).pathname : config.base | ||
); | ||
|
||
const fileId = id.split('?')[0]; | ||
let fileUrl = fileId.includes('/pages/') | ||
? fileId.replace(/^.*?\/pages\//, sitePathname).replace(/(\/index)?\.mdx$/, '') | ||
: undefined; | ||
if (fileUrl && config.trailingSlash === 'always') { | ||
fileUrl = appendForwardSlash(fileUrl); | ||
} | ||
return { fileId, fileUrl }; | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/integrations/mdx/test/fixtures/mdx-url-export/src/pages/pages.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({ | ||
urls: Object.values(mdxPages ?? {}).map(v => v?.url), | ||
}) | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
packages/integrations/mdx/test/fixtures/mdx-url-export/src/pages/test-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 @@ | ||
# I'm a page with a url of "/test-1!" |
1 change: 1 addition & 0 deletions
1
packages/integrations/mdx/test/fixtures/mdx-url-export/src/pages/test-2.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 @@ | ||
# I'm a page with a url of "/test-2!" |
3 changes: 3 additions & 0 deletions
3
...s/integrations/mdx/test/fixtures/mdx-url-export/src/pages/with-url-override.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,3 @@ | ||
export const url = '/AH!' | ||
|
||
# I'm a test with a url override! |
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,28 @@ | ||
import mdx from '@astrojs/mdx'; | ||
|
||
import { expect } from 'chai'; | ||
import { loadFixture } from '../../../astro/test/test-utils.js'; | ||
|
||
describe('MDX url export', () => { | ||
let fixture; | ||
|
||
before(async () => { | ||
fixture = await loadFixture({ | ||
root: new URL('./fixtures/mdx-url-export/', import.meta.url), | ||
integrations: [mdx()], | ||
}); | ||
|
||
await fixture.build(); | ||
}); | ||
|
||
it('generates correct urls in glob result', async () => { | ||
const { urls } = JSON.parse(await fixture.readFile('/pages.json')); | ||
expect(urls).to.include('/test-1'); | ||
expect(urls).to.include('/test-2'); | ||
}); | ||
|
||
it('respects "export url" overrides in glob result', async () => { | ||
const { urls } = JSON.parse(await fixture.readFile('/pages.json')); | ||
expect(urls).to.include('/AH!'); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.