xonecas.io
719
Le

lextide / files

lextide / src / hast.ts

2.8 KB
1import type { Segment } from "./core/engine";
2import type { Element, ElementContent, Root, Text } from "./types";
3
4const DEFAULT_PREFIX = "hljs-";
5
6type Container = Root | Element;
7
8export function buildChildren(
9	segments: readonly Segment[],
10	prefix: string | null | undefined,
11): ElementContent[] {
12	const root: Root = { type: "root", children: [] };
13	const scopes: string[] = [];
14	const stack: Container[] = [root];
15
16	for (const segment of segments) {
17		const shared = sharedScopeCount(scopes, segment.scopes);
18
19		while (scopes.length > shared) {
20			scopes.pop();
21			stack.pop();
22		}
23
24		while (scopes.length < segment.scopes.length) {
25			const scope = segment.scopes[scopes.length];
26			const element: Element = {
27				type: "element",
28				tagName: "span",
29				properties: { className: serializeScope(scope, prefix) },
30				children: [],
31			};
32
33			stack[stack.length - 1].children.push(element);
34			scopes.push(scope);
35			stack.push(element);
36		}
37
38		appendText(stack[stack.length - 1], segment.text);
39	}
40
41	return root.children;
42}
43
44export function createRoot(
45	children: ElementContent[],
46	language: string | undefined,
47	relevance: number,
48): Root {
49	return {
50		type: "root",
51		children,
52		data: { language, relevance },
53	};
54}
55
56export function cloneChildren(
57	children: readonly ElementContent[],
58): ElementContent[] {
59	return children.map((child) => cloneNode(child));
60}
61
62export function cloneRoot(root: Root): Root {
63	return {
64		type: "root",
65		children: cloneChildren(root.children),
66		data: root.data
67			? {
68					language: root.data.language,
69					relevance: root.data.relevance,
70				}
71			: undefined,
72	};
73}
74
75function cloneNode(node: ElementContent): ElementContent {
76	if (node.type === "text") {
77		const text: Text = {
78			type: "text",
79			value: node.value,
80		};
81
82		return text;
83	}
84
85	return {
86		type: "element",
87		tagName: "span",
88		properties: {
89			className: [...node.properties.className],
90		},
91		children: cloneChildren(node.children),
92	};
93}
94
95function appendText(container: Container, value: string): void {
96	if (value.length === 0) {
97		return;
98	}
99
100	const tail = container.children[container.children.length - 1];
101
102	if (tail?.type === "text") {
103		tail.value += value;
104		return;
105	}
106
107	container.children.push({ type: "text", value });
108}
109
110function serializeScope(
111	scope: string,
112	prefix: string | null | undefined,
113): string[] {
114	const resolvedPrefix =
115		typeof prefix === "string" ? prefix : prefix === null ? "" : DEFAULT_PREFIX;
116
117	return scope
118		.split(".")
119		.map((part, index) =>
120			index === 0 ? `${resolvedPrefix}${part}` : `${part}${"_".repeat(index)}`,
121		);
122}
123
124function sharedScopeCount(
125	left: readonly string[],
126	right: readonly string[],
127): number {
128	const limit = Math.min(left.length, right.length);
129	let index = 0;
130
131	while (index < limit && left[index] === right[index]) {
132		index += 1;
133	}
134
135	return index;
136}