Skip to content

Commit

Permalink
wip: move mdx to collection type API
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Feb 16, 2023
1 parent cc44e4f commit 24df79b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
5 changes: 5 additions & 0 deletions examples/with-markdoc/src/content/blog/with-mdx.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: 'Example!'
---

# With MDX {frontmatter.title}
9 changes: 6 additions & 3 deletions examples/with-markdoc/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import Marquee from '../components/Marquee.astro';
import { getEntryBySlug } from 'astro:content';
import RedP from '../components/RedP.astro';
const testEntry = await getEntryBySlug('blog', 'test');
console.log(testEntry);
const { Content } = await testEntry.render();
const mdocEntry = await getEntryBySlug('blog', 'test');
const mdxEntry = await getEntryBySlug('blog', 'with-mdx');
console.log(mdocEntry);
const { Content } = await mdocEntry.render();
const { Content: MDXContent } = await mdxEntry.render();
---

<html lang="en">
Expand All @@ -21,6 +23,7 @@ const { Content } = await testEntry.render();
<body>
<h1>Astro</h1>
<article>
<MDXContent />
<Content
components={{
marquee: Marquee,
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/content/consts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const defaultContentEntryExts = ['.md', '.mdx'] as const;
export const defaultContentEntryExts = ['.md'] as const;
export const PROPAGATED_ASSET_FLAG = 'astroPropagatedAssets';
export const CONTENT_FLAG = 'astroContent';
export const VIRTUAL_MODULE_ID = 'astro:content';
Expand Down
24 changes: 23 additions & 1 deletion packages/integrations/mdx/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import mdxPlugin, { Options as MdxRollupPluginOptions } from '@mdx-js/rollup';
import type { AstroIntegration } from 'astro';
import { parse as parseESM } from 'es-module-lexer';
import fs from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import type { Options as RemarkRehypeOptions } from 'remark-rehype';
import { VFile } from 'vfile';
import type { Plugin as VitePlugin } from 'vite';
Expand All @@ -22,12 +23,33 @@ export type MdxOptions = Omit<typeof markdownConfigDefaults, 'remarkPlugins' | '
remarkRehype: RemarkRehypeOptions;
};

const contentEntryType = {
extensions: ['.mdx'],
async getEntryInfo({ fileUrl }: { fileUrl: URL }) {
const rawContents = await fs.readFile(fileUrl, 'utf-8');
const parsed = parseFrontmatter(rawContents, fileURLToPath(fileUrl));
return {
data: parsed.data,
body: parsed.content,
slug: parsed.data.slug,
rawData: parsed.matter,
};
},
};

export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): AstroIntegration {
return {
name: '@astrojs/mdx',
hooks: {
'astro:config:setup': async ({ updateConfig, config, addPageExtension, command }: any) => {
'astro:config:setup': async ({
updateConfig,
config,
addPageExtension,
addContentEntryType,
command,
}: any) => {
addPageExtension('.mdx');
addContentEntryType(contentEntryType);

const extendMarkdownConfig =
partialMdxOptions.extendMarkdownConfig ?? defaultOptions.extendMarkdownConfig;
Expand Down

0 comments on commit 24df79b

Please sign in to comment.