UNPKG

@tanstack/highlight

Version:

Tiny class-based syntax highlighting for documentation.

108 lines (107 loc) • 4.51 kB
export function renderTokenOutput(tokens, options, renderer) { const rangeDecorations = []; const lineDecorations = []; for (const decoration of options.decorations || []) { if (!decoration.range) { lineDecorations.push(decoration); continue; } const [start, end] = decoration.range; if (hasIntegerOffsets(start, end) && start < end) rangeDecorations.push(decoration); } const cursor = { index: 0, offset: 0, boundaries: [0, 0] }; if (!options.lineNumbers && !lineDecorations.length) { return renderTokenSlice(tokens, 0, Infinity, rangeDecorations, cursor, renderer); } const code = tokens.map((token) => token.value).join(''); const nodes = []; let start = 0; for (let line = 1;; line++) { const newline = code.indexOf('\n', start); const end = newline < 0 ? code.length : newline; const active = lineDecorations.filter((decoration) => includesLine(decoration.lines, line)); nodes.push(renderer.element([ 'th-line', ...active.flatMap((decoration) => splitClassNames(decoration.className)), ], lineData(active, line), renderTokenSlice(tokens, start, end, rangeDecorations, cursor, renderer))); if (newline < 0) break; nodes.push(renderer.text('\n')); start = end + 1; if (start === code.length) break; } return nodes; } function renderTokenSlice(tokens, start, end, decorations, cursor, renderer) { const nodes = []; for (; cursor.index < tokens.length; cursor.index++) { const token = tokens[cursor.index]; const tokenStart = cursor.offset; const tokenEnd = tokenStart + token.value.length; const sliceStart = Math.max(start, tokenStart); const sliceEnd = Math.min(end, tokenEnd); if (sliceStart < sliceEnd) { let sorted = cursor.boundaries; sorted[0] = sliceStart; sorted[1] = sliceEnd; let boundaries; for (const decoration of decorations) { const [decorationStart, decorationEnd] = decoration.range; if (decorationEnd <= sliceStart || decorationStart >= sliceEnd) continue; boundaries ||= new Set(sorted); boundaries.add(Math.max(sliceStart, decorationStart)).add(Math.min(sliceEnd, decorationEnd)); } if (boundaries) sorted = [...boundaries].sort((a, b) => a - b); for (let index = 0; index < sorted.length - 1; index++) { const segmentStart = sorted[index]; const segmentEnd = sorted[index + 1]; let node = renderer.text(token.value.slice(segmentStart - tokenStart, segmentEnd - tokenStart)); if (token.className) { node = renderer.token(token.className, node); } for (const decoration of decorations) { if (decoration.range[0] > segmentStart || decoration.range[1] < segmentEnd) continue; node = renderer.element(['th-decoration', ...splitClassNames(decoration.className)], stringifyData(decoration.data), [node]); } nodes.push(node); } } // Keep a token that crosses the line boundary for the next slice. if (tokenEnd > end) break; cursor.offset = tokenEnd; } return nodes; } function includesLine(lines, line) { return typeof lines === 'number' ? lines === line : line >= lines[0] && line <= lines[1]; } function lineData(decorations, line) { const data = Object.fromEntries(decorations.flatMap((decoration) => dataEntries(decoration.data))); data.line = String(line); return data; } function stringifyData(data) { if (data) return Object.fromEntries(dataEntries(data)); } function dataEntries(data) { return Object.entries(data || {}).map(([key, value]) => [normalizeDataKey(key), String(value)]); } export function normalizeDataKey(value) { return value.replace(/[A-Z]/g, (character) => `-${character.toLowerCase()}`).replace(/[^a-z0-9_.:-]/gi, '-'); } function splitClassNames(className) { return className?.match(/\S+/g) || []; } export function hasIntegerOffsets(start, end) { return Number.isInteger(start) && Number.isInteger(end); }