-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
code_highlight.ts
67 lines (58 loc) 路 1.86 KB
/
code_highlight.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import hljs, { HLJSOptions, LanguageFn } from "../deps/highlight.ts";
import { merge } from "../core/utils/object.ts";
import { log } from "../core/utils/log.ts";
import type Site from "../core/site.ts";
import type { Page } from "../core/file.ts";
export interface Options {
/** The list of extensions this plugin applies to */
extensions?: string[];
/** Register languages on the Highlight.js context. */
languages?: Record<string, LanguageFn>;
/**
* Options passed to highlight.js.
* @see https://highlightjs.readthedocs.io/en/latest/api.html#configure
*/
options?: Omit<HLJSOptions, "__emitter">;
}
// Default options
export const defaults: Options = {
extensions: [".html"],
options: {
ignoreUnescapedHTML: false,
noHighlightRe: /^$/i,
languageDetectRe: /\blanguage-([\w-]+)\b/i,
classPrefix: "hljs-",
cssSelector: "pre code",
languages: undefined,
},
};
/** A plugin to syntax-highlight code using the highlight.js library */
export default function (userOptions?: Options) {
const options = merge(defaults, userOptions);
hljs.configure(options.options);
if (options.languages) {
for (const [name, fn] of Object.entries(options.languages)) {
hljs.registerLanguage(name, fn);
}
}
return (site: Site) => {
site.process(options.extensions, processCodeHighlight);
function processCodeHighlight(pages: Page[]) {
for (const page of pages) {
page.document!.querySelectorAll<HTMLElement>(
options.options.cssSelector,
)
.forEach((element) => {
try {
hljs.highlightElement(element);
element.removeAttribute("data-highlighted");
} catch (error) {
log.error(
`Error highlighting code block in ${page.sourcePath}: ${error}`,
);
}
});
}
}
};
}