hast-util-from-lezer
Version:
Render styled Lezer syntax trees to hast
22 lines (21 loc) • 730 B
JavaScript
import { highlightTree, classHighlighter } from "@lezer/highlight";
export function fromLezer(source, tree) {
const children = [];
let index = 0;
highlightTree(tree, classHighlighter, (from, to, classes) => {
if (from > index) {
children.push({ type: "text", value: source.slice(index, from) });
}
children.push({
type: "element",
tagName: "span",
properties: { className: classes },
children: [{ type: "text", value: source.slice(from, to) }],
});
index = to;
});
if (index < source.length) {
children.push({ type: "text", value: source.slice(index) });
}
return { type: "root", children };
}