Very fast and lightweight. Each line is highlighted once, and only the line being written gets updated, so it stays fast even when the code is long. No flicker, no dependencies.
Add two files and every <pre><code class="language-xxx"> block is highlighted. No build step. The code boxes on this page work this way.
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/codelight.js/build/styles/default.min.css">
<script src="https://cdn.jsdelivr.net/npm/codelight.js/build/codelight.min.js"></script>
<script>codelight.highlightAll();</script>Mount a view once, then write each chunk as it arrives. Finished lines never change. The chat above works this way.
const view = codelight.mount(codeElement, { lang: "py" });
stream.on("data", (chunk) => view.write(chunk));npm install codelight.js, then pass the whole reply on every update. Only the new part is highlighted.
import { CodeLight } from "codelight.js/react";
import "codelight.js/styles/default.css";
<CodeLight code={partialCode} lang="ts" />Find code blocks in a reply that's still streaming, including a fence that hasn't closed yet.
import { splitFences } from "codelight.js";
for (const part of splitFences(replySoFar)) {
if (part.type === "code") views[i].set(part.code);
}19 languages, all in the one 8 KB file. Pick one to see it highlighted.