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

Preserve code element node meta for rehype syntax highlighters #5335

Merged
merged 1 commit into from
Nov 9, 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/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",
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 have been a dev dependency as it’s only used in the test?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh right. I used the wrong install command. Fixing this now, thanks for catching this! 🙏

"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.