export interface DecorationInline { startIndex: number; endIndex: number; className: string; title?: string; } export class Decorator { public decorationGroups: Record = {}; highlight(code: string) { const decorations: DecorationInline[] = []; for (const groupName in this.decorationGroups) { const group = this.decorationGroups[groupName]; decorations.push(...group); } const cutIndexes = new Set(); for (const d of decorations) { cutIndexes.add(d.startIndex); cutIndexes.add(d.endIndex); } cutIndexes.add(0); cutIndexes.add(code.length); const cutIndexesArr = Array.from(cutIndexes); cutIndexesArr.sort((a, b) => a - b); let html = ''; let lastIndex = 0; for (const currentIdx of cutIndexesArr) { const text = code.substring(lastIndex, currentIdx); const activeDecors = decorations.filter((d) => lastIndex >= d.startIndex && currentIdx <= d.endIndex ); for (const decor of activeDecors) { if (decor.title) { html += ``; } else { html += ``; } } html += escapeHtml(text); for (const decor of activeDecors) { html += ''; } lastIndex = currentIdx; } return html; } } function escapeHtml(text: string) { return text .replace(/&/g, '&') .replace(//g, '>'); }