719
Le
lextide / files
lextide / src / languages / markdown.ts
14.9 KB
1import type { EmptyChild, InternalRule } from "../core/grammar"; 2import { defineGrammar } from "../core/grammar"; 3import type { LanguageFn } from "../types"; 4 5const MARKDOWN_ENTITY = /&(?:[A-Za-z0-9]+|#[0-9]{1,7}|#[Xx][0-9a-fA-F]{1,6});/; 6 7const MARKDOWN_INLINE_RULES: InternalRule[] = [ 8 { match: markdownInlineCodeMatcher, scope: "code", relevance: 1 }, 9 { match: markdownLinkMatcher, relevance: 2 }, 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 16const 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 24const MARKDOWN_STRONG_CHILD_RULES: InternalRule[] = [ 25 { match: markdownInlineCodeMatcher, scope: "code", relevance: 0 }, 26 { match: markdownLinkMatcher, relevance: 0 }, 27 { match: markdownEmphasisMatcher, relevance: 0 }, 28 { match: MARKDOWN_ENTITY, scope: "literal", relevance: 0 }, 29]; 30 31export const markdown: LanguageFn = () => 32 defineGrammar({ 33 aliases: ["md", "mkdown", "mkd"], 34 name: "markdown", 35 relevance: 0, 36 startState: "root", 37 states: { 38 backtickFence: { 39 defaultScope: "code", 40 rules: [{ match: markdownBacktickFenceEndMatcher, pop: true }], 41 }, 42 root: { 43 rules: [ 44 { match: markdownBacktickFenceStartMatcher, relevance: 3 }, 45 { match: markdownTildeFenceStartMatcher, relevance: 3 }, 46 { match: markdownIndentedCodeMatcher, scope: "code", relevance: 1 }, 47 { match: markdownHeadingMatcher, relevance: 2 }, 48 { match: markdownSetextHeadingMatcher, relevance: 2 }, 49 { match: markdownBlockquoteMatcher, relevance: 1 }, 50 { match: markdownBulletMatcher, scope: "bullet", relevance: 1 }, 51 { match: markdownLinkDefinitionMatcher, relevance: 1 }, 52 ...MARKDOWN_INLINE_RULES, 53 ], 54 }, 55 tildeFence: { 56 defaultScope: "code", 57 rules: [{ match: markdownTildeFenceEndMatcher, pop: true }], 58 }, 59 }, 60 }); 61 62function createEmptyChild(position: number, scope: string): EmptyChild { 63 return { position, scopes: [scope] }; 64} 65 66function 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 84function createScopedRanges( 85 ranges: readonly { length: number; scope: string; start: number }[], 86): InternalRule[] { 87 return ranges 88 .filter((range) => range.length > 0) 89 .map((range) => ({ 90 match: (_input: string, position: number) => 91 position === range.start ? range.length : null, 92 scope: range.scope, 93 })); 94} 95 96function 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 121function findStyledSpanEnd( 122 input: string, 123 position: number, 124 delimiter: string, 125): number | null { 126 const lineEnd = findLineEnd(input, position); 127 const contentStart = position + delimiter.length; 128 129 if (contentStart >= lineEnd || /\s/.test(input[contentStart] ?? "")) { 130 return null; 131 } 132 133 let cursor = input.indexOf(delimiter, contentStart); 134 135 while (cursor !== -1 && cursor < lineEnd) { 136 if (!/\s/.test(input[cursor - 1] ?? "")) { 137 return cursor + delimiter.length; 138 } 139 140 cursor = input.indexOf(delimiter, cursor + delimiter.length); 141 } 142 143 return null; 144} 145 146function findLineEnd(input: string, position: number): number { 147 const lineEnd = input.indexOf("\n", position); 148 149 return lineEnd === -1 ? input.length : lineEnd; 150} 151 152function findLineEndWithNewline(input: string, position: number): number { 153 const lineEnd = findLineEnd(input, position); 154 155 return lineEnd < input.length ? lineEnd + 1 : lineEnd; 156} 157 158function hasIndentedCodePrefix(input: string, position: number): boolean { 159 return input[position] === "\t" || input.startsWith(" ", position); 160} 161 162function isFenceLine( 163 input: string, 164 position: number, 165 marker: "`" | "~", 166): boolean { 167 if (lineOffset(input, position) === null || input[position] !== marker) { 168 return false; 169 } 170 171 let cursor = position; 172 173 while (input[cursor] === marker) { 174 cursor += 1; 175 } 176 177 if (cursor - position < 3) { 178 return false; 179 } 180 181 const lineEnd = findLineEnd(input, position); 182 183 for (; cursor < lineEnd; cursor += 1) { 184 if (input[cursor] !== " " && input[cursor] !== "\t") { 185 return false; 186 } 187 } 188 189 return true; 190} 191 192function isLineStart(input: string, position: number): boolean { 193 return position === 0 || input[position - 1] === "\n"; 194} 195 196function lineOffset(input: string, position: number): number | null { 197 let cursor = position; 198 let offset = 0; 199 200 while (cursor > 0 && input[cursor - 1] !== "\n") { 201 cursor -= 1; 202 offset += 1; 203 204 if (input[cursor] !== " " || offset > 3) { 205 return null; 206 } 207 } 208 209 return offset; 210} 211 212function markdownBacktickFenceEndMatcher( 213 input: string, 214 position: number, 215): number | null { 216 return isFenceLine(input, position, "`") 217 ? findLineEnd(input, position) - position 218 : null; 219} 220 221function markdownBacktickFenceStartMatcher( 222 input: string, 223 position: number, 224): { length: number; push: string; pushScope: string } | null { 225 return createFenceStartMatch(input, position, "`", "backtickFence"); 226} 227 228function markdownBlockquoteMatcher( 229 input: string, 230 position: number, 231): { children: InternalRule[]; length: number; scope: string } | null { 232 const offset = lineOffset(input, position); 233 234 if (offset === null || input[position] !== ">") { 235 return null; 236 } 237 238 const lineEnd = findLineEnd(input, position); 239 240 return { 241 children: MARKDOWN_INLINE_RULES, 242 length: lineEnd - position, 243 scope: "quote", 244 }; 245} 246 247function markdownBulletMatcher(input: string, position: number): number | null { 248 if (lineOffset(input, position) === null) { 249 return null; 250 } 251 252 const single = input[position]; 253 254 if (single === "*" || single === "+" || single === "-") { 255 return /\s/.test(input[position + 1] ?? "") ? 1 : null; 256 } 257 258 let cursor = position; 259 260 while (/\d/.test(input[cursor] ?? "")) { 261 cursor += 1; 262 } 263 264 if (cursor === position || input[cursor] !== ".") { 265 return null; 266 } 267 268 return /\s/.test(input[cursor + 1] ?? "") ? cursor - position + 1 : null; 269} 270 271function markdownEmphasisMatcher( 272 input: string, 273 position: number, 274): { children: InternalRule[]; length: number; scope: string } | null { 275 if (input[position] === "*" && input[position + 1] !== "*") { 276 const end = findStyledSpanEnd(input, 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 }; 293 } 294 295 if (input[position] === "_" && input[position + 1] !== "_") { 296 const end = findStyledSpanEnd(input, 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 }; 313 } 314 315 return null; 316} 317 318function markdownHeadingMatcher( 319 input: string, 320 position: number, 321): { children: InternalRule[]; length: number; scope: string } | null { 322 if (lineOffset(input, position) === null || input[position] !== "#") { 323 return null; 324 } 325 326 let cursor = position; 327 328 while (input[cursor] === "#") { 329 cursor += 1; 330 } 331 332 if (cursor - position > 6) { 333 return null; 334 } 335 336 const next = input[cursor]; 337 338 if (next && next !== "\n" && next !== " " && next !== "\t") { 339 return null; 340 } 341 342 return { 343 children: MARKDOWN_INLINE_RULES, 344 length: findLineEnd(input, position) - position, 345 scope: "section", 346 }; 347} 348 349function 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 369function markdownInlineCodeMatcher( 370 input: string, 371 position: number, 372): number | null { 373 if (input[position] !== "`") { 374 return null; 375 } 376 377 const lineEnd = findLineEnd(input, position); 378 const end = input.indexOf("`", position + 1); 379 380 if (end === -1 || end >= lineEnd || end === position + 1) { 381 return null; 382 } 383 384 return end - position + 1; 385} 386 387function markdownLinkDefinitionMatcher( 388 input: string, 389 position: number, 390): { 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 425function markdownLinkMatcher( 426 input: string, 427 position: number, 428): { 429 emptyChildren?: EmptyChild[]; 430 children: InternalRule[]; 431 length: number; 432} | null { 433 if (input[position] !== "[") { 434 return null; 435 } 436 437 const lineEnd = findLineEnd(input, position); 438 const labelEnd = input.indexOf("]", position + 1); 439 440 if (labelEnd === -1 || labelEnd >= lineEnd) { 441 return null; 442 } 443 444 if (input[labelEnd + 1] === "(") { 445 const urlStart = labelEnd + 2; 446 const urlEnd = input.indexOf(")", urlStart); 447 448 if (urlEnd === -1 || urlEnd >= lineEnd) { 449 return null; 450 } 451 452 const urlLength = urlEnd - urlStart; 453 454 return { 455 children: createScopedRanges([ 456 { length: labelEnd - position - 1, scope: "string", start: 1 }, 457 { 458 length: urlLength, 459 scope: "link", 460 start: urlStart - position, 461 }, 462 ]), 463 emptyChildren: 464 urlLength === 0 465 ? [createEmptyChild(urlStart - position, "link")] 466 : undefined, 467 length: urlEnd - position + 1, 468 }; 469 } 470 471 if (input[labelEnd + 1] !== "[") { 472 return null; 473 } 474 475 const symbolStart = labelEnd + 2; 476 const symbolEnd = input.indexOf("]", symbolStart); 477 478 if (symbolEnd === -1 || symbolEnd >= lineEnd) { 479 return null; 480 } 481 482 return { 483 children: createScopedRanges([ 484 { length: labelEnd - position - 1, scope: "string", start: 1 }, 485 { 486 length: symbolEnd - symbolStart, 487 scope: "symbol", 488 start: symbolStart - position, 489 }, 490 ]), 491 length: symbolEnd - position + 1, 492 }; 493} 494 495function markdownSetextHeadingMatcher( 496 input: string, 497 position: number, 498): { children: InternalRule[]; length: number; scope: string } | null { 499 if (lineOffset(input, position) === null) { 500 return null; 501 } 502 503 const titleEnd = findLineEnd(input, position); 504 505 if (titleEnd === position || titleEnd >= input.length) { 506 return null; 507 } 508 509 const underlineStart = titleEnd + 1; 510 const underlineOffset = lineOffset(input, underlineStart); 511 512 if (underlineOffset === null) { 513 return null; 514 } 515 516 let cursor = underlineStart + underlineOffset; 517 const marker = input[cursor]; 518 519 if (marker !== "=" && marker !== "-") { 520 return null; 521 } 522 523 while (input[cursor] === marker) { 524 cursor += 1; 525 } 526 527 if (cursor - (underlineStart + underlineOffset) < 2) { 528 return null; 529 } 530 531 const underlineEnd = findLineEnd(input, underlineStart); 532 533 for (; cursor < underlineEnd; cursor += 1) { 534 if (input[cursor] !== " " && input[cursor] !== "\t") { 535 return null; 536 } 537 } 538 539 const titleRule = createNestedRangeRule( 540 0, 541 titleEnd - position, 542 MARKDOWN_INLINE_RULES, 543 ); 544 545 return { 546 children: titleRule ? [titleRule] : [], 547 length: underlineEnd - position, 548 scope: "section", 549 }; 550} 551 552function markdownStrongMatcher( 553 input: string, 554 position: number, 555): { children: InternalRule[]; length: number; scope: string } | null { 556 if (input.startsWith("***", position) || input.startsWith("___", position)) { 557 return null; 558 } 559 560 if (input.startsWith("**", position)) { 561 const end = findStyledSpanEnd(input, position, "**"); 562 563 if (end === null) { 564 return null; 565 } 566 567 const innerRule = createNestedRangeRule( 568 2, 569 end - position - 4, 570 MARKDOWN_STRONG_CHILD_RULES, 571 ); 572 573 return { 574 children: innerRule ? [innerRule] : [], 575 length: end - position, 576 scope: "strong", 577 }; 578 } 579 580 if (input.startsWith("__", position)) { 581 const end = findStyledSpanEnd(input, position, "__"); 582 583 if (end === null) { 584 return null; 585 } 586 587 const innerRule = createNestedRangeRule( 588 2, 589 end - position - 4, 590 MARKDOWN_STRONG_CHILD_RULES, 591 ); 592 593 return { 594 children: innerRule ? [innerRule] : [], 595 length: end - position, 596 scope: "strong", 597 }; 598 } 599 600 return null; 601} 602 603function markdownTildeFenceEndMatcher( 604 input: string, 605 position: number, 606): number | null { 607 return isFenceLine(input, position, "~") 608 ? findLineEnd(input, position) - position 609 : null; 610} 611 612function markdownTildeFenceStartMatcher( 613 input: string, 614 position: number, 615): { length: number; push: string; pushScope: string } | null { 616 return createFenceStartMatch(input, position, "~", "tildeFence"); 617} 618 619function markdownTripleStrongMatcher( 620 input: string, 621 position: number, 622): { children: InternalRule[]; length: number; scope: string } | null { 623 if (input.startsWith("***", position)) { 624 return createTripleStrongMatch(input, position, "***"); 625 } 626 627 if (input.startsWith("___", position)) { 628 return createTripleStrongMatch(input, position, "___"); 629 } 630 631 return null; 632} 633 634function createFenceStartMatch( 635 input: string, 636 position: number, 637 marker: "`" | "~", 638 state: string, 639): { length: number; push: string; pushScope: string } | null { 640 if (lineOffset(input, position) === null || input[position] !== marker) { 641 return null; 642 } 643 644 let cursor = position; 645 646 while (input[cursor] === marker) { 647 cursor += 1; 648 } 649 650 if (cursor - position < 3) { 651 return null; 652 } 653 654 return { 655 length: findLineEndWithNewline(input, position) - position, 656 push: state, 657 pushScope: "code", 658 }; 659} 660 661function createTripleStrongMatch( 662 input: string, 663 position: number, 664 delimiter: "***" | "___", 665): { children: InternalRule[]; length: number; scope: string } | null { 666 const end = findLastStyledSpanEnd(input, position, delimiter); 667 668 if (end === null) { 669 return null; 670 } 671 672 const emphasisRule = createNestedRangeRule( 673 2, 674 end - position - 4, 675 MARKDOWN_EMPHASIS_CHILD_RULES, 676 "emphasis", 677 ); 678 679 return { 680 children: emphasisRule ? [emphasisRule] : [], 681 length: end - position, 682 scope: "strong", 683 }; 684}