2022-08-30 17:38:35 +00:00
import { compile as mdxCompile } from '@mdx-js/mdx' ;
2022-09-26 22:25:48 +00:00
import { PluggableList } from '@mdx-js/mdx/lib/core.js' ;
2022-07-21 20:43:58 +00:00
import mdxPlugin , { Options as MdxRollupPluginOptions } from '@mdx-js/rollup' ;
2022-09-26 22:25:48 +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-30 17:41:06 +00:00
import { blue , bold } from 'kleur/colors' ;
2022-09-26 22:23:47 +00:00
import fs from 'node:fs/promises' ;
2022-11-22 15:15:03 +00:00
import type { Options as RemarkRehypeOptions } from 'remark-rehype' ;
2022-09-26 22:25:48 +00:00
import { VFile } from 'vfile' ;
import type { Plugin as VitePlugin } from 'vite' ;
2022-08-30 17:38:35 +00:00
import {
getRehypePlugins ,
getRemarkPlugins ,
2022-09-26 22:25:48 +00:00
recmaInjectImportMetaEnvPlugin ,
rehypeApplyFrontmatterExport ,
2022-09-26 22:23:47 +00:00
} from './plugins.js' ;
2022-09-26 22:25:48 +00:00
import { getFileInfo , handleExtendsNotSupported , parseFrontmatter } 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-09-26 22:23:47 +00:00
export type MdxOptions = {
remarkPlugins? : PluggableList ;
rehypePlugins? : PluggableList ;
2022-10-24 18:27:37 +00:00
recmaPlugins? : PluggableList ;
2022-09-26 22:23:47 +00:00
/ * *
* Choose which remark and rehype plugins to inherit , if any .
*
* - "markdown" ( default ) - inherit your project ’ s markdown plugin config ( [ see Markdown docs ] ( https : //docs.astro.build/en/guides/markdown-content/#configuring-markdown))
* - "astroDefaults" - inherit Astro ’ s default plugins only ( [ see defaults ] ( https : //docs.astro.build/en/reference/configuration-reference/#markdownextenddefaultplugins))
* - false - do not inherit any plugins
* /
extendPlugins ? : 'markdown' | 'astroDefaults' | false ;
2022-11-22 15:12:25 +00:00
remarkRehype? : RemarkRehypeOptions ;
2022-09-26 22:23:47 +00:00
} ;
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 )
) {
2022-10-03 15:37:21 +00:00
console . info (
2022-08-30 17:38:35 +00:00
blue ( ` [MDX] Now inheriting remark and rehype plugins from "markdown" config. ` )
) ;
2022-10-03 15:37:21 +00:00
console . info (
2022-08-30 17:38:35 +00:00
` If you applied a plugin to both your Markdown and MDX configs, we suggest ${ bold (
'removing the duplicate MDX entry.'
) } `
) ;
2022-10-03 15:37:21 +00:00
console . info ( ` See "extendPlugins" option to configure this behavior. ` ) ;
2022-08-30 17:38:35 +00:00
}
2022-07-21 20:43:58 +00:00
2022-11-22 15:12:25 +00:00
let remarkRehypeOptions = mdxOptions . remarkRehype ;
if ( mdxOptions . extendPlugins === 'markdown' ) {
remarkRehypeOptions = {
. . . config . markdown . remarkRehype ,
. . . remarkRehypeOptions ,
} ;
}
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-10-24 18:27:37 +00:00
recmaPlugins : mdxOptions.recmaPlugins ,
2022-07-29 15:22:57 +00:00
jsx : true ,
jsxImportSource : 'astro' ,
2022-10-26 14:18:49 +00:00
// Note: disable `.md` (and other alternative extensions for markdown files like `.markdown`) support
2022-07-29 15:22:57 +00:00
format : 'mdx' ,
mdExtensions : [ ] ,
2022-11-22 15:12:25 +00:00
remarkRehypeOptions ,
2022-08-01 21:23:56 +00:00
} ;
2022-07-29 15:22:57 +00:00
2022-09-26 22:23:47 +00:00
let importMetaEnv : Record < string , any > = {
SITE : config.site ,
} ;
2022-06-30 18:11:12 +00:00
updateConfig ( {
vite : {
plugins : [
{
enforce : 'pre' ,
2022-08-01 21:23:56 +00:00
. . . mdxPlugin ( mdxPluginOpts ) ,
2022-09-26 22:23:47 +00:00
configResolved ( resolved ) {
importMetaEnv = { . . . importMetaEnv , . . . resolved . env } ;
} ,
2022-08-01 21:23:56 +00:00
// Override transform to alter code before MDX compilation
// ex. inject layouts
2022-09-26 22:23:47 +00:00
async transform ( _ , id ) {
2022-08-01 21:23:56 +00:00
if ( ! id . endsWith ( 'mdx' ) ) return ;
2022-07-29 15:22:57 +00:00
2022-09-26 22:23:47 +00:00
// Read code from file manually to prevent Vite from parsing `import.meta.env` expressions
const { fileId } = getFileInfo ( id , config ) ;
const code = await fs . readFile ( fileId , 'utf-8' ) ;
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-10-24 18:27:37 +00:00
recmaPlugins : [
. . . ( mdxPluginOpts . recmaPlugins ? ? [ ] ) ,
( ) = > recmaInjectImportMetaEnvPlugin ( { importMetaEnv } ) ,
] ,
2022-08-05 23:55:38 +00:00
} ) ;
2022-08-01 21:23:56 +00:00
return {
2022-09-26 22:23:47 +00:00
code : escapeViteEnvReferences ( String ( compiled . value ) ) ,
2022-08-01 21:23:56 +00:00
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
2022-12-05 20:56:43 +00:00
const [ moduleImports , moduleExports ] = parseESM ( code ) ;
// Fragment import should already be injected, but check just to be safe.
const importsFromJSXRuntime = moduleImports
. filter ( ( { n } ) = > n === 'astro/jsx-runtime' )
. map ( ( { ss , se } ) = > code . substring ( ss , se ) ) ;
const hasFragmentImport = importsFromJSXRuntime . some ( ( statement ) = >
/[\s,{](Fragment,|Fragment\s*})/ . test ( statement )
) ;
if ( ! hasFragmentImport ) {
code = 'import { Fragment } from "astro/jsx-runtime"\n' + code ;
}
2022-07-20 14:56:32 +00:00
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' ) ) {
2022-12-05 20:56:43 +00:00
// Make `Content` the default export so we can wrap `MDXContent` and pass in `Fragment`
code = code . replace ( 'export default MDXContent;' , '' ) ;
code += ` \ nexport const Content = (props = {}) => MDXContent({
. . . props ,
components : { Fragment , . . . props . components } ,
} ) ;
export default Content ; ` ;
2022-08-12 22:17:26 +00:00
}
2022-07-28 14:58:44 +00:00
2022-12-05 20:56:43 +00:00
// Ensures styles and scripts are injected into a `<head>`
// When a layout is not applied
code += ` \ nContent[Symbol.for('astro.needsHeadRendering')] = !Boolean(frontmatter.layout); ` ;
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
}
2022-09-26 22:23:47 +00:00
return escapeViteEnvReferences ( 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
}
2022-09-26 22:23:47 +00:00
// Converts the first dot in `import.meta.env` to its Unicode escape sequence,
// which prevents Vite from replacing strings like `import.meta.env.SITE`
// in our JS representation of loaded Markdown files
function escapeViteEnvReferences ( code : string ) {
return code . replace ( /import\.meta\.env/g , 'import\\u002Emeta.env' ) ;
}