Rehype plugin to inject language type into the pre element.
npm install @lnkkerst/rehype-pre-language
# or use yarn
yarn add @lnkkerst/rehype-pre-language
# or use pnpm
pnpm install @lnkkerst/rehype-pre-language
import rehypePreLanguage from "@lnkkerst/rehype-pre-language";
import { readFileSync } from "node:fs";
import rehypeParse from "rehype-parse";
import rehypeStringify from "rehype-stringify";
import { unified } from "unified";
const doc = readFileSync("./example.html");
const file = await unified()
.use(rehypeParse)
.use(rehypePreLanguage)
.use(rehypeStringify)
.process(doc);
console.log(String(file));
<html>
<head></head>
<body>
<pre>
<code class="language-javascript">console.log("hello world")</code>
</pre>
</body>
</html>
<html>
<head></head>
<body>
<pre data-language="javascript">
<code class="language-javascript">console.log("hello world")</code>
</pre>
</body>
</html>