|
1 | | -import {HLJSApi, HighlightOptions} from 'highlight.js'; |
2 | | - |
3 | 1 | export default class HTMLCodeBlockElement extends HTMLElement { |
4 | | - /** |
5 | | - * A library for performing syntax highlighting. |
6 | | - * Before running `customElements.define()`, |
7 | | - * you need to assign it directly to `HTMLCodeBlockElement.endgine`. |
8 | | - * Currently, only highlight.js is assumed. |
9 | | - */ |
10 | | - static endgine: any; |
11 | | - |
12 | 2 | /** |
13 | 3 | * Returns the result of highlighting the received source code string. |
| 4 | + * Before running `customElements.define()`, |
| 5 | + * you need to assign it directly to `HTMLCodeBlockElement.highlight`. |
14 | 6 | * @param src - Source code string for highlight |
15 | | - * @param options - Option for library of highlight |
| 7 | + * @param options - Option for highlight |
16 | 8 | * @returns - Object of the highlight result |
17 | 9 | */ |
18 | | - static highlight( |
19 | | - src: string, |
20 | | - options?: HighlightOptions, |
21 | | - ): { |
22 | | - value: string, |
23 | | - } { |
24 | | - const {endgine} = HTMLCodeBlockElement; |
25 | | - |
26 | | - if (!endgine) { |
27 | | - throw new Error('The syntax highlighting engine is not set to `HTMLCodeBlockElement.endgine`.'); |
28 | | - } |
29 | | - |
30 | | - const hljs: HLJSApi = endgine; |
31 | | - |
32 | | - if ( |
33 | | - // Verifying the existence of a language |
34 | | - options?.language && |
35 | | - hljs.getLanguage(options.language) |
36 | | - ) { |
37 | | - return hljs.highlight( |
38 | | - src, |
39 | | - options, |
40 | | - ); |
41 | | - } |
42 | | - |
43 | | - return hljs.highlightAuto(src); |
44 | | - } |
| 10 | + static highlight: (src: string, options?: { |
| 11 | + /** Language Mode Name */ |
| 12 | + language: string, |
| 13 | + }) => { |
| 14 | + markup: string, |
| 15 | + } = () => { |
| 16 | + throw new TypeError('The syntax highlighting engine is not set to `HTMLCodeBlockElement.highlight`.'); |
| 17 | + }; |
45 | 18 |
|
46 | 19 | #slots = (() => { |
47 | 20 | /** |
@@ -113,7 +86,7 @@ export default class HTMLCodeBlockElement extends HTMLElement { |
113 | 86 | })() |
114 | 87 |
|
115 | 88 | /** The resulting syntax-highlighted markup */ |
116 | | - const {value: markup} = HTMLCodeBlockElement.highlight(src, { |
| 89 | + const {markup} = HTMLCodeBlockElement.highlight(src, { |
117 | 90 | language: this.#language, |
118 | 91 | }); |
119 | 92 |
|
|
0 commit comments