Skip to content

Commit cdd9506

Browse files
committed
lift lang from pre attribute to code classname
1 parent cfa4391 commit cdd9506

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/plugins/rehype/filter-elements.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const ALLOWED_ELEMENTS = [
3838
"div",
3939
"dl",
4040
"dt",
41-
"figcaption",
41+
// "figcaption",
4242
"figure",
4343
"hr",
4444
"li",
@@ -96,6 +96,8 @@ const ALLOWED_ELEMENTS = [
9696

9797
const ALLOWED_ATTRIBUTES: Record<string, string[]> = {
9898
a: ["href", "id", "target"],
99+
pre: ["dataLanguage"],
100+
code: ["className"],
99101
"rule-id": ["id"],
100102
};
101103

@@ -128,6 +130,18 @@ export default function () {
128130
}
129131
}
130132

133+
if (tag === "pre") {
134+
const language = element.properties.dataLanguage;
135+
if (!language) return;
136+
137+
const code = element.children.find(
138+
(child) => child.type === "element" && child.tagName === "code",
139+
);
140+
if (!code) return;
141+
142+
(code as Element).properties.className = [`language-${language}`];
143+
}
144+
131145
if (tag === "rule-id") {
132146
return unwrap(index, parent, {
133147
...element,

0 commit comments

Comments
 (0)