| 356 | } |
| 357 | |
| 358 | export const markdownSafetyIssues = (document: string): ReadonlyArray<string> => { |
| 359 | const issues: Array<string> = [] |
| 360 | let fence: string | undefined |
| 361 | const lines = document.split("\n") |
| 362 | for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) { |
| 363 | const line = lines[lineIndex]! |
| 364 | const fenceMatch = /^\s*(`{3,})(?:[^`]*)$/.exec(line) |
| 365 | if (fence !== undefined) { |
| 366 | if (line.trim() === fence) { |
| 367 | fence = undefined |
| 368 | } |
| 369 | continue |
| 370 | } |
| 371 | if (fenceMatch !== null) { |
| 372 | fence = fenceMatch[1]! |
| 373 | continue |
| 374 | } |
| 375 | if ( |
| 376 | line === "<!-- dprint-ignore-file -->" || |
| 377 | line.startsWith("**Replacement:** ") || |
| 378 | line === "**Example**" |
| 379 | ) { |
| 380 | continue |
| 381 | } |
| 382 | let index = 0 |
| 383 | while (index < line.length) { |
| 384 | const character = line[index]! |
| 385 | if (character === "\\") { |
| 386 | index += 2 |
| 387 | continue |
| 388 | } |
| 389 | if (character === "`") { |
| 390 | let end = index + 1 |
| 391 | while (line[end] === "`") { |
| 392 | end++ |
| 393 | } |
| 394 | const delimiter = line.slice(index, end) |
| 395 | const close = line.indexOf(delimiter, end) |
| 396 | if (close === -1) { |
| 397 | issues.push(`line ${lineIndex + 1}: unclosed inline code span`) |
| 398 | break |
| 399 | } |
| 400 | index = close + delimiter.length |
| 401 | continue |
| 402 | } |
| 403 | if (character === "~" || character === "<" || character === "*" || character === "_") { |
| 404 | issues.push(`line ${lineIndex + 1}: unescaped ${JSON.stringify(character)}`) |
| 405 | } |
| 406 | index++ |
| 407 | } |
| 408 | } |
| 409 | if (fence !== undefined) { |
| 410 | issues.push(`unclosed ${fence} code fence`) |
| 411 | } |
| 412 | return issues |
| 413 | } |
| 414 | |
| 415 | export const unannotatedApiIds = ( |