diff options
Diffstat (limited to 'packages/integrations/mdx/src/index.ts')
-rw-r--r-- | packages/integrations/mdx/src/index.ts | 59 |
1 files changed, 41 insertions, 18 deletions
diff --git a/packages/integrations/mdx/src/index.ts b/packages/integrations/mdx/src/index.ts index 66ab7b837..2ac6cc66a 100644 --- a/packages/integrations/mdx/src/index.ts +++ b/packages/integrations/mdx/src/index.ts @@ -1,11 +1,15 @@ -import mdxPlugin, { Options as MdxRollupPluginOptions } from '@mdx-js/rollup'; +import type { RemarkMdxFrontmatterOptions } from 'remark-mdx-frontmatter'; import type { AstroIntegration } from 'astro'; +import remarkShikiTwoslash from 'remark-shiki-twoslash'; +import { nodeTypes } from '@mdx-js/mdx'; +import rehypeRaw from 'rehype-raw'; +import mdxPlugin, { Options as MdxRollupPluginOptions } from '@mdx-js/rollup'; import { parse as parseESM } from 'es-module-lexer'; import remarkFrontmatter from 'remark-frontmatter'; import remarkGfm from 'remark-gfm'; -import type { RemarkMdxFrontmatterOptions } from 'remark-mdx-frontmatter'; import remarkMdxFrontmatter from 'remark-mdx-frontmatter'; import remarkSmartypants from 'remark-smartypants'; +import remarkPrism from './remark-prism.js'; import { getFileInfo } from './utils.js'; type WithExtends<T> = T | { extends: T }; @@ -23,7 +27,10 @@ type MdxOptions = { const DEFAULT_REMARK_PLUGINS = [remarkGfm, remarkSmartypants]; -function handleExtends<T>(config: WithExtends<T[] | undefined>, defaults: T[] = []): T[] { +function handleExtends<T>( + config: WithExtends<T[] | undefined>, + defaults: T[] = [], +): T[] { if (Array.isArray(config)) return config; return [...defaults, ...(config?.extends ?? [])]; @@ -35,27 +42,43 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration { hooks: { 'astro:config:setup': ({ updateConfig, config, addPageExtension, command }: any) => { addPageExtension('.mdx'); + 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 }]); + } + + if (config.markdown.syntaxHighlight === 'prism') { + remarkPlugins.push(remarkPrism); + rehypePlugins.push([rehypeRaw, { passThrough: nodeTypes }]); + } + + remarkPlugins.push(remarkFrontmatter); + remarkPlugins.push([ + remarkMdxFrontmatter, + { + name: 'frontmatter', + ...mdxOptions.frontmatterOptions, + }, + ]); + updateConfig({ vite: { plugins: [ { enforce: 'pre', ...mdxPlugin({ - 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, - }, - ], - ], - rehypePlugins: handleExtends(mdxOptions.rehypePlugins), + remarkPlugins, + rehypePlugins, jsx: true, jsxImportSource: 'astro', // Note: disable `.md` support |