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

Adds dataLanguage property to the replacement <pre> element. #10538

Merged
merged 14 commits into from
Apr 10, 2024
5 changes: 5 additions & 0 deletions .changeset/cold-snakes-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/markdown-remark": minor
---

Adds a `data-language` attribute on the rendered `pre` and `code` elements to expose the highlighted syntax language
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a usage example?

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Adds a `data-language` attribute on the rendered `pre` and `code` elements to expose the highlighted syntax language
Adds a `data-language` attribute on the rendered `pre` and `code` elements to expose the highlighted syntax language.
This allows retrieving the language in a rehype plugin:
```js
// myRehypePre.js
import { visit } from "unist-util-visit";
export default function myRehypePre() {
return (tree) => {
visit(tree, { tagName: "pre" }, (node) => {
const lang = node.properties.dataLanguage;
[...]
});
};
}
```

Hi @604qgc ! I was thinking of including something like this in the changeset, since for minor releases of new features we do like to show them off a bit and really help people understand why your thing is cool! 💅

I took this from your PR description, but if there's something else you'd prefer to use, or would change this in some way, please do! I think a changeset of this style/length is helpful to people to understand the new feature, so anything like this you can suggest would be great.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you @sarah11918 !
I added some more notes for clarity, but happy to trim if that's a bit too long db66313

5 changes: 3 additions & 2 deletions packages/astro/components/Code.astro
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ const highlighter = await getCachedHighlighter({
wrap,
});

const html = await highlighter.highlight(code, typeof lang === 'string' ? lang : lang.name, {
const finalLang = typeof lang === 'string' ? lang : lang.name
const html = await highlighter.highlight(code, finalLang, {
inline,
attributes: rest as any,
attributes: {...rest, dataLanguage: finalLang} as any,
});
---

Expand Down
4 changes: 4 additions & 0 deletions packages/markdown/remark/src/highlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export async function highlightCodeBlocks(tree: Root, highlighter: Highlighter)
const html = await highlighter(code, language, { meta });
// The replacement returns a root node with 1 child, the `<pr>` element replacement.
const replacement = fromHtml(html, { fragment: true }).children[0] as Element;

// Adds dataLanguage property to replacement `<pre>`
replacement.properties.dataLanguage = language

// We just generated this node, so any positional information is invalid.
removePosition(replacement);

Expand Down
Loading