Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MDX] Support recmaPlugins config #5146

Merged
merged 4 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/clever-keys-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/mdx': patch
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a minor?

Suggested change
'@astrojs/mdx': patch
'@astrojs/mdx': minor

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this integration is still pre-1.0, we're treating patches as "minors" and minors as "majors." Good comment though 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right didn't notice it was pre 1.0 👍

---

Support recmaPlugins config option
6 changes: 6 additions & 0 deletions packages/integrations/mdx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,12 @@ export default {
}
```

### recmaPlugins

These are plugins that modify the output [estree](https://github.com/estree/estree) directly. This is useful for modifying or injecting JavaScript variables in your MDX files.

We suggest [using AST Explorer](https://astexplorer.net/) to play with estree outputs, and trying [`estree-util-visit`](https://unifiedjs.com/explore/package/estree-util-visit/) for searching across JavaScript nodes.

## Examples

- The [Astro MDX example](https://github.com/withastro/astro/tree/latest/examples/with-mdx) shows how to use MDX files in your Astro project.
Expand Down
3 changes: 2 additions & 1 deletion packages/integrations/mdx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"build": "astro-scripts build \"src/**/*.ts\" && tsc",
"build:ci": "astro-scripts build \"src/**/*.ts\"",
"dev": "astro-scripts dev \"src/**/*.ts\"",
"test": "mocha --exit --timeout 20000"
"test": "mocha --exit --timeout 20000",
"test:match": "mocha --timeout 20000 -g"
},
"dependencies": {
"@astrojs/prism": "^1.0.1",
Expand Down
7 changes: 6 additions & 1 deletion packages/integrations/mdx/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const COMPILED_CONTENT_ERROR =
export type MdxOptions = {
remarkPlugins?: PluggableList;
rehypePlugins?: PluggableList;
recmaPlugins?: PluggableList;
/**
* Choose which remark and rehype plugins to inherit, if any.
*
Expand Down Expand Up @@ -64,6 +65,7 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
const mdxPluginOpts: MdxRollupPluginOptions = {
remarkPlugins: await getRemarkPlugins(mdxOptions, config),
rehypePlugins: getRehypePlugins(mdxOptions, config),
recmaPlugins: mdxOptions.recmaPlugins,
jsx: true,
jsxImportSource: 'astro',
// Note: disable `.md` support
Expand Down Expand Up @@ -100,7 +102,10 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
...(mdxPluginOpts.rehypePlugins ?? []),
() => rehypeApplyFrontmatterExport(frontmatter),
],
recmaPlugins: [() => recmaInjectImportMetaEnvPlugin({ importMetaEnv })],
recmaPlugins: [
...(mdxPluginOpts.recmaPlugins ?? []),
() => recmaInjectImportMetaEnvPlugin({ importMetaEnv }),
],
});

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export let recmaPluginWorking = false

# TOC test

## Table of contents
Expand All @@ -17,3 +19,5 @@ Oh cool, more text!
## Section 2

And section 2, with a hyperlink to check GFM is preserved: https://handle-me-gfm.com

<div data-recma-plugin-works={recmaPluginWorking}></div>
38 changes: 38 additions & 0 deletions packages/integrations/mdx/test/mdx-plugins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { expect } from 'chai';
import { parseHTML } from 'linkedom';
import { loadFixture } from '../../../astro/test/test-utils.js';
import remarkToc from 'remark-toc';
import { visit as estreeVisit } from 'estree-util-visit';

const FIXTURE_ROOT = new URL('./fixtures/mdx-plugins/', import.meta.url);
const FILE = '/with-plugins/index.html';
Expand Down Expand Up @@ -164,6 +165,21 @@ describe('MDX plugins', () => {
expect(selectGfmLink(document)).to.be.null;
expect(selectRemarkExample(document)).to.be.null;
});

it('supports custom recma plugins', async () => {
const fixture = await buildFixture({
integrations: [
mdx({
recmaPlugins: [recmaExamplePlugin],
}),
],
});

const html = await fixture.readFile(FILE);
const { document } = parseHTML(html);

expect(selectRecmaExample(document)).to.not.be.null;
});
});

async function buildFixture(config) {
Expand Down Expand Up @@ -194,6 +210,24 @@ function rehypeExamplePlugin() {
};
}

function recmaExamplePlugin() {
return (tree) => {
estreeVisit(tree, (node) => {
if (
node.type === 'VariableDeclarator' &&
node.id.name === 'recmaPluginWorking' &&
node.init?.type === 'Literal'
) {
node.init = {
...(node.init ?? {}),
value: true,
raw: 'true',
};
}
});
};
}

function selectTocLink(document) {
return document.querySelector('ul a[href="#section-1"]');
}
Expand All @@ -209,3 +243,7 @@ function selectRemarkExample(document) {
function selectRehypeExample(document) {
return document.querySelector('div[data-rehype-plugin-works]');
}

function selectRecmaExample(document) {
return document.querySelector('div[data-recma-plugin-works]');
}