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 20:34:21 -05:00
|
|
|
import type { RemarkMdxFrontmatterOptions } from 'remark-mdx-frontmatter';
|
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 20:34:21 -05:00
|
|
|
import remarkFrontmatter from 'remark-frontmatter';
|
|
|
|
import remarkMdxFrontmatter from 'remark-mdx-frontmatter';
|
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 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' }}
|
|
|
|
*/
|
|
|
|
frontmatterOptions?: RemarkMdxFrontmatterOptions;
|
|
|
|
}
|
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>,
|
2022-07-20 20:34:21 -05:00
|
|
|
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');
|
|
|
|
updateConfig({
|
|
|
|
vite: {
|
|
|
|
plugins: [
|
|
|
|
{
|
|
|
|
enforce: 'pre',
|
|
|
|
...mdxPlugin({
|
2022-07-20 20:34:21 -05:00
|
|
|
remarkPlugins: [
|
|
|
|
...handleExtends(mdxOptions.remarkPlugins, DEFAULT_REMARK_PLUGINS),
|
|
|
|
// Frontmatter plugins should always be applied!
|
|
|
|
// We can revisit this if a strong use case to *remove*
|
|
|
|
// YAML frontmatter via config is reported.
|
|
|
|
remarkFrontmatter,
|
|
|
|
[remarkMdxFrontmatter, {
|
|
|
|
name: 'frontmatter',
|
|
|
|
...mdxOptions.frontmatterOptions,
|
|
|
|
}],
|
|
|
|
],
|
2022-07-20 13:14:23 -05:00
|
|
|
rehypePlugins: handleExtends(mdxOptions.rehypePlugins),
|
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
|
|
|
}
|