Skip to content

Commit ccafa8d

Browse files
604qgcbluwymatthewpsarah11918
authored
Adds dataLanguage property to the replacement <pre> element. (#10538)
* Update highlight.ts * Create cold-snakes-train.md * Update Code.astro Solution for use-case described in withastro/roadmap#276 (withastro/roadmap#276) * roll-back initial fix * new fix * update changeset * Update packages/markdown/remark/src/rehype-prism.ts * Update .changeset/cold-snakes-train.md Co-authored-by: Sarah Rainsberger <[email protected]> * Update .changeset/cold-snakes-train.md Co-authored-by: Sarah Rainsberger <[email protected]> * Update .changeset/cold-snakes-train.md Co-authored-by: Sarah Rainsberger <[email protected]> * Update .changeset/cold-snakes-train.md Co-authored-by: Sarah Rainsberger <[email protected]> --------- Co-authored-by: Bjorn Lu <[email protected]> Co-authored-by: Matthew Phillips <[email protected]> Co-authored-by: Sarah Rainsberger <[email protected]>
1 parent 39988ef commit ccafa8d

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

.changeset/cold-snakes-train.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
"@astrojs/markdown-remark": minor
3+
---
4+
5+
Adds a `data-language` attribute on the rendered `pre` elements to expose the highlighted syntax language.
6+
7+
For example, the following Markdown code block will expose `data-language="python"`:
8+
```
9+
\```python
10+
def func():
11+
print('Hello Astro!')
12+
\```
13+
```
14+
15+
This allows retrieving the language in a rehype plugin from `node.properties.dataLanguage` by accessing the `<pre>` element using `{ tagName: "pre" }`:
16+
```js
17+
// myRehypePre.js
18+
import { visit } from "unist-util-visit";
19+
export default function myRehypePre() {
20+
return (tree) => {
21+
visit(tree, { tagName: "pre" }, (node) => {
22+
const lang = node.properties.dataLanguage;
23+
[...]
24+
});
25+
};
26+
}
27+
```
28+
29+
Note: The `<pre>` element is not exposed when using Astro's `<Code />` component which outputs flattened HTML.
30+
31+
32+
The `data-language` attribute may also be used in css rules:
33+
```css
34+
pre::before {
35+
content: attr(data-language);
36+
}
37+
38+
pre[data-language="javascript"] {
39+
font-size: 2rem;
40+
}
41+
```
42+

packages/markdown/remark/src/rehype-prism.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const rehypePrism: Plugin<[], Root> = () => {
99
let { html, classLanguage } = runHighlighterWithAstro(language, code);
1010

1111
return Promise.resolve(
12-
`<pre class="${classLanguage}"><code is:raw class="${classLanguage}">${html}</code></pre>`
12+
`<pre class="${classLanguage}" data-language="${language}"><code is:raw class="${classLanguage}">${html}</code></pre>`
1313
);
1414
});
1515
};

packages/markdown/remark/src/shiki.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@ export async function createShikiHighlighter({
102102

103103
// Replace "shiki" class naming with "astro-code"
104104
node.properties.class = classValue.replace(/shiki/g, 'astro-code');
105-
105+
106+
// Add data-language attribute
107+
node.properties.dataLanguage = lang;
108+
106109
// Handle code wrapping
107110
// if wrap=null, do nothing.
108111
if (wrap === false) {

0 commit comments

Comments
 (0)