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
64import type Hexo from '../hexo';
export interface HighlightOptions {
lang: string | undefined,
caption: string | undefined,
lines_length?: number | undefined,
// plugins/filter/before_post_render/backtick_code_block
firstLineNumber?: string | number
// plugins/tag/code.ts
language_attr?: boolean | undefined;
firstLine?: string | number;
line_number?: boolean | undefined;
line_threshold?: number | undefined;
mark?: number[] | string;
wrap?: boolean | undefined;
}
interface HighlightExecArgs {
context?: Hexo;
args?: [string, HighlightOptions];
}
interface StoreFunction {
(content: string, options: HighlightOptions): string;
priority?: number;
}
interface Store {
[key: string]: StoreFunction
}
class SyntaxHighlight {
public store: Store;
constructor() {
this.store = {};
}
register(name: string, fn: StoreFunction): void {
if (typeof fn !== 'function') throw new TypeError('fn must be a function');
this.store[name] = fn;
}
query(name: string): StoreFunction {
return name && this.store[name];
}
exec(name: string, options: HighlightExecArgs): string {
const fn = this.store[name];
if (!fn) throw new TypeError(`syntax highlighter ${name} is not registered`);
const ctx = options.context;
const args = options.args || [];
return Reflect.apply(fn, ctx, args);
}
}
export default SyntaxHighlight;