2022-08-30 17:38:35 +00:00
import { compile as mdxCompile } from '@mdx-js/mdx' ;
2022-07-21 20:43:58 +00:00
import mdxPlugin , { Options as MdxRollupPluginOptions } from '@mdx-js/rollup' ;
2022-08-30 17:38:35 +00:00
import type { AstroIntegration } from 'astro' ;
2022-07-20 14:56:32 +00:00
import { parse as parseESM } from 'es-module-lexer' ;
2022-08-01 21:25:50 +00:00
import { VFile } from 'vfile' ;
2022-07-29 15:24:57 +00:00
import type { Plugin as VitePlugin } from 'vite' ;
2022-08-30 17:38:35 +00:00
import { bold , blue } from 'kleur/colors' ;
import { rehypeApplyFrontmatterExport } from './astro-data-utils.js' ;
import {
getFileInfo ,
parseFrontmatter ,
handleExtendsNotSupported ,
getRehypePlugins ,
getRemarkPlugins ,
} from './utils.js' ;
import type { MdxOptions } from './utils.js' ;
2022-07-20 18:14:23 +00:00
2022-08-12 22:17:26 +00:00
const RAW_CONTENT_ERROR =
'MDX does not support rawContent()! If you need to read the Markdown contents to calculate values (ex. reading time), we suggest injecting frontmatter via remark plugins. Learn more on our docs: https://docs.astro.build/en/guides/integrations-guide/mdx/#inject-frontmatter-via-remark-or-rehype-plugins' ;
const COMPILED_CONTENT_ERROR =
'MDX does not support compiledContent()! If you need to read the HTML contents to calculate values (ex. reading time), we suggest injecting frontmatter via rehype plugins. Learn more on our docs: https://docs.astro.build/en/guides/integrations-guide/mdx/#inject-frontmatter-via-remark-or-rehype-plugins' ;
2022-07-20 18:14:23 +00:00
export default function mdx ( mdxOptions : MdxOptions = { } ) : AstroIntegration {
2022-06-30 18:09:09 +00:00
return {
2022-06-30 18:11:12 +00:00
name : '@astrojs/mdx' ,
hooks : {
2022-08-15 14:43:12 +00:00
'astro:config:setup' : async ( { updateConfig , config , addPageExtension , command } : any ) = > {
2022-06-30 18:11:12 +00:00
addPageExtension ( '.mdx' ) ;
2022-08-30 17:38:35 +00:00
mdxOptions . extendPlugins ? ? = 'markdown' ;
handleExtendsNotSupported ( mdxOptions . remarkPlugins ) ;
handleExtendsNotSupported ( mdxOptions . rehypePlugins ) ;
// TODO: remove for 1.0. Shipping to ease migration to new minor
if (
mdxOptions . extendPlugins === 'markdown' &&
( config . markdown . rehypePlugins ? . length || config . markdown . remarkPlugins ? . length )
) {
console . log (
blue ( ` [MDX] Now inheriting remark and rehype plugins from "markdown" config. ` )
) ;
console . log (
` If you applied a plugin to both your Markdown and MDX configs, we suggest ${ bold (
'removing the duplicate MDX entry.'
) } `
) ;
console . log ( ` See "extendPlugins" option to configure this behavior. ` ) ;
}
2022-07-21 20:43:58 +00:00
2022-08-01 21:23:56 +00:00
const mdxPluginOpts : MdxRollupPluginOptions = {
2022-08-15 14:43:12 +00:00
remarkPlugins : await getRemarkPlugins ( mdxOptions , config ) ,
2022-08-05 23:55:38 +00:00
rehypePlugins : getRehypePlugins ( mdxOptions , config ) ,
2022-07-29 15:22:57 +00:00
jsx : true ,
jsxImportSource : 'astro' ,
// Note: disable `.md` support
format : 'mdx' ,
mdExtensions : [ ] ,
2022-08-01 21:23:56 +00:00
} ;
2022-07-29 15:22:57 +00:00
2022-06-30 18:11:12 +00:00
updateConfig ( {
vite : {
plugins : [
{
enforce : 'pre' ,
2022-08-01 21:23:56 +00: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 15:22:57 +00:00
2022-08-11 17:36:34 +00:00
const { data : frontmatter , content : pageContent } = parseFrontmatter ( code , id ) ;
2022-08-05 23:55:38 +00:00
const compiled = await mdxCompile ( new VFile ( { value : pageContent , path : id } ) , {
. . . mdxPluginOpts ,
rehypePlugins : [
. . . ( mdxPluginOpts . rehypePlugins ? ? [ ] ) ,
2022-08-08 22:33:35 +00:00
( ) = > rehypeApplyFrontmatterExport ( frontmatter ) ,
2022-08-05 23:55:38 +00:00
] ,
} ) ;
2022-08-01 21:23:56 +00:00
return {
code : String ( compiled . value ) ,
map : compiled.map ,
} ;
2022-07-29 15:24:57 +00:00
} ,
2022-06-30 18:11:12 +00:00
} ,
2022-07-20 14:56:32 +00:00
{
2022-08-01 21:23:56 +00:00
name : '@astrojs/mdx-postprocess' ,
// These transforms must happen *after* JSX runtime transformations
2022-07-29 15:22:57 +00:00
transform ( code , id ) {
2022-06-30 18:11:12 +00:00
if ( ! id . endsWith ( '.mdx' ) ) return ;
2022-08-23 17:25:35 +00:00
// Ensures styles and scripts are injected into a `<head>`
// When a layout is not applied
code += ` \ nMDXContent[Symbol.for('astro.needsHeadRendering')] = !Boolean(frontmatter.layout); ` ;
2022-07-20 14:56:32 +00:00
const [ , moduleExports ] = parseESM ( code ) ;
2022-07-28 14:58:44 +00:00
const { fileUrl , fileId } = getFileInfo ( id , config ) ;
2022-07-20 14:56:32 +00:00
if ( ! moduleExports . includes ( 'url' ) ) {
code += ` \ nexport const url = ${ JSON . stringify ( fileUrl ) } ; ` ;
}
2022-07-28 14:58:44 +00:00
if ( ! moduleExports . includes ( 'file' ) ) {
code += ` \ nexport const file = ${ JSON . stringify ( fileId ) } ; ` ;
}
2022-08-12 22:17:26 +00:00
if ( ! moduleExports . includes ( 'rawContent' ) ) {
code += ` \ nexport function rawContent() { throw new Error( ${ JSON . stringify (
RAW_CONTENT_ERROR
) } ) } ; ` ;
}
if ( ! moduleExports . includes ( 'compiledContent' ) ) {
code += ` \ nexport function compiledContent() { throw new Error( ${ JSON . stringify (
COMPILED_CONTENT_ERROR
) } ) } ; ` ;
}
if ( ! moduleExports . includes ( 'Content' ) ) {
code += ` \ nexport const Content = MDXContent; ` ;
}
2022-07-28 14:58:44 +00:00
2022-07-20 14:56:32 +00:00
if ( command === 'dev' ) {
// TODO: decline HMR updates until we have a stable approach
code += ` \ nif (import.meta.hot) {
2022-06-30 18:09:09 +00:00
import . meta . hot . decline ( ) ;
2022-07-20 14:58:33 +00:00
} ` ;
2022-07-20 14:56:32 +00:00
}
return code ;
2022-06-30 18:11:12 +00:00
} ,
} ,
2022-07-29 15:22:57 +00:00
] as VitePlugin [ ] ,
2022-06-30 18:11:12 +00:00
} ,
} ) ;
} ,
} ,
} ;
2022-06-30 18:09:09 +00:00
}