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
42 changes: 42 additions & 0 deletions .changeset/cold-snakes-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
"@astrojs/markdown-remark": minor
---

Adds a `data-language` attribute on the rendered `pre` elements to expose the highlighted syntax language.

For example, the following markdown fenced-codeblock will expose `data-language="python"`:
matthewp marked this conversation as resolved.
Show resolved Hide resolved
```
\```python
def func():
print('Hello Astro!')
\```
```

This allows retrieving the language in a rehype plugin:
matthewp marked this conversation as resolved.
Show resolved Hide resolved
```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;
[...]
});
};
}
```

Note: if using the built-in `<Code>` component, the output of the component being flattened html, the replacement `<pre>` cannot be accessed using `{ tagName: "pre" }`.
matthewp marked this conversation as resolved.
Show resolved Hide resolved
matthewp marked this conversation as resolved.
Show resolved Hide resolved


It also allows to use the `data-language` attribute in css rules:
matthewp marked this conversation as resolved.
Show resolved Hide resolved
matthewp marked this conversation as resolved.
Show resolved Hide resolved
```css
pre::before {
content: attr(data-language);
}

pre[data-language="javascript"] {
font-size: 2rem;
}
```

2 changes: 1 addition & 1 deletion packages/markdown/remark/src/rehype-prism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const rehypePrism: Plugin<[], Root> = () => {
let { html, classLanguage } = runHighlighterWithAstro(language, code);

return Promise.resolve(
`<pre class="${classLanguage}"><code is:raw class="${classLanguage}">${html}</code></pre>`
`<pre data-language="${language}" class="${classLanguage}"><code is:raw class="${classLanguage}">${html}</code></pre>`
bluwy marked this conversation as resolved.
Show resolved Hide resolved
);
});
};
Expand Down
5 changes: 4 additions & 1 deletion packages/markdown/remark/src/shiki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ export async function createShikiHighlighter({

// Replace "shiki" class naming with "astro-code"
node.properties.class = classValue.replace(/shiki/g, 'astro-code');


// Add data-language attribute
node.properties.dataLanguage = lang;

// Handle code wrapping
// if wrap=null, do nothing.
if (wrap === false) {
Expand Down
Loading