2022-08-01 16:25:50 -05:00
|
|
|
import { compile as mdxCompile, nodeTypes } from '@mdx-js/mdx';
|
2022-07-21 15:43:58 -05:00
|
|
|
import mdxPlugin, { Options as MdxRollupPluginOptions } from '@mdx-js/rollup';
|
2022-08-05 18:58:09 -05:00
|
|
|
import type { AstroConfig, 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:36:26 -05:00
|
|
|
import remarkGfm from 'remark-gfm';
|
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-08-01 16:25:50 -05:00
|
|
|
import { VFile } from 'vfile';
|
2022-07-29 10:24:57 -05:00
|
|
|
import type { Plugin as VitePlugin } from 'vite';
|
2022-08-05 18:58:09 -05:00
|
|
|
import { rehypeApplyFrontmatterExport, remarkInitializeAstroData } from './astro-data-utils.js';
|
2022-08-01 16:25:50 -05:00
|
|
|
import rehypeCollectHeadings from './rehype-collect-headings.js';
|
2022-07-21 15:43:58 -05:00
|
|
|
import remarkPrism from './remark-prism.js';
|
2022-08-05 18:55:38 -05:00
|
|
|
import { getFileInfo, parseFrontmatter } 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:36:26 -05:00
|
|
|
};
|
2022-07-20 13:14:23 -05:00
|
|
|
|
2022-08-05 22:51:38 -05:00
|
|
|
const DEFAULT_REMARK_PLUGINS: MdxRollupPluginOptions['remarkPlugins'] = [
|
|
|
|
remarkGfm,
|
|
|
|
remarkSmartypants,
|
|
|
|
];
|
|
|
|
const DEFAULT_REHYPE_PLUGINS: MdxRollupPluginOptions['rehypePlugins'] = [];
|
2022-07-20 13:14:23 -05:00
|
|
|
|
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 ?? [])];
|
|
|
|
}
|
|
|
|
|
2022-08-05 18:55:38 -05:00
|
|
|
function getRemarkPlugins(
|
|
|
|
mdxOptions: MdxOptions,
|
|
|
|
config: AstroConfig
|
|
|
|
): MdxRollupPluginOptions['remarkPlugins'] {
|
|
|
|
let remarkPlugins = [
|
|
|
|
// Initialize vfile.data.astroExports before all plugins are run
|
|
|
|
remarkInitializeAstroData,
|
|
|
|
...handleExtends(mdxOptions.remarkPlugins, DEFAULT_REMARK_PLUGINS),
|
|
|
|
];
|
|
|
|
if (config.markdown.syntaxHighlight === 'shiki') {
|
|
|
|
// Default export still requires ".default" chaining for some reason
|
|
|
|
// Workarounds tried:
|
|
|
|
// - "import * as remarkShikiTwoslash"
|
|
|
|
// - "import { default as remarkShikiTwoslash }"
|
|
|
|
const shikiTwoslash = (remarkShikiTwoslash as any).default ?? remarkShikiTwoslash;
|
|
|
|
remarkPlugins.push([shikiTwoslash, config.markdown.shikiConfig]);
|
|
|
|
}
|
|
|
|
if (config.markdown.syntaxHighlight === 'prism') {
|
|
|
|
remarkPlugins.push(remarkPrism);
|
|
|
|
}
|
|
|
|
return remarkPlugins;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRehypePlugins(
|
|
|
|
mdxOptions: MdxOptions,
|
|
|
|
config: AstroConfig
|
|
|
|
): MdxRollupPluginOptions['rehypePlugins'] {
|
|
|
|
let rehypePlugins = handleExtends(mdxOptions.rehypePlugins, DEFAULT_REHYPE_PLUGINS);
|
|
|
|
|
|
|
|
if (config.markdown.syntaxHighlight === 'shiki' || config.markdown.syntaxHighlight === 'prism') {
|
2022-08-10 16:10:06 -05:00
|
|
|
rehypePlugins.unshift([rehypeRaw, { passThrough: nodeTypes }]);
|
2022-08-05 18:55:38 -05:00
|
|
|
}
|
2022-08-05 22:51:38 -05:00
|
|
|
// getHeadings() is guaranteed by TS, so we can't allow user to override
|
2022-08-10 16:10:06 -05:00
|
|
|
rehypePlugins.unshift(rehypeCollectHeadings);
|
2022-08-05 18:55:38 -05:00
|
|
|
|
|
|
|
return rehypePlugins;
|
|
|
|
}
|
|
|
|
|
2022-07-20 13:14:23 -05:00
|
|
|
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
|
|
|
|
2022-08-01 16:23:56 -05:00
|
|
|
const mdxPluginOpts: MdxRollupPluginOptions = {
|
2022-08-05 18:55:38 -05:00
|
|
|
remarkPlugins: getRemarkPlugins(mdxOptions, config),
|
|
|
|
rehypePlugins: getRehypePlugins(mdxOptions, config),
|
2022-07-29 10:22:57 -05:00
|
|
|
jsx: true,
|
|
|
|
jsxImportSource: 'astro',
|
|
|
|
// Note: disable `.md` support
|
|
|
|
format: 'mdx',
|
|
|
|
mdExtensions: [],
|
2022-08-01 16:23:56 -05:00
|
|
|
};
|
2022-07-29 10:22:57 -05:00
|
|
|
|
2022-06-30 13:11:12 -05:00
|
|
|
updateConfig({
|
|
|
|
vite: {
|
|
|
|
plugins: [
|
|
|
|
{
|
|
|
|
enforce: 'pre',
|
2022-08-01 16:23:56 -05:00
|
|
|
...mdxPlugin(mdxPluginOpts),
|
|
|
|
// Override transform to alter code before MDX compilation
|
|
|
|
// ex. inject layouts
|
|
|
|
async transform(code, id) {
|
|
|
|
if (!id.endsWith('mdx')) return;
|
2022-07-29 10:22:57 -05:00
|
|
|
|
2022-08-11 12:36:34 -05:00
|
|
|
const { data: frontmatter, content: pageContent } = parseFrontmatter(code, id);
|
2022-08-05 18:55:38 -05:00
|
|
|
const compiled = await mdxCompile(new VFile({ value: pageContent, path: id }), {
|
|
|
|
...mdxPluginOpts,
|
|
|
|
rehypePlugins: [
|
|
|
|
...(mdxPluginOpts.rehypePlugins ?? []),
|
2022-08-08 17:33:35 -05:00
|
|
|
() => rehypeApplyFrontmatterExport(frontmatter),
|
2022-08-05 18:55:38 -05:00
|
|
|
],
|
|
|
|
});
|
2022-08-01 16:23:56 -05:00
|
|
|
|
|
|
|
return {
|
|
|
|
code: String(compiled.value),
|
|
|
|
map: compiled.map,
|
|
|
|
};
|
2022-07-29 10:24:57 -05:00
|
|
|
},
|
2022-06-30 13:11:12 -05:00
|
|
|
},
|
2022-07-20 09:56:32 -05:00
|
|
|
{
|
2022-08-01 16:23:56 -05:00
|
|
|
name: '@astrojs/mdx-postprocess',
|
|
|
|
// These transforms must happen *after* JSX runtime transformations
|
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-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
|
|
|
}
|