xonecas.io
719
Le

lextide / commit 2463152

Harden bash and markdown support

xonecas2026-04-12 2463152994631b901aa75c910fd446554c05f9d0 parent eeae88e

9 files changed

This diff is too big to show in full. The first 1000 lines are below, and every file is listed with its counts.

TODO.md+3−0
@@ -26,4 +26,7 @@ Use it to track progress with the user.
2626 - [x] Add Markdown support to languages
2727 - [x] complete language parity for builtin languages
2828 - [x] fix nested state parsing inside child matches (`parseChildren`) so bash command substitutions like `$(dirname "${BASH_SOURCE[0]}")` preserve inner string scopes instead of flattening quotes to plain text
29+- [x] add focused bash and markdown language-support regression coverage
30+- [x] harden bash support for broader built-ins and arithmetic base-prefixed numbers
31+- [x] harden markdown support for setext headings, fenced and indented code blocks, reference links, empty inline links, and nested emphasis/strong spans
2932 - [x] Update AGENTS.md and TODO.md
examples/index.html+325−0
@@ -0,0 +1,325 @@
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>
src/core/engine.ts+96−8
@@ -10,6 +10,7 @@ const stickyExpressions = new WeakMap<RegExp, RegExp>();
1010
1111 interface ResolvedRule {
1212 children?: InternalRule[] | undefined;
13+ emptyChildren?: { position: number; scopes: string[] }[] | undefined;
1314 length: number;
1415 pop: boolean;
1516 push?: string | undefined;
@@ -184,11 +185,15 @@ function emitResolvedSegments(
184185 stack: readonly string[],
185186 resolved: ResolvedRule,
186187 ): void {
187- if (resolved.children && resolved.children.length > 0) {
188- const childSegments = parseChildren(
188+ if (
189+ (resolved.children && resolved.children.length > 0) ||
190+ (resolved.emptyChildren && resolved.emptyChildren.length > 0)
191+ ) {
192+ const childSegments = expandResolvedSegments(
189193 states,
190194 text,
191195 resolved.children,
196+ resolved.emptyChildren,
192197 scopes,
193198 position,
194199 stack,
@@ -395,11 +400,15 @@ function parseChildren(
395400 resolved.scope,
396401 );
397402
398- if (resolved.children && resolved.children.length > 0) {
399- const childSegments = parseChildren(
403+ if (
404+ (resolved.children && resolved.children.length > 0) ||
405+ (resolved.emptyChildren && resolved.emptyChildren.length > 0)
406+ ) {
407+ const childSegments = expandResolvedSegments(
400408 states,
401409 text,
402410 resolved.children,
411+ resolved.emptyChildren,
403412 scopes,
404413 offset + position,
405414 stateAfter,
@@ -433,6 +442,75 @@ function parseChildren(
433442 return segments;
434443 }
435444
445+function expandResolvedSegments(
446+ states: ReturnType<typeof getInternalStates>,
447+ text: string,
448+ rules: readonly InternalRule[] | undefined,
449+ emptyChildren: readonly { position: number; scopes: string[] }[] | undefined,
450+ baseScopes: readonly string[],
451+ offset: number,
452+ stateAfter: readonly string[],
453+): Segment[] {
454+ const segments = parseChildren(
455+ states,
456+ text,
457+ rules ?? [],
458+ baseScopes,
459+ offset,
460+ stateAfter,
461+ );
462+
463+ if (!emptyChildren || emptyChildren.length === 0) {
464+ return segments;
465+ }
466+
467+ for (const emptyChild of emptyChildren) {
468+ const position = offset + emptyChild.position;
469+
470+ splitSegmentsAtPosition(segments, position);
471+ pushSegment(
472+ segments,
473+ "",
474+ [...baseScopes, ...emptyChild.scopes],
475+ position,
476+ position,
477+ stateAfter,
478+ );
479+ }
480+
481+ segments.sort(compareSegments);
482+ return segments;
483+}
484+
485+function splitSegmentsAtPosition(segments: Segment[], position: number): void {
486+ for (let index = 0; index < segments.length; index += 1) {
487+ const segment = segments[index];
488+
489+ if (position <= segment.start || position >= segment.end) {
490+ continue;
491+ }
492+
493+ const splitIndex = position - segment.start;
494+ const left: Segment = {
495+ text: segment.text.slice(0, splitIndex),
496+ scopes: [...segment.scopes],
497+ start: segment.start,
498+ end: position,
499+ stateAfter: [...segment.stateAfter],
500+ };
501+ const right: Segment = {
502+ text: segment.text.slice(splitIndex),
503+ scopes: [...segment.scopes],
504+ start: position,
505+ end: segment.end,
506+ stateAfter: [...segment.stateAfter],
507+ };
508+
509+ segments.splice(index, 1, left, right);
510+ return;
511+ }
512+}
513+
436514 function pushSegment(
437515 segments: Segment[],
438516 text: string,
@@ -441,10 +519,6 @@ function pushSegment(
441519 end: number,
442520 stateAfter: readonly string[],
443521 ): void {
444- if (text.length === 0) {
445- return;
446- }
447-
448522 const tail = segments[segments.length - 1];
449523
450524 if (tail && tail.end === start && sameScopes(tail.scopes, scopes)) {
@@ -509,6 +583,7 @@ function buildResolvedRuleFromLength(
509583 push: rule.push,
510584 pop: rule.pop === true,
511585 children: rule.children,
586+ emptyChildren: undefined,
512587 relevance: rule.relevance ?? 0,
513588 pushScope: rule.pushScope,
514589 };
@@ -524,6 +599,7 @@ function buildResolvedRuleFromObject(
524599 push: matched.push ?? rule.push,
525600 pop: matched.pop ?? rule.pop === true,
526601 children: matched.children ?? rule.children,
602+ emptyChildren: matched.emptyChildren,
527603 relevance: matched.relevance ?? rule.relevance ?? 0,
528604 pushScope: matched.pushScope ?? rule.pushScope,
529605 };
@@ -547,6 +623,18 @@ function rootChildKey(segment: Segment): string {
547623 return segment.scopes[0] ?? "";
548624 }
549625
626+function compareSegments(left: Segment, right: Segment): number {
627+ if (left.start !== right.start) {
628+ return left.start - right.start;
629+ }
630+
631+ if (left.end !== right.end) {
632+ return left.end - right.end;
633+ }
634+
635+ return left.scopes.length - right.scopes.length;
636+}
637+
550638 function sameScopes(
551639 left: readonly string[],
552640 right: readonly string[],
src/core/grammar.ts+6−0
@@ -2,12 +2,18 @@ import type { GrammarDefinition, GrammarRule } from "../types";
22
33 export const INTERNAL_STATES = Symbol("lextide.internalStates");
44
5+export interface EmptyChild {
6+ position: number;
7+ scopes: string[];
8+}
9+
510 type MatchResultObject = {
611 length: number;
712 scope?: string | undefined;
813 push?: string | undefined;
914 pop?: boolean | undefined;
1015 children?: InternalRule[] | undefined;
16+ emptyChildren?: EmptyChild[] | undefined;
1117 relevance?: number | undefined;
1218 pushScope?: string | null | undefined;
1319 };
src/languages/bash.ts+83−1
@@ -34,10 +34,12 @@ const BASH_LITERALS = new Set(["false", "true"]);
3434 const BASH_BUILT_INS = new Set([
3535 "alias",
3636 "bind",
37+ "break",
3738 "builtin",
3839 "caller",
3940 "cd",
4041 "command",
42+ "continue",
4143 "declare",
4244 "echo",
4345 "enable",
@@ -45,6 +47,8 @@ const BASH_BUILT_INS = new Set([
4547 "exec",
4648 "exit",
4749 "export",
50+ "getopts",
51+ "hash",
4852 "help",
4953 "let",
5054 "local",
@@ -58,6 +62,7 @@ const BASH_BUILT_INS = new Set([
5862 "return",
5963 "set",
6064 "shift",
65+ "shopt",
6166 "source",
6267 "test",
6368 "times",
@@ -101,14 +106,17 @@ const BASH_ENVIRONMENT_COMMANDS = new Set([
101106 "node",
102107 "nohup",
103108 "perl",
109+ "printenv",
104110 "ps",
105111 "python",
112+ "readlink",
106113 "rg",
107114 "rm",
108115 "rsync",
109116 "scp",
110117 "sed",
111118 "sh",
119+ "sleep",
112120 "sort",
113121 "ssh",
114122 "sudo",
@@ -120,13 +128,17 @@ const BASH_ENVIRONMENT_COMMANDS = new Set([
120128 "uniq",
121129 "wc",
122130 "wget",
131+ "whoami",
123132 "xargs",
133+ "yes",
124134 ]);
125135
126136 const BASH_COMMANDS = new Set([
127137 ...BASH_BUILT_INS,
128138 ...BASH_ENVIRONMENT_COMMANDS,
129139 ]);
140+const BASH_ARITHMETIC_NUMBER =
141+ /\d+#[0-9A-Za-z]+|(?:0[xX][0-9a-fA-F]+|\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)/;
130142 const BASH_IDENTIFIER_RULE = {
131143 match: createIdentifierMatcher({
132144 keywordRelevance: 2,
@@ -315,6 +327,26 @@ function bashFunctionMatcher(input: string, position: number): number | null {
315327 return nameEnd - position;
316328 }
317329
330+function bashOptionMatcher(input: string, position: number): number | null {
331+ if (input[position] !== "-" || !isOptionStart(input, position)) {
332+ return null;
333+ }
334+
335+ if (input[position + 1] === "-") {
336+ return readLongOption(input, position);
337+ }
338+
339+ return readShortOption(input, position);
340+}
341+
342+function bashOperatorMatcher(input: string, position: number): number | null {
343+ if (input.startsWith("&&", position) || input.startsWith("||", position)) {
344+ return 2;
345+ }
346+
347+ return input[position] === "|" ? 1 : null;
348+}
349+
318350 function bashHereDocMatcher(
319351 input: string,
320352 position: number,
@@ -393,7 +425,7 @@ function createArithmeticChildren(): InternalRule[] {
393425 return [
394426 { match: bashCommandSubstitutionMatcher, relevance: 2 },
395427 { match: bashVariableMatcher, scope: "variable", relevance: 2 },
396- createNumberRule(),
428+ { match: BASH_ARITHMETIC_NUMBER, scope: "number", relevance: 1 },
397429 BASH_IDENTIFIER_RULE,
398430 ];
399431 }
@@ -424,6 +456,8 @@ function createBashInlineRules(includeComments: boolean): InternalRule[] {
424456 { match: bashArithmeticMatcher, relevance: 2 },
425457 { match: bashCommandSubstitutionMatcher, relevance: 2 },
426458 { match: bashVariableMatcher, scope: "variable", relevance: 2 },
459+ { match: bashOptionMatcher, scope: "attr", relevance: 1 },
460+ { match: bashOperatorMatcher, scope: "operator", relevance: 0 },
427461 { match: bashArrayAssignmentMatcher, scope: "variable", relevance: 1 },
428462 { match: bashFunctionMatcher, scope: "title.function", relevance: 2 },
429463 { match: bashCommandMatcher, scope: "built_in", relevance: 1 },
@@ -661,6 +695,18 @@ function isCommandStart(input: string, position: number): boolean {
661695 ].includes(word);
662696 }
663697
698+function isOptionBoundaryCharacter(character: string | undefined): boolean {
699+ return character === undefined || /[\s[\]{}()<>;&|]/.test(character);
700+}
701+
702+function isOptionStart(input: string, position: number): boolean {
703+ return isOptionBoundaryCharacter(input[position - 1]);
704+}
705+
706+function isOptionValueCharacter(character: string | undefined): boolean {
707+ return character !== undefined && !/[\s[\]{}()<>;&|]/.test(character);
708+}
709+
664710 function looksLikeAssignment(input: string, position: number): boolean {
665711 let cursor = position;
666712
@@ -697,6 +743,42 @@ function looksLikeAssignment(input: string, position: number): boolean {
697743 );
698744 }
699745
746+function readLongOption(input: string, position: number): number | null {
747+ let cursor = position + 2;
748+
749+ if (!/[A-Za-z]/.test(input[cursor] ?? "")) {
750+ return null;
751+ }
752+
753+ while (/[A-Za-z0-9-]/.test(input[cursor] ?? "")) {
754+ cursor += 1;
755+ }
756+
757+ if (input[cursor] === "=") {
758+ cursor += 1;
759+
760+ while (isOptionValueCharacter(input[cursor])) {
761+ cursor += 1;
762+ }
763+ }
764+
765+ return cursor - position;
766+}
767+
768+function readShortOption(input: string, position: number): number | null {
769+ let cursor = position + 1;
770+
771+ if (!/[A-Za-z]/.test(input[cursor] ?? "")) {
772+ return null;
773+ }
774+
775+ while (/[A-Za-z]/.test(input[cursor] ?? "")) {
776+ cursor += 1;
777+ }
778+
779+ return cursor - position;
780+}
781+
700782 function readHereDocDelimiter(
701783 input: string,
702784 position: number,
src/languages/markdown.ts+326−18
@@ -1,4 +1,4 @@
1-import type { InternalRule } from "../core/grammar";
1+import type { EmptyChild, InternalRule } from "../core/grammar";
22 import { defineGrammar } from "../core/grammar";
33 import type { LanguageFn } from "../types";
44
@@ -7,8 +7,24 @@ const MARKDOWN_ENTITY = /&(?:[A-Za-z0-9]+|#[0-9]{1,7}|#[Xx][0-9a-fA-F]{1,6});/;
77 const MARKDOWN_INLINE_RULES: InternalRule[] = [
88 { match: markdownInlineCodeMatcher, scope: "code", relevance: 1 },
99 { match: markdownLinkMatcher, relevance: 2 },
10- { match: markdownStrongMatcher, scope: "strong", relevance: 1 },
11- { match: markdownEmphasisMatcher, scope: "emphasis", relevance: 0 },
10+ { match: markdownTripleStrongMatcher, relevance: 1 },
11+ { match: markdownStrongMatcher, relevance: 1 },
12+ { match: markdownEmphasisMatcher, relevance: 0 },
13+ { match: MARKDOWN_ENTITY, scope: "literal", relevance: 0 },
14+];
15+
16+const MARKDOWN_EMPHASIS_CHILD_RULES: InternalRule[] = [
17+ { match: markdownInlineCodeMatcher, scope: "code", relevance: 0 },
18+ { match: markdownLinkMatcher, relevance: 0 },
19+ { match: markdownTripleStrongMatcher, relevance: 0 },
20+ { match: markdownStrongMatcher, relevance: 0 },
21+ { match: MARKDOWN_ENTITY, scope: "literal", relevance: 0 },
22+];
23+
24+const MARKDOWN_STRONG_CHILD_RULES: InternalRule[] = [
25+ { match: markdownInlineCodeMatcher, scope: "code", relevance: 0 },
26+ { match: markdownLinkMatcher, relevance: 0 },
27+ { match: markdownEmphasisMatcher, relevance: 0 },
1228 { match: MARKDOWN_ENTITY, scope: "literal", relevance: 0 },
1329 ];
1430
@@ -27,9 +43,12 @@ export const markdown: LanguageFn = () =>
2743 rules: [
2844 { match: markdownBacktickFenceStartMatcher, relevance: 3 },
2945 { match: markdownTildeFenceStartMatcher, relevance: 3 },
46+ { match: markdownIndentedCodeMatcher, scope: "code", relevance: 1 },
3047 { match: markdownHeadingMatcher, relevance: 2 },
48+ { match: markdownSetextHeadingMatcher, relevance: 2 },
3149 { match: markdownBlockquoteMatcher, relevance: 1 },
3250 { match: markdownBulletMatcher, scope: "bullet", relevance: 1 },
51+ { match: markdownLinkDefinitionMatcher, relevance: 1 },
3352 ...MARKDOWN_INLINE_RULES,
3453 ],
3554 },
@@ -40,6 +59,28 @@ export const markdown: LanguageFn = () =>
4059 },
4160 });
4261
62+function createEmptyChild(position: number, scope: string): EmptyChild {
63+ return { position, scopes: [scope] };
64+}
65+
66+function createNestedRangeRule(
67+ start: number,
68+ length: number,
69+ children: InternalRule[],
70+ scope?: string,
71+): InternalRule | null {
72+ if (length <= 0) {
73+ return null;
74+ }
75+
76+ return {
77+ children,
78+ match: (_input: string, position: number) =>
79+ position === start ? length : null,
80+ scope,
81+ };
82+}
83+
4384 function createScopedRanges(
4485 ranges: readonly { length: number; scope: string; start: number }[],
4586 ): InternalRule[] {
@@ -52,6 +93,31 @@ function createScopedRanges(
5293 }));
5394 }
5495
96+function findLastStyledSpanEnd(
97+ input: string,
98+ position: number,
99+ delimiter: string,
100+): number | null {
101+ const lineEnd = findLineEnd(input, position);
102+ const contentStart = position + delimiter.length;
103+
104+ if (contentStart >= lineEnd || /\s/.test(input[contentStart] ?? "")) {
105+ return null;
106+ }
107+
108+ let cursor = input.lastIndexOf(delimiter, lineEnd - delimiter.length);
109+
110+ while (cursor >= contentStart) {
111+ if (!/\s/.test(input[cursor - 1] ?? "")) {
112+ return cursor + delimiter.length;
113+ }
114+
115+ cursor = input.lastIndexOf(delimiter, cursor - 1);
116+ }
117+
118+ return null;
119+}
120+
55121 function findStyledSpanEnd(
56122 input: string,
57123 position: number,
@@ -89,6 +155,10 @@ function findLineEndWithNewline(input: string, position: number): number {
89155 return lineEnd < input.length ? lineEnd + 1 : lineEnd;
90156 }
91157
158+function hasIndentedCodePrefix(input: string, position: number): boolean {
159+ return input[position] === "\t" || input.startsWith(" ", position);
160+}
161+
92162 function isFenceLine(
93163 input: string,
94164 position: number,
@@ -119,6 +189,10 @@ function isFenceLine(
119189 return true;
120190 }
121191
192+function isLineStart(input: string, position: number): boolean {
193+ return position === 0 || input[position - 1] === "\n";
194+}
195+
122196 function lineOffset(input: string, position: number): number | null {
123197 let cursor = position;
124198 let offset = 0;
@@ -140,7 +214,7 @@ function markdownBacktickFenceEndMatcher(
140214 position: number,
141215 ): number | null {
142216 return isFenceLine(input, position, "`")
143- ? findLineEndWithNewline(input, position) - position
217+ ? findLineEnd(input, position) - position
144218 : null;
145219 }
146220
@@ -197,15 +271,45 @@ function markdownBulletMatcher(input: string, position: number): number | null {
197271 function markdownEmphasisMatcher(
198272 input: string,
199273 position: number,
200-): number | null {
274+): { children: InternalRule[]; length: number; scope: string } | null {
201275 if (input[position] === "*" && input[position + 1] !== "*") {
202276 const end = findStyledSpanEnd(input, position, "*");
203- return end === null ? null : end - position;
277+
278+ if (end === null) {
279+ return null;
280+ }
281+
282+ const innerRule = createNestedRangeRule(
283+ 1,
284+ end - position - 2,
285+ MARKDOWN_EMPHASIS_CHILD_RULES,
286+ );
287+
288+ return {
289+ children: innerRule ? [innerRule] : [],
290+ length: end - position,
291+ scope: "emphasis",
292+ };
204293 }
205294
206295 if (input[position] === "_" && input[position + 1] !== "_") {
207296 const end = findStyledSpanEnd(input, position, "_");
208- return end === null ? null : end - position;
297+
298+ if (end === null) {
299+ return null;
300+ }
301+
302+ const innerRule = createNestedRangeRule(
303+ 1,
304+ end - position - 2,
305+ MARKDOWN_EMPHASIS_CHILD_RULES,
306+ );
307+
308+ return {
309+ children: innerRule ? [innerRule] : [],
310+ length: end - position,
311+ scope: "emphasis",
312+ };
209313 }
210314
211315 return null;
@@ -242,6 +346,26 @@ function markdownHeadingMatcher(
242346 };
243347 }
244348
349+function markdownIndentedCodeMatcher(
350+ input: string,
351+ position: number,
352+): number | null {
353+ if (
354+ !isLineStart(input, position) ||
355+ !hasIndentedCodePrefix(input, position)
356+ ) {
357+ return null;
358+ }
359+
360+ let cursor = position;
361+
362+ while (cursor < input.length && hasIndentedCodePrefix(input, cursor)) {
363+ cursor = findLineEndWithNewline(input, cursor);
364+ }
365+
366+ return cursor - position;
367+}
368+
245369 function markdownInlineCodeMatcher(
246370 input: string,
247371 position: number,
@@ -260,10 +384,52 @@ function markdownInlineCodeMatcher(
260384 return end - position + 1;
261385 }
262386
263-function markdownLinkMatcher(
387+function markdownLinkDefinitionMatcher(
264388 input: string,
265389 position: number,
266390 ): { children: InternalRule[]; length: number } | null {
391+ if (!isLineStart(input, position) || input[position] !== "[") {
392+ return null;
393+ }
394+
395+ const lineEnd = findLineEnd(input, position);
396+ const labelEnd = input.indexOf("]:", position + 1);
397+
398+ if (labelEnd === -1 || labelEnd >= lineEnd) {
399+ return null;
400+ }
401+
402+ let urlStart = labelEnd + 2;
403+
404+ while (urlStart < lineEnd && /[ \t]/.test(input[urlStart] ?? "")) {
405+ urlStart += 1;
406+ }
407+
408+ if (urlStart >= lineEnd) {
409+ return null;
410+ }
411+
412+ return {
413+ children: createScopedRanges([
414+ { length: labelEnd - position - 1, scope: "symbol", start: 1 },
415+ {
416+ length: lineEnd - urlStart,
417+ scope: "link",
418+ start: urlStart - position,
419+ },
420+ ]),
421+ length: lineEnd - position,
422+ };
423+}
424+
425+function markdownLinkMatcher(
426+ input: string,
427+ position: number,
428+): {
429+ emptyChildren?: EmptyChild[];
430+ children: InternalRule[];
431+ length: number;
432+} | null {
267433 if (input[position] !== "[") {
268434 return null;
269435 }
@@ -276,21 +442,28 @@ function markdownLinkMatcher(
276442 }
277443
278444 if (input[labelEnd + 1] === "(") {
279- const urlEnd = input.indexOf(")", labelEnd + 2);
445+ const urlStart = labelEnd + 2;
446+ const urlEnd = input.indexOf(")", urlStart);
280447
281448 if (urlEnd === -1 || urlEnd >= lineEnd) {
282449 return null;
283450 }
284451
452+ const urlLength = urlEnd - urlStart;
453+
285454 return {
286455 children: createScopedRanges([
287456 { length: labelEnd - position - 1, scope: "string", start: 1 },
288457 {
289- length: urlEnd - labelEnd - 2,
458+ length: urlLength,
290459 scope: "link",
291- start: labelEnd - position + 2,
460+ start: urlStart - position,
292461 },
293462 ]),
463+ emptyChildren:
464+ urlLength === 0
465+ ? [createEmptyChild(urlStart - position, "link")]
466+ : undefined,
294467 length: urlEnd - position + 1,
295468 };
296469 }
@@ -299,7 +472,8 @@ function markdownLinkMatcher(
299472 return null;
300473 }
301474
302- const symbolEnd = input.indexOf("]", labelEnd + 2);
475+ const symbolStart = labelEnd + 2;
476+ const symbolEnd = input.indexOf("]", symbolStart);
303477
Cut short: this commit's diff is over 1000 lines.
test/bash-language-support.test.ts+152−0
Cut short: this commit's diff is over 1000 lines.
test/language-test-helpers.ts+77−0
Cut short: this commit's diff is over 1000 lines.
test/markdown-language-support.test.ts+184−0
Cut short: this commit's diff is over 1000 lines.