Skip to content

Commit 74f41e5

Browse files
⚡ Consolidate logic for highlighting in one place
1 parent 413e5e8 commit 74f41e5

File tree

5 files changed

+48
-42
lines changed

5 files changed

+48
-42
lines changed

src/class/HTMLCodeBlockElement.ts

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,20 @@
1-
import {HLJSApi, HighlightOptions} from 'highlight.js';
2-
31
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-
122
/**
133
* 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`.
146
* @param src - Source code string for highlight
15-
* @param options - Option for library of highlight
7+
* @param options - Option for highlight
168
* @returns - Object of the highlight result
179
*/
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+
};
4518

4619
#slots = (() => {
4720
/**
@@ -113,7 +86,7 @@ export default class HTMLCodeBlockElement extends HTMLElement {
11386
})()
11487

11588
/** The resulting syntax-highlighted markup */
116-
const {value: markup} = HTMLCodeBlockElement.highlight(src, {
89+
const {markup} = HTMLCodeBlockElement.highlight(src, {
11790
language: this.#language,
11891
});
11992

src/index.all.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import hljs from 'highlight.js';
22
import CustomElementConstructor from './class/HTMLCodeBlockElement';
3+
import {mkHighlightCallback} from './utils/highlight';
34
import './utils/add-style';
45

5-
CustomElementConstructor.endgine = hljs;
6+
CustomElementConstructor.highlight = mkHighlightCallback(hljs);
67
customElements.define('code-block', CustomElementConstructor);
78

89
export const HTMLCodeBlockElement = CustomElementConstructor;

src/index.common.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import hljs from 'highlight.js/lib/common';
22
import CustomElementConstructor from './class/HTMLCodeBlockElement';
3+
import {mkHighlightCallback} from './utils/highlight';
34
import './utils/add-style';
45

5-
CustomElementConstructor.endgine = hljs;
6+
CustomElementConstructor.highlight = mkHighlightCallback(hljs);
67
customElements.define('code-block', CustomElementConstructor);
78

89
export const HTMLCodeBlockElement = CustomElementConstructor;

src/index.core.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import hljs from 'highlight.js/lib/core';
22
import CustomElementConstructor from './class/HTMLCodeBlockElement';
3+
import {mkHighlightCallback} from './utils/highlight';
34
import './utils/add-style';
45

5-
CustomElementConstructor.endgine = hljs;
6+
CustomElementConstructor.highlight = mkHighlightCallback(hljs);
67
customElements.define('code-block', CustomElementConstructor);
78

89
export const HTMLCodeBlockElement = CustomElementConstructor;

src/utils/highlight.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {HLJSApi, HighlightOptions} from 'highlight.js';
2+
3+
/**
4+
* Callback maker for highlight.js
5+
* @param endgine - A library for performing syntax highlighting.
6+
* @returns - A function for HTMLCodeBlockElement.highlight
7+
*/
8+
export const mkHighlightCallback = (endgine: HLJSApi) => (
9+
src: string,
10+
options?: HighlightOptions,
11+
) => {
12+
const hljs: HLJSApi = endgine;
13+
14+
if (
15+
// Verifying the existence of a language
16+
options?.language &&
17+
hljs.getLanguage(options.language)
18+
) {
19+
return {
20+
markup: hljs.highlight(
21+
src,
22+
options,
23+
).value,
24+
};
25+
}
26+
27+
return {
28+
markup: hljs.highlightAuto(src).value,
29+
};
30+
};

0 commit comments

Comments
 (0)