forked from highlightjs/highlight.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
html_renderer.js
46 lines (34 loc) · 801 Bytes
/
html_renderer.js
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
const SPAN_CLOSE = '</span>';
import {escapeHTML} from './utils';
const emitsWrappingTags = (node) => {
return !!node.kind;
}
export default class HTMLRenderer {
constructor(tree, options) {
this.buffer = "";
this.classPrefix = options.classPrefix;
tree.walk(this);
}
// renderer API
addText(text) {
this.buffer += escapeHTML(text)
}
openNode(node) {
if (!emitsWrappingTags(node)) return;
let className = node.kind;
if (!node.sublanguage)
className = `${this.classPrefix}${className}`;
this.span(className);
}
closeNode(node) {
if (!emitsWrappingTags(node)) return;
this.buffer += SPAN_CLOSE;
}
// helpers
span(className) {
this.buffer += `<span class="${className}">`
}
value() {
return this.buffer;
}
}