summaryrefslogtreecommitdiff
path: root/packages/integrations/mdx/src
diff options
context:
space:
mode:
authorGravatar Ben Holmes <hey@bholmes.dev> 2022-07-20 14:14:23 -0400
committerGravatar GitHub <noreply@github.com> 2022-07-20 14:14:23 -0400
commit19433eb4a4441522f68492ca914ad2ab4f061343 (patch)
treeea06d6dc4669f0d72fe4c969c913e03317f236eb /packages/integrations/mdx/src
parentd50f46bfab53a2485627330c78b82bb6e1dc57fa (diff)
downloadastro-19433eb4a4441522f68492ca914ad2ab4f061343.tar.gz
astro-19433eb4a4441522f68492ca914ad2ab4f061343.tar.zst
astro-19433eb4a4441522f68492ca914ad2ab4f061343.zip
[MDX] Support remark and rehype plugins, with defaults (#3977)
* feaet: allow remark and rehype plugin config * deps: add remark-gfm, remark-smartypants * feat: add gfm and smartypants by default * test: add GFM and remark plugin tests * feat: preserve default plugins with "extends" * docs: add remarkPlugins * docs: add rehypePlugins * chore: changeset * fix: remove skip from mdx tests * chore: dup hyperlink flavor text * chore: authGen -> autoGen * nit: markdown -> Markdown Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com> * nit: markdown -> Markdown 1 Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com> * nit: markdown -> Markdown 2 Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com> * nit: markdown -> Markdown 3 Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com> * nit: markdown -> Markdown 4 Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com> Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
Diffstat (limited to 'packages/integrations/mdx/src')
-rw-r--r--packages/integrations/mdx/src/index.ts24
1 files changed, 22 insertions, 2 deletions
diff --git a/packages/integrations/mdx/src/index.ts b/packages/integrations/mdx/src/index.ts
index 140e86632..af63c4ad2 100644
--- a/packages/integrations/mdx/src/index.ts
+++ b/packages/integrations/mdx/src/index.ts
@@ -1,9 +1,26 @@
-import mdxPlugin from '@mdx-js/rollup';
+import mdxPlugin, { Options as MdxRollupPluginOptions } from '@mdx-js/rollup';
import type { AstroIntegration } from 'astro';
import { parse as parseESM } from 'es-module-lexer';
+import remarkGfm from 'remark-gfm';
+import remarkSmartypants from 'remark-smartypants';
import { getFileInfo } from './utils.js';
-export default function mdx(): AstroIntegration {
+type WithExtends<T> = T | { extends: T };
+
+type MdxOptions = {
+ remarkPlugins?: WithExtends<MdxRollupPluginOptions['remarkPlugins']>;
+ rehypePlugins?: WithExtends<MdxRollupPluginOptions['rehypePlugins']>;
+}
+
+const DEFAULT_REMARK_PLUGINS = [remarkGfm, remarkSmartypants];
+
+function handleExtends<T>(config: WithExtends<T[] | undefined>, defaults: T[] = []): T[] | undefined {
+ if (Array.isArray(config)) return config;
+
+ return [...defaults, ...(config?.extends ?? [])];
+}
+
+export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
return {
name: '@astrojs/mdx',
hooks: {
@@ -15,6 +32,9 @@ export default function mdx(): AstroIntegration {
{
enforce: 'pre',
...mdxPlugin({
+ remarkPlugins: handleExtends(mdxOptions.remarkPlugins, DEFAULT_REMARK_PLUGINS),
+ rehypePlugins: handleExtends(mdxOptions.rehypePlugins),
+ // place these after so the user can't override
jsx: true,
jsxImportSource: 'astro',
// Note: disable `.md` support