Skip to content

Commit

Permalink
Preserve code element node meta for rehype syntax highlighters (#5335)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Nov 9, 2022
1 parent dc00ca4 commit dca762c
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/heavy-kangaroos-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/mdx': patch
---

Preserve code element node `data.meta` in `properties.metastring` for rehype syntax highlighters, like `rehype-pretty-code``
1 change: 1 addition & 0 deletions packages/integrations/mdx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"github-slugger": "^1.4.0",
"gray-matter": "^4.0.3",
"kleur": "^4.1.4",
"rehype-pretty-code": "^0.4.0",
"rehype-raw": "^6.1.1",
"remark-frontmatter": "^4.0.1",
"remark-gfm": "^3.0.1",
Expand Down
3 changes: 3 additions & 0 deletions packages/integrations/mdx/src/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import remarkSmartypants from 'remark-smartypants';
import type { Data, VFile } from 'vfile';
import { MdxOptions } from './index.js';
import rehypeCollectHeadings from './rehype-collect-headings.js';
import rehypeMetaString from './rehype-meta-string.js';
import remarkPrism from './remark-prism.js';
import remarkShiki from './remark-shiki.js';
import { jsToTreeNode } from './utils.js';
Expand Down Expand Up @@ -150,6 +151,8 @@ export function getRehypePlugins(
let rehypePlugins: PluggableList = [
// getHeadings() is guaranteed by TS, so we can't allow user to override
rehypeCollectHeadings,
// ensure `data.meta` is preserved in `properties.metastring` for rehype syntax highlighters
rehypeMetaString,
// rehypeRaw allows custom syntax highlighters to work without added config
[rehypeRaw, { passThrough: nodeTypes }] as any,
];
Expand Down
17 changes: 17 additions & 0 deletions packages/integrations/mdx/src/rehype-meta-string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { visit } from 'unist-util-visit';

/**
* Moves `data.meta` to `properties.metastring` for the `code` element node
* as `rehype-raw` strips `data` from all nodes, which may contain useful information.
* e.g. ```js {1:3} => metastring: "{1:3}"
*/
export default function rehypeMetaString() {
return function (tree: any) {
visit(tree, (node) => {
if (node.type === 'element' && node.tagName === 'code' && node.data?.meta) {
node.properties ??= {};
node.properties.metastring = node.data.meta;
}
});
};
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Syntax highlighting

```astro
```astro {2}
---
const handlesAstroSyntax = true
---
Expand Down
28 changes: 28 additions & 0 deletions packages/integrations/mdx/test/mdx-syntax-highlighting.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 shikiTwoslash from 'remark-shiki-twoslash';
import rehypePrettyCode from 'rehype-pretty-code';

const FIXTURE_ROOT = new URL('./fixtures/mdx-syntax-hightlighting/', import.meta.url);

Expand Down Expand Up @@ -88,4 +89,31 @@ describe('MDX syntax highlighting', () => {
const twoslashCodeBlock = document.querySelector('pre.shiki');
expect(twoslashCodeBlock).to.not.be.null;
});

it('supports custom highlighter - rehype-pretty-code', async () => {
const fixture = await loadFixture({
root: FIXTURE_ROOT,
markdown: {
syntaxHighlight: false,
},
integrations: [
mdx({
rehypePlugins: [
[
rehypePrettyCode,
{
onVisitHighlightedLine(node) {
node.properties.style = 'background-color:#000000';
},
},
],
],
}),
],
});
await fixture.build();

const html = await fixture.readFile('/index.html');
expect(html).to.include('style="background-color:#000000"')
});
});
16 changes: 16 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dca762c

Please sign in to comment.