Answer the stability nits: the runs a trailing-anchored regex re-walks
CI / gate (push) Successful in 8s

This commit is contained in:
2026-09-01 20:11:41 +02:00
parent e68dbc41a3
commit 5a41547222
4 changed files with 45 additions and 34 deletions
+14 -2
View File
@@ -223,6 +223,18 @@ export function setextHeadingLevel(line: string): number | undefined {
return underline.startsWith('=') ? 1 : 2
}
export function trimSpace(text: string): string {
return text.replace(/^[ \t]+|[ \t]+$/g, '')
function spaceOrTab(character: string): boolean {
return character === ' ' || character === '\t'
}
export function trimSpace(text: string): string {
let start = 0
while (start < text.length && spaceOrTab(text.charAt(start))) start += 1
return trimTrailingSpace(text.slice(start))
}
export function trimTrailingSpace(text: string): string {
let end = text.length
while (end > 0 && spaceOrTab(text.charAt(end - 1))) end -= 1
return text.slice(0, end)
}
+3 -5
View File
@@ -1,7 +1,7 @@
import type { AdfMark, AdfNode } from '../../adf/document.ts'
import type { EmphasisPairing } from '../emphasis-matching.ts'
import type { LinkDefinition } from '../link-syntax.ts'
import { backslashEscape, decodeTextEscapes, inlineHtmlConstruct, readBracketedAutolink, readEmailAutolink } from '../commonmark-grammar.ts'
import { backslashEscape, decodeTextEscapes, inlineHtmlConstruct, readBracketedAutolink, readEmailAutolink, trimTrailingSpace } from '../commonmark-grammar.ts'
import { backtickRun, closingBacktickRun } from '../backtick-runs.ts'
import { delimiterFlags, matchEmphasis, runLength } from '../emphasis-matching.ts'
import { failure, faulted, success, type ConvertErrorPath, type Result } from '../../result.ts'
@@ -28,9 +28,7 @@ type Run = { canClose: boolean; canOpen: boolean; character: string; index: numb
type Scan = { definitions: LinkDefinitions; path: ConvertErrorPath; pending: string; pieces: Piece[]; source: string }
const hardBreakSpaces = / {2,}$/
const imageAlone = 'an image fits only as a paragraph of its own'
const trailingSpace = /[ \t]+$/
export function parseInlineContent(source: string, definitions: LinkDefinitions, path: ConvertErrorPath): Result<InlineContent> {
const scan: Scan = { definitions, path, pending: '', pieces: [], source }
@@ -95,7 +93,7 @@ function readBackslash(scan: Scan, index: number): number {
}
function readLineEnding(scan: Scan, index: number): number {
const hard = hardBreakSpaces.test(scan.pending)
const hard = scan.pending.endsWith(' ')
flush(scan, true)
if (hard) pushNode(scan, { type: 'hardBreak' })
else scan.pending = ' '
@@ -154,7 +152,7 @@ function readDirective(scan: Scan, index: number): Result<number> {
}
function flush(scan: Scan, strip: boolean): void {
const raw = strip ? scan.pending.replace(trailingSpace, '') : scan.pending
const raw = strip ? trimTrailingSpace(scan.pending) : scan.pending
scan.pending = ''
if (raw !== '') scan.pieces.push({ kind: 'nodes', nodes: [{ text: decodeTextEscapes(raw), type: 'text' }] })
}