@@ -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.