Skip to content

Commit 24df79b

Browse files
committed
wip: move mdx to collection type API
1 parent cc44e4f commit 24df79b

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
title: 'Example!'
3+
---
4+
5+
# With MDX {frontmatter.title}

examples/with-markdoc/src/pages/index.astro

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import Marquee from '../components/Marquee.astro';
55
import { getEntryBySlug } from 'astro:content';
66
import RedP from '../components/RedP.astro';
77
8-
const testEntry = await getEntryBySlug('blog', 'test');
9-
console.log(testEntry);
10-
const { Content } = await testEntry.render();
8+
const mdocEntry = await getEntryBySlug('blog', 'test');
9+
const mdxEntry = await getEntryBySlug('blog', 'with-mdx');
10+
console.log(mdocEntry);
11+
const { Content } = await mdocEntry.render();
12+
const { Content: MDXContent } = await mdxEntry.render();
1113
---
1214

1315
<html lang="en">
@@ -21,6 +23,7 @@ const { Content } = await testEntry.render();
2123
<body>
2224
<h1>Astro</h1>
2325
<article>
26+
<MDXContent />
2427
<Content
2528
components={{
2629
marquee: Marquee,

packages/astro/src/content/consts.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const defaultContentEntryExts = ['.md', '.mdx'] as const;
1+
export const defaultContentEntryExts = ['.md'] as const;
22
export const PROPAGATED_ASSET_FLAG = 'astroPropagatedAssets';
33
export const CONTENT_FLAG = 'astroContent';
44
export const VIRTUAL_MODULE_ID = 'astro:content';

packages/integrations/mdx/src/index.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import mdxPlugin, { Options as MdxRollupPluginOptions } from '@mdx-js/rollup';
66
import type { AstroIntegration } from 'astro';
77
import { parse as parseESM } from 'es-module-lexer';
88
import fs from 'node:fs/promises';
9+
import { fileURLToPath } from 'node:url';
910
import type { Options as RemarkRehypeOptions } from 'remark-rehype';
1011
import { VFile } from 'vfile';
1112
import type { Plugin as VitePlugin } from 'vite';
@@ -22,12 +23,33 @@ export type MdxOptions = Omit<typeof markdownConfigDefaults, 'remarkPlugins' | '
2223
remarkRehype: RemarkRehypeOptions;
2324
};
2425

26+
const contentEntryType = {
27+
extensions: ['.mdx'],
28+
async getEntryInfo({ fileUrl }: { fileUrl: URL }) {
29+
const rawContents = await fs.readFile(fileUrl, 'utf-8');
30+
const parsed = parseFrontmatter(rawContents, fileURLToPath(fileUrl));
31+
return {
32+
data: parsed.data,
33+
body: parsed.content,
34+
slug: parsed.data.slug,
35+
rawData: parsed.matter,
36+
};
37+
},
38+
};
39+
2540
export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): AstroIntegration {
2641
return {
2742
name: '@astrojs/mdx',
2843
hooks: {
29-
'astro:config:setup': async ({ updateConfig, config, addPageExtension, command }: any) => {
44+
'astro:config:setup': async ({
45+
updateConfig,
46+
config,
47+
addPageExtension,
48+
addContentEntryType,
49+
command,
50+
}: any) => {
3051
addPageExtension('.mdx');
52+
addContentEntryType(contentEntryType);
3153

3254
const extendMarkdownConfig =
3355
partialMdxOptions.extendMarkdownConfig ?? defaultOptions.extendMarkdownConfig;

0 commit comments

Comments
 (0)