2022-07-20 13:14:23 -05:00
|
|
|
import mdxPlugin, { Options as MdxRollupPluginOptions } from '@mdx-js/rollup';
|
2022-07-20 09:58:33 -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-20 13:14:23 -05:00
|
|
|
import remarkGfm from 'remark-gfm';
|
|
|
|
import remarkSmartypants from 'remark-smartypants';
|
2022-07-20 09:56:32 -05:00
|
|
|
import { getFileInfo } 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 13:16:33 -05:00
|
|
|
};
|
2022-07-20 13:14:23 -05:00
|
|
|
|
|
|
|
const DEFAULT_REMARK_PLUGINS = [remarkGfm, remarkSmartypants];
|
|
|
|
|
2022-07-20 13:16:33 -05:00
|
|
|
function handleExtends<T>(
|
|
|
|
config: WithExtends<T[] | undefined>,
|
|
|
|
defaults: T[] = []
|
|
|
|
): T[] | undefined {
|
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');
|
|
|
|
updateConfig({
|
|
|
|
vite: {
|
|
|
|
plugins: [
|
|
|
|
{
|
|
|
|
enforce: 'pre',
|
|
|
|
...mdxPlugin({
|
2022-07-20 13:14:23 -05:00
|
|
|
remarkPlugins: handleExtends(mdxOptions.remarkPlugins, DEFAULT_REMARK_PLUGINS),
|
|
|
|
rehypePlugins: handleExtends(mdxOptions.rehypePlugins),
|
|
|
|
// place these after so the user can't override
|
2022-06-30 13:11:12 -05:00
|
|
|
jsx: true,
|
|
|
|
jsxImportSource: 'astro',
|
|
|
|
// Note: disable `.md` support
|
|
|
|
format: 'mdx',
|
|
|
|
mdExtensions: [],
|
|
|
|
}),
|
|
|
|
},
|
2022-07-20 09:56:32 -05:00
|
|
|
{
|
2022-06-30 13:11:12 -05:00
|
|
|
name: '@astrojs/mdx',
|
|
|
|
transform(code: string, id: string) {
|
|
|
|
if (!id.endsWith('.mdx')) return;
|
2022-07-20 09:56:32 -05:00
|
|
|
const [, moduleExports] = parseESM(code);
|
|
|
|
|
|
|
|
if (!moduleExports.includes('url')) {
|
|
|
|
const { fileUrl } = getFileInfo(id, config);
|
|
|
|
code += `\nexport const url = ${JSON.stringify(fileUrl)};`;
|
|
|
|
}
|
|
|
|
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-06-30 13:09:09 -05:00
|
|
|
}
|