Skip to content

Commit 5aa99e3

Browse files
committed
Install and configure highlight.js.
1 parent 81794d8 commit 5aa99e3

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

browser-extension/package-lock.json

Lines changed: 14 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

browser-extension/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"author": "DiffPlug",
33
"dependencies": {
44
"@wxt-dev/webextension-polyfill": "^1.0.0",
5+
"highlight.js": "^11.11.1",
56
"overtype": "^1.2.3",
67
"webextension-polyfill": "^0.12.0"
78
},

browser-extension/src/entrypoints/content.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import hljs from "highlight.js";
12
import OverType from "../overtype/overtype";
23

34
export default defineContentScript({
45
main() {
56
if (window.location.hostname !== "github.com") {
67
return;
78
}
8-
9+
OverType.setCodeHighlighter(hljsHighlighter);
910
const ghCommentBox = document.getElementById(
1011
"new_comment_field"
1112
) as HTMLTextAreaElement | null;
@@ -35,3 +36,18 @@ function modifyDOM(overtypeInput: HTMLTextAreaElement): HTMLElement {
3536
overtypeContainer.classList.add("overtype-container");
3637
return overtypeContainer.parentElement!.closest("div")!;
3738
}
39+
40+
function hljsHighlighter(code: string, language: string) {
41+
try {
42+
if (language && hljs.getLanguage(language)) {
43+
const result = hljs.highlight(code, { language });
44+
return result.value;
45+
} else {
46+
const result = hljs.highlightAuto(code);
47+
return result.value;
48+
}
49+
} catch (error) {
50+
console.warn("highlight.js highlighting failed:", error);
51+
return code;
52+
}
53+
}

0 commit comments

Comments
 (0)