xonecas.io
719
Le

lextide / files

lextide / examples / index.html

8.6 KB
1<!doctype html>
2<html lang="en">
3	<head>
4		<meta charset="utf-8" />
5		<meta name="viewport" content="width=device-width, initial-scale=1" />
6		<title>lextide examples</title>
7		<script src="https://cdn.tailwindcss.com"></script>
8		<style>
9			:root {
10				color-scheme: dark;
11			}
12
13			.hljs-comment,
14			.hljs-quote {
15				color: #94a3b8;
16				font-style: italic;
17			}
18
19			.hljs-keyword,
20			.hljs-meta,
21			.hljs-bullet {
22				color: #c084fc;
23			}
24
25			.hljs-title,
26			.function_,
27			.hljs-section,
28			.hljs-built_in,
29			.hljs-tag,
30			.hljs-name {
31				color: #7dd3fc;
32			}
33
34			.hljs-string,
35			.hljs-code,
36			.hljs-link,
37			.hljs-symbol,
38			.hljs-attr,
39			.hljs-property {
40				color: #86efac;
41			}
42
43			.hljs-number,
44			.hljs-literal,
45			.hljs-variable,
46			.inherited__ {
47				color: #fdba74;
48			}
49
50			.hljs-operator,
51			.hljs-regexp,
52			.hljs-subst,
53			.hljs-escape {
54				color: #f9a8d4;
55			}
56
57			.hljs-emphasis {
58				font-style: italic;
59			}
60
61			.hljs-strong {
62				font-weight: 700;
63			}
64		</style>
65	</head>
66	<body class="min-h-screen bg-slate-950 text-slate-100">
67		<div class="mx-auto px-6 py-10">
68			<header class="mb-8 space-y-3">
69				<p class="text-sm font-medium uppercase tracking-[0.2em] text-cyan-300">
70					lextide example
71				</p>
72				<h1 class="text-3xl font-semibold tracking-tight">Language preview dashboard</h1>
73				<p class="max-w-3xl text-sm text-slate-400">
74					Minimal manual verification page for the built-in languages. Run
75					<code class="rounded bg-slate-900 px-1.5 py-0.5 text-slate-200">bun run build</code>
76					and serve the repo root, then open
77					<code class="rounded bg-slate-900 px-1.5 py-0.5 text-slate-200">/examples/index.html</code>.
78				</p>
79			</header>
80
81			<div
82				id="summary"
83				class="mb-6 rounded-2xl border border-slate-800 bg-slate-900/70 px-4 py-3 text-sm text-slate-300"
84			></div>
85
86			<div id="preview" class="grid gap-5 md:grid-cols-2 xl:grid-cols-3"></div>
87		</div>
88
89		<script type="module">
90			import { common, createLowlight } from "../dist/index.js";
91
92			const lowlight = createLowlight(common);
93			const jsTemplateName = "${" + "name}";
94			const jsTemplateTotal = "${" + "total}";
95			const samples = {
96				bash: [
97					'#!/usr/bin/env bash',
98					'if [ -n "$USER" ]; then',
99					'  mkdir -p ./tmp/demo && ls --color=never ./tmp/demo | wc -l',
100					'  curl --fail https://example.com | jq -r ".title" && echo "hello $(whoami)"',
101					"fi",
102					"",
103				].join("\n"),
104				javascript: [
105					"const users = [{ name: \"Ada\", score: 42 }];",
106					"function greet(name) {",
107					"  const total = 1 + 2;",
108					`  return \`hi ${jsTemplateName} (${jsTemplateTotal})\`;`,
109					"}",
110					"const match = /ada/i.test(users[0]?.name ?? \"unknown\");",
111					"console.log(match && greet(users[0].name));",
112					"",
113				].join("\n"),
114				typescript: [
115					"type Result<T> = T | null;",
116					"interface Person {",
117					"  name: string;",
118					"  tags: Array<string>;",
119					"}",
120					'const user: Person = { name: "Ada", tags: ["dev"] };',
121					"function pick<T extends keyof Person>(value: Person, key: T): Result<Person[T]> {",
122					"  return value[key] ?? null;",
123					"}",
124					"",
125				].join("\n"),
126				python: [
127					"@cache",
128					"def greet(name: str) -> str:",
129					'    data = [item.upper() for item in ("ada", name)]',
130					'    return f"hi {data[0]}"',
131					"",
132				].join("\n"),
133				markdown: [
134					"# Notes",
135					"",
136					"Quick start",
137					"-----------",
138					"",
139					"- **bold** and _italic_",
140					"- [docs](https://example.com)",
141					"- [guide][ref] and []()",
142					"> quote with `code` &amp; entity",
143					"",
144					"```ts",
145					"const value = 1",
146					"```",
147					"",
148					"    indented code",
149					"",
150					"[ref]: https://example.com/docs",
151					"",
152				].join("\n"),
153			};
154
155			const languages = lowlight.listLanguages();
156			const summary = document.querySelector("#summary");
157			const preview = document.querySelector("#preview");
158
159			summary.textContent = `Built-ins: ${languages.join(", ")}`;
160
161			for (const language of languages) {
162				const source = samples[language] ?? "";
163				const tree = lowlight.highlight(language, source);
164				preview.append(createCard(language, source, tree));
165			}
166
167			function createCard(language, source, tree) {
168				const card = document.createElement("section");
169				card.className =
170					"overflow-hidden rounded-2xl border border-slate-800 bg-slate-900/70 shadow-lg shadow-black/20";
171
172				const header = document.createElement("div");
173				header.className =
174					"flex items-center justify-between gap-3 border-b border-slate-800 px-4 py-3";
175
176				const title = document.createElement("div");
177
178				const titleLabel = document.createElement("div");
179				titleLabel.className = "font-medium text-slate-100";
180				titleLabel.textContent = language;
181
182				const titleMeta = document.createElement("div");
183				titleMeta.className = "text-xs text-slate-500";
184				titleMeta.textContent = `relevance ${tree.data?.relevance ?? 0}`;
185
186				title.append(titleLabel, titleMeta);
187
188				const actions = document.createElement("div");
189				actions.className = "flex items-center gap-2";
190
191				const streamButton = document.createElement("button");
192				streamButton.className =
193					"rounded-lg border border-slate-700 bg-slate-800 px-3 py-1.5 text-xs font-medium text-slate-200 transition hover:border-cyan-400/40 hover:text-cyan-200 disabled:cursor-not-allowed disabled:opacity-60";
194				streamButton.textContent = "Replay stream";
195
196				const badge = document.createElement("span");
197				badge.className =
198					"rounded-full border border-cyan-400/30 bg-cyan-400/10 px-2 py-1 text-xs font-medium text-cyan-200";
199				badge.textContent = tree.data?.language ?? language;
200
201				actions.append(streamButton, badge);
202				header.append(title, actions);
203
204				const body = document.createElement("div");
205				body.className = "p-4";
206
207				const pre = document.createElement("pre");
208				pre.className =
209					"overflow-x-auto rounded-xl bg-slate-950 p-4 text-sm leading-6 text-slate-200";
210
211				const code = document.createElement("code");
212				code.className = "block min-h-40 font-mono";
213				renderNodes(tree.children, code);
214				pre.append(code);
215
216				const footer = document.createElement("div");
217				footer.className = "mt-3 text-xs text-slate-500";
218				footer.textContent = `${source.length} chars · scopes: ${collectClasses(tree.children).join(", ") || "plain text"}`;
219
220				let streamRun = 0;
221				streamButton.addEventListener("click", async () => {
222					streamRun += 1;
223					const currentRun = streamRun;
224					streamButton.disabled = true;
225					code.replaceChildren();
226
227					const stream = lowlight.stream(language, { allowRecalls: true });
228					const rendered = [];
229					const chunks = createReplayChunks(source);
230
231					for (const chunk of chunks) {
232						if (currentRun !== streamRun) {
233							return;
234						}
235
236						applyUpdate(rendered, stream.write(chunk));
237						renderNodes(rendered, code);
238						await delay(90);
239					}
240
241					if (currentRun !== streamRun) {
242						return;
243					}
244
245					applyUpdate(rendered, stream.end());
246					renderNodes(rendered, code);
247
248					if (currentRun === streamRun) {
249						streamButton.disabled = false;
250					}
251				});
252
253				body.append(pre, footer);
254				card.append(header, body);
255				return card;
256			}
257
258			function applyUpdate(rendered, update) {
259				if (update.recall > 0) {
260					rendered.splice(rendered.length - update.recall, update.recall);
261				}
262
263				rendered.push(...update.stable, ...update.unstable);
264			}
265
266			function collectClasses(children, classes = new Set()) {
267				for (const child of children) {
268					if (child.type === "element") {
269						for (const className of child.properties.className) {
270							classes.add(className);
271						}
272
273						collectClasses(child.children, classes);
274					}
275				}
276
277				return [...classes].sort();
278			}
279
280			function createReplayChunks(source) {
281				const pattern = [1, 2, 3, 5, 8, 13];
282				const chunks = [];
283				let cursor = 0;
284				let step = 0;
285
286				while (cursor < source.length) {
287					const size = pattern[step % pattern.length];
288					chunks.push(source.slice(cursor, cursor + size));
289					cursor += size;
290					step += 1;
291				}
292
293				return chunks;
294			}
295
296			function delay(timeout) {
297				return new Promise((resolve) => {
298					window.setTimeout(resolve, timeout);
299				});
300			}
301
302			function renderNodes(children, parent) {
303				parent.replaceChildren();
304				renderChildren(children, parent);
305			}
306
307			function renderChildren(children, parent) {
308				for (const child of children) {
309					parent.append(renderNode(child));
310				}
311			}
312
313			function renderNode(node) {
314				if (node.type === "text") {
315					return document.createTextNode(node.value);
316				}
317
318				const element = document.createElement("span");
319				element.className = node.properties.className.join(" ");
320				renderChildren(node.children, element);
321				return element;
322			}
323		</script>
324	</body>
325</html>