1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-08 16:36:15 +00:00
Jan Korf d533557324
Websocket refactoring (#190)
Websocket refactoring
2024-02-24 19:21:47 +01:00

47 lines
813 B
JavaScript

import { escapeHTML } from './utils';
const SPAN_CLOSE = '</span>';
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;
}
}