diff options
author | 2022-07-21 16:43:58 -0400 | |
---|---|---|
committer | 2022-07-21 16:43:58 -0400 | |
commit | 3b8a7445247221100462ba035f6778b43ea180e7 (patch) | |
tree | c242680269c56ef20c121097432c6874bf5a2d03 /packages/integrations/mdx/src/index.ts | |
parent | 3f7b5f155e85dc28b7fc95e4386a304aa9e80cdd (diff) | |
download | astro-3b8a7445247221100462ba035f6778b43ea180e7.tar.gz astro-3b8a7445247221100462ba035f6778b43ea180e7.tar.zst astro-3b8a7445247221100462ba035f6778b43ea180e7.zip |
[MDX] Add Prism and Shiki support (#4002)
* deps: add rehype-prism, shiki, rehype-pretty-code
* wip: apply rehype plugins depending on config
* wip: cherry-pick jsx-runtime fix?
* deps: rehype-pretty-code -> shiki-twoslash, add rehype-raw
* wip: add jsx-runtime fix
* feat: get shiki working!
* deps: add @astrojs/prism, prismjs, unist-util-visit
* feat: add prism support
* example: add small syntax highlight demo to with-mdx
* deps: remove rehype-prism
* chore: remove unused async
* chore: add .test.js to all mdx tests
* test: shiki, shikiConfig, prism
* fix: remove "is:raw" from prism output
* docs: add syntax highlighting section
* chore: add changeset
* nit: "Shiki config" -> Shiki config
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
* Revert "wip: add jsx-runtime fix"
This reverts commit 07f4528f449281afb7bbc154b09292244795a183.
* docs: link to integration README from example
Co-authored-by: Nate Moore <nate@astro.build>
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
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 |