From 5bb35f858fb6ea15c11bbec76565d86703b1224d Mon Sep 17 00:00:00 2001 From: bluwy Date: Tue, 14 Feb 2023 16:06:13 +0100 Subject: [PATCH 1/2] Support rehype plugins that inject namespaced attributes --- .changeset/moody-cats-train.md | 5 +++ packages/integrations/mdx/package.json | 4 +-- packages/integrations/mdx/src/index.ts | 1 + .../integrations/mdx/test/mdx-plugins.test.js | 35 +++++++++++++++++++ pnpm-lock.yaml | 18 +++++----- 5 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 .changeset/moody-cats-train.md diff --git a/.changeset/moody-cats-train.md b/.changeset/moody-cats-train.md new file mode 100644 index 000000000000..bb52f73a1eae --- /dev/null +++ b/.changeset/moody-cats-train.md @@ -0,0 +1,5 @@ +--- +'@astrojs/mdx': patch +--- + +Support rehype plugins that inject namespaced attributes diff --git a/packages/integrations/mdx/package.json b/packages/integrations/mdx/package.json index 70421c806d07..4bf3b4d4a2d3 100644 --- a/packages/integrations/mdx/package.json +++ b/packages/integrations/mdx/package.json @@ -32,8 +32,8 @@ "dependencies": { "@astrojs/markdown-remark": "^2.0.1", "@astrojs/prism": "^2.0.0", - "@mdx-js/mdx": "^2.1.2", - "@mdx-js/rollup": "^2.1.1", + "@mdx-js/mdx": "^2.3.0", + "@mdx-js/rollup": "^2.3.0", "acorn": "^8.8.0", "es-module-lexer": "^0.10.5", "estree-util-visit": "^1.2.0", diff --git a/packages/integrations/mdx/src/index.ts b/packages/integrations/mdx/src/index.ts index 577d073ee19b..7012849a4921 100644 --- a/packages/integrations/mdx/src/index.ts +++ b/packages/integrations/mdx/src/index.ts @@ -74,6 +74,7 @@ export default function mdx(partialMdxOptions: Partial = {}): AstroI const { data: frontmatter, content: pageContent } = parseFrontmatter(code, id); const compiled = await mdxCompile(new VFile({ value: pageContent, path: id }), { ...mdxPluginOpts, + elementAttributeNameCase: 'html', remarkPlugins: [ // Ensure `data.astro` is available to all remark plugins toRemarkInitializeAstroData({ userFrontmatter: frontmatter }), diff --git a/packages/integrations/mdx/test/mdx-plugins.test.js b/packages/integrations/mdx/test/mdx-plugins.test.js index 828bcb3d5a0c..eb63a72d6fde 100644 --- a/packages/integrations/mdx/test/mdx-plugins.test.js +++ b/packages/integrations/mdx/test/mdx-plugins.test.js @@ -63,6 +63,20 @@ describe('MDX plugins', () => { expect(selectRehypeExample(document)).to.not.be.null; }); + it('supports custom rehype plugins with namespaced attributes', async () => { + const fixture = await buildFixture({ + integrations: [ + mdx({ + rehypePlugins: [rehypeSvgPlugin], + }), + ], + }); + const html = await fixture.readFile(FILE); + const { document } = parseHTML(html); + + expect(selectRehypeSvg(document)).to.not.be.null; + }); + it('extends markdown config by default', async () => { const fixture = await buildFixture({ markdown: { @@ -207,6 +221,23 @@ function rehypeExamplePlugin() { }; } +function rehypeSvgPlugin() { + return (tree) => { + tree.children.push({ + type: 'element', + tagName: 'svg', + properties: { xmlns:"http://www.w3.org/2000/svg" }, + children: [ + { + type: 'element', + tagName: 'use', + properties: { 'xlink:href': '#icon' } + } + ] + }); + }; +} + function recmaExamplePlugin() { return (tree) => { estreeVisit(tree, (node) => { @@ -245,6 +276,10 @@ function selectRehypeExample(document) { return document.querySelector('div[data-rehype-plugin-works]'); } +function selectRehypeSvg(document) { + return document.querySelector('svg > use[xlink\\:href]'); +} + function selectRecmaExample(document) { return document.querySelector('div[data-recma-plugin-works]'); } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6d665afdb1e7..26c20bcf1fce 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2877,8 +2877,8 @@ importers: specifiers: '@astrojs/markdown-remark': ^2.0.1 '@astrojs/prism': ^2.0.0 - '@mdx-js/mdx': ^2.1.2 - '@mdx-js/rollup': ^2.1.1 + '@mdx-js/mdx': ^2.3.0 + '@mdx-js/rollup': ^2.3.0 '@types/chai': ^4.3.1 '@types/estree': ^1.0.0 '@types/github-slugger': ^1.3.0 @@ -2915,8 +2915,8 @@ importers: dependencies: '@astrojs/markdown-remark': link:../../markdown/remark '@astrojs/prism': link:../../astro-prism - '@mdx-js/mdx': 2.2.1 - '@mdx-js/rollup': 2.2.1 + '@mdx-js/mdx': 2.3.0 + '@mdx-js/rollup': 2.3.0 acorn: 8.8.2 es-module-lexer: 0.10.5 estree-util-visit: 1.2.1 @@ -6401,8 +6401,8 @@ packages: - supports-color dev: false - /@mdx-js/mdx/2.2.1: - resolution: {integrity: sha512-hZ3ex7exYLJn6FfReq8yTvA6TE53uW9UHJQM9IlSauOuS55J9y8RtA7W+dzp6Yrzr00/U1sd7q+Wf61q6SfiTQ==} + /@mdx-js/mdx/2.3.0: + resolution: {integrity: sha512-jLuwRlz8DQfQNiUCJR50Y09CGPq3fLtmtUQfVrj79E0JWu3dvsVcxVIcfhR5h0iXu+/z++zDrYeiJqifRynJkA==} dependencies: '@types/estree-jsx': 1.0.0 '@types/mdx': 2.0.3 @@ -6425,15 +6425,15 @@ packages: - supports-color dev: false - /@mdx-js/rollup/2.2.1: - resolution: {integrity: sha512-wpGeK9iO7gPEIyC/ZTiggLY/MkEWDj5IWSsjlpkefgjb5RbmUukXU6/D2rHA+VAopxigS3NlaIL2ctpYBi4fmg==} + /@mdx-js/rollup/2.3.0: + resolution: {integrity: sha512-wLvRfJS/M4UmdqTd+WoaySEE7q4BIejYf1xAHXYvtT1du/1Tl/z2450Gg2+Hu7fh05KwRRiehiTP9Yc/Dtn0fA==} peerDependencies: rollup: '>=2' peerDependenciesMeta: rollup: optional: true dependencies: - '@mdx-js/mdx': 2.2.1 + '@mdx-js/mdx': 2.3.0 '@rollup/pluginutils': 5.0.2 source-map: 0.7.4 vfile: 5.3.7 From 98f65993cc400c3aeddeafdeb6dc11a705a46e78 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Tue, 14 Feb 2023 16:58:16 +0100 Subject: [PATCH 2/2] Fix rehype property casing --- packages/integrations/mdx/test/mdx-plugins.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/integrations/mdx/test/mdx-plugins.test.js b/packages/integrations/mdx/test/mdx-plugins.test.js index eb63a72d6fde..7d4e4fe9aa58 100644 --- a/packages/integrations/mdx/test/mdx-plugins.test.js +++ b/packages/integrations/mdx/test/mdx-plugins.test.js @@ -231,7 +231,7 @@ function rehypeSvgPlugin() { { type: 'element', tagName: 'use', - properties: { 'xlink:href': '#icon' } + properties: { 'xLinkHref': '#icon' } } ] });