2022-07-29 10:22:57 -05:00
|
|
|
import type { Plugin as VitePlugin } from 'vite';
|
2022-07-21 15:43:58 -05:00
|
|
|
import { nodeTypes } from '@mdx-js/mdx';
|
|
|
|
import mdxPlugin, { Options as MdxRollupPluginOptions } from '@mdx-js/rollup';
|
2022-07-21 15:46:16 -05:00
|
|
|
import type { AstroIntegration } from 'astro';
|
2022-07-20 09:56:32 -05:00
|
|
|
import { parse as parseESM } from 'es-module-lexer';
|
2022-07-21 15:46:16 -05:00
|
|
|
import rehypeRaw from 'rehype-raw';
|
2022-07-20 20:34:21 -05:00
|
|
|
import remarkFrontmatter from 'remark-frontmatter';
|
2022-07-20 20:36:26 -05:00
|
|
|
import remarkGfm from 'remark-gfm';
|
2022-07-21 15:46:16 -05:00
|
|
|
import type { RemarkMdxFrontmatterOptions } from 'remark-mdx-frontmatter';
|
2022-07-20 20:34:21 -05:00
|
|
|
import remarkMdxFrontmatter from 'remark-mdx-frontmatter';
|
2022-07-21 15:46:16 -05:00
|
|
|
import remarkShikiTwoslash from 'remark-shiki-twoslash';
|
2022-07-20 20:36:26 -05:00
|
|
|
import remarkSmartypants from 'remark-smartypants';
|
2022-07-21 15:43:58 -05:00
|
|
|
import remarkPrism from './remark-prism.js';
|
2022-07-29 10:22:57 -05:00
|
|
|
import { getFileInfo, getFrontmatter } from './utils.js';
|
2022-06-30 13:09:09 -05:00
|
|
|
|
2022-07-20 13:14:23 -05:00
|
|
|
type WithExtends<T> = T | { extends: T };
|
|
|
|
|
|
|
|
type MdxOptions = {
|
|
|
|
remarkPlugins?: WithExtends<MdxRollupPluginOptions['remarkPlugins']>;
|
|
|
|
rehypePlugins?: WithExtends<MdxRollupPluginOptions['rehypePlugins']>;
|
2022-07-20 20:34:21 -05:00
|
|
|
/**
|
|
|
|
* Configure the remark-mdx-frontmatter plugin
|
|
|
|
* @see https://github.com/remcohaszing/remark-mdx-frontmatter#options for a full list of options
|
|
|
|
* @default {{ name: 'frontmatter' }}
|
|
|
|
*/
|
2022-07-20 20:36:26 -05:00
|
|
|
frontmatterOptions?: RemarkMdxFrontmatterOptions;
|
|
|
|
};
|
2022-07-20 13:14:23 -05:00
|
|
|
|
|
|
|
const DEFAULT_REMARK_PLUGINS = [remarkGfm, remarkSmartypants];
|
|
|
|
|
2022-07-21 15:46:16 -05:00
|
|
|
function handleExtends<T>(config: WithExtends<T[] | undefined>, defaults: T[] = []): T[] {
|
2022-07-20 13:14:23 -05:00
|
|
|
if (Array.isArray(config)) return config;
|
|
|
|
|
|
|
|
return [...defaults, ...(config?.extends ?? [])];
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
|
2022-06-30 13:09:09 -05:00
|
|
|
return {
|
2022-06-30 13:11:12 -05:00
|
|
|
name: '@astrojs/mdx',
|
|
|
|
hooks: {
|
2022-07-20 09:56:32 -05:00
|
|
|
'astro:config:setup': ({ updateConfig, config, addPageExtension, command }: any) => {
|
2022-06-30 13:11:12 -05:00
|
|
|
addPageExtension('.mdx');
|
2022-07-21 15:43:58 -05:00
|
|
|
let remarkPlugins = handleExtends(mdxOptions.remarkPlugins, DEFAULT_REMARK_PLUGINS);
|
|
|
|
let rehypePlugins = handleExtends(mdxOptions.rehypePlugins);
|
|
|
|
|
|
|
|
if (config.markdown.syntaxHighlight === 'shiki') {
|
|
|
|
remarkPlugins.push([
|
|
|
|
// Default export still requires ".default" chaining for some reason
|
|
|
|
// Workarounds tried:
|
|
|
|
// - "import * as remarkShikiTwoslash"
|
|
|
|
// - "import { default as remarkShikiTwoslash }"
|
|
|
|
(remarkShikiTwoslash as any).default,
|
|
|
|
config.markdown.shikiConfig,
|
|
|
|
]);
|
|
|
|
rehypePlugins.push([rehypeRaw, { passThrough: nodeTypes }]);
|
|
|
|
}
|
2022-07-21 15:46:16 -05:00
|
|
|
|
2022-07-21 15:43:58 -05:00
|
|
|
if (config.markdown.syntaxHighlight === 'prism') {
|
|
|
|
remarkPlugins.push(remarkPrism);
|
|
|
|
rehypePlugins.push([rehypeRaw, { passThrough: nodeTypes }]);
|
|
|
|
}
|
|
|
|
|
|
|
|
remarkPlugins.push(remarkFrontmatter);
|
|
|
|
remarkPlugins.push([
|
|
|
|
remarkMdxFrontmatter,
|
|
|
|
{
|
|
|
|
name: 'frontmatter',
|
|
|
|
...mdxOptions.frontmatterOptions,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
2022-07-29 10:22:57 -05:00
|
|
|
const configuredMdxPlugin = mdxPlugin({
|
|
|
|
remarkPlugins,
|
|
|
|
rehypePlugins,
|
|
|
|
jsx: true,
|
|
|
|
jsxImportSource: 'astro',
|
|
|
|
// Note: disable `.md` support
|
|
|
|
format: 'mdx',
|
|
|
|
mdExtensions: [],
|
|
|
|
})
|
|
|
|
|
2022-06-30 13:11:12 -05:00
|
|
|
updateConfig({
|
|
|
|
vite: {
|
|
|
|
plugins: [
|
|
|
|
{
|
|
|
|
enforce: 'pre',
|
2022-07-29 10:22:57 -05:00
|
|
|
...configuredMdxPlugin,
|
|
|
|
// Override transform to inject layouts before MDX compilation
|
|
|
|
async transform(this, code, id) {
|
|
|
|
if (!id.endsWith('.mdx')) return;
|
|
|
|
|
|
|
|
const mdxPluginTransform = configuredMdxPlugin.transform?.bind(this);
|
|
|
|
// If user overrides our default YAML parser,
|
|
|
|
// do not attempt to parse the `layout` via gray-matter
|
|
|
|
if (mdxOptions.frontmatterOptions?.parsers) {
|
|
|
|
return mdxPluginTransform?.(code, id);
|
|
|
|
}
|
|
|
|
const frontmatter = getFrontmatter(code, id);
|
|
|
|
if (frontmatter.layout) {
|
|
|
|
const { layout, ...content } = frontmatter;
|
|
|
|
code += `\nexport default async function({ children }) {\nconst Layout = (await import(${
|
|
|
|
JSON.stringify(frontmatter.layout)
|
|
|
|
})).default;\nreturn <Layout content={${
|
|
|
|
JSON.stringify(content)
|
|
|
|
}}>{children}</Layout> }`
|
|
|
|
}
|
|
|
|
return mdxPluginTransform?.(code, id);
|
|
|
|
}
|
2022-06-30 13:11:12 -05:00
|
|
|
},
|
2022-07-20 09:56:32 -05:00
|
|
|
{
|
2022-06-30 13:11:12 -05:00
|
|
|
name: '@astrojs/mdx',
|
2022-07-29 10:22:57 -05:00
|
|
|
transform(code, id) {
|
2022-06-30 13:11:12 -05:00
|
|
|
if (!id.endsWith('.mdx')) return;
|
2022-07-20 09:56:32 -05:00
|
|
|
const [, moduleExports] = parseESM(code);
|
|
|
|
|
2022-07-25 23:26:47 -05:00
|
|
|
// This adds support for injected "page-ssr" scripts in MDX files.
|
|
|
|
// TODO: This should only be happening on page entrypoints, not all imported MDX.
|
|
|
|
// TODO: This code is copy-pasted across all Astro/Vite plugins that deal with page
|
|
|
|
// entrypoints (.astro, .md, .mdx). This should be handled in some centralized place,
|
|
|
|
// or otherwise refactored to not require copy-paste handling logic.
|
|
|
|
code += `\nimport "${'astro:scripts/page-ssr.js'}";`;
|
|
|
|
|
2022-07-28 09:58:44 -05:00
|
|
|
const { fileUrl, fileId } = getFileInfo(id, config);
|
2022-07-20 09:56:32 -05:00
|
|
|
if (!moduleExports.includes('url')) {
|
|
|
|
code += `\nexport const url = ${JSON.stringify(fileUrl)};`;
|
|
|
|
}
|
2022-07-28 09:58:44 -05:00
|
|
|
if (!moduleExports.includes('file')) {
|
|
|
|
code += `\nexport const file = ${JSON.stringify(fileId)};`;
|
|
|
|
}
|
|
|
|
|
2022-07-20 09:56:32 -05:00
|
|
|
if (command === 'dev') {
|
|
|
|
// TODO: decline HMR updates until we have a stable approach
|
|
|
|
code += `\nif (import.meta.hot) {
|
2022-06-30 13:09:09 -05:00
|
|
|
import.meta.hot.decline();
|
2022-07-20 09:58:33 -05:00
|
|
|
}`;
|
2022-07-20 09:56:32 -05:00
|
|
|
}
|
|
|
|
return code;
|
2022-06-30 13:11:12 -05:00
|
|
|
},
|
|
|
|
},
|
2022-07-29 10:22:57 -05:00
|
|
|
] as VitePlugin[],
|
2022-06-30 13:11:12 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2022-06-30 13:09:09 -05:00
|
|
|
}
|