Answer the review: one home per contract, and cover the matching's last branch
CI / gate (push) Successful in 5s

This commit is contained in:
2026-08-27 13:17:08 +02:00
parent a8bea25b7c
commit 1108d36d1f
7 changed files with 76 additions and 30 deletions
+6 -19
View File
@@ -297,32 +297,19 @@ test('escapes a literal delimiter that would merge with an emitted one', () => {
assert.equal(emitted(marked('x', { attrs: { href: 'https://example.com/' }, type: 'link' }), { text: '{}', type: 'text' }), '[x](https://example.com/){}\n')
})
test('escapes a literal delimiter run that flanks either way', () => {
const marked = (text: string, ...marks: AdfMark[]): AdfNode => ({ marks, text, type: 'text' })
const emitted = (...content: AdfNode[]): string => markdown(adfToMarkdown(document(paragraph(...content))))
assert.equal(emitted({ text: 'un', type: 'text' }, marked('a* b', { type: 'em' }), { text: 'istic', type: 'text' }), 'un*a\\* b*istic\n')
assert.equal(emitted(marked('a~~ b', { type: 'strike' })), '~~a\\~~ b~~\n')
assert.equal(emitted(marked('a_ b', { type: 'em' })), '_a\\_ b_\n')
assert.equal(emitted({ text: 'a* b', type: 'text' }), 'a\\* b\n')
assert.equal(emitted({ text: '2 * 3', type: 'text' }), '2 * 3\n')
assert.equal(emitted({ text: 'snake_case_name', type: 'text' }), 'snake_case_name\n')
test('escapes a literal delimiter run that only closes', () => {
const emitted = (text: string): string => markdown(adfToMarkdown(document(paragraph({ text, type: 'text' }))))
assert.equal(emitted('a* b'), 'a\\* b\n')
assert.equal(emitted('2 * 3'), '2 * 3\n')
})
test('carries a mark run CommonMark matching pairs elsewhere', () => {
test("spells a mark run CommonMark's matching pairs as written", () => {
const marked = (text: string, ...marks: AdfMark[]): AdfNode => ({ marks, text, type: 'text' })
const emitted = (...content: AdfNode[]): string => markdown(adfToMarkdown(document(paragraph(...content))))
const em: AdfMark = { type: 'em' }
const strong: AdfMark = { type: 'strong' }
assert.equal(
emitted({ text: 'un', type: 'text' }, marked('a', em), marked('b', em, strong), marked('c', strong), { text: 'istic', type: 'text' }),
'un:adf{json="{\\"marks\\":[{\\"type\\":\\"em\\"}],\\"text\\":\\"a\\",\\"type\\":\\"text\\"}"}' +
':adf{json="{\\"marks\\":[{\\"type\\":\\"em\\"},{\\"type\\":\\"strong\\"}],\\"text\\":\\"b\\",\\"type\\":\\"text\\"}"}**c**istic\n',
)
assert.equal(
emitted(marked('a', strong, em), marked('b', strong), marked('c', strong, em)),
'***a*b**:adf{json="{\\"marks\\":[{\\"type\\":\\"strong\\"},{\\"type\\":\\"em\\"}],\\"text\\":\\"c\\",\\"type\\":\\"text\\"}"}\n',
)
assert.equal(emitted({ text: 'un', type: 'text' }, marked('a', em, strong), { text: 'istic', type: 'text' }), 'un***a***istic\n')
assert.equal(emitted({ text: 're', type: 'text' }, marked('structure', strong), { text: ' the code', type: 'text' }), 're**structure** the code\n')
assert.equal(
emitted({ text: 'un', type: 'text' }, marked('a', em), marked('b', em, strong), marked('c', em), { text: 'istic', type: 'text' }),
'un*a**b**c*istic\n',
+1 -2
View File
@@ -72,7 +72,6 @@ function roundTripFixtures(): { name: string; path: string }[] {
)
}
// One spelling for two documents is a round-trip break no parser can undo, and no parser is needed to see it.
test('no two round-trip documents share one markdown spelling', () => {
const spellings = new Map<string, string>()
for (const fixture of roundTripFixtures()) {
@@ -89,7 +88,7 @@ test('no round-trip fixture repeats the document another holds', () => {
const documents = new Map<string, string>()
for (const fixture of roundTripFixtures()) {
const parsed: unknown = JSON.parse(readFileSync(fixture.path, 'utf8'))
assert.ok(isJsonValue(parsed))
assert.ok(isJsonValue(parsed), `${fixture.name} does not hold a JSON value`)
const document = serializeCanonicalJson(parsed, 'compact')
assert.equal(documents.get(document), undefined, `${fixture.name} repeats the document ${documents.get(document)} holds`)
documents.set(document, fixture.name)
+2 -4
View File
@@ -1,8 +1,8 @@
import { isUnicodeWhitespace } from './commonmark-grammar.ts'
export type DelimiterRun = { canClose: boolean; canOpen: boolean; character: string; length: number }
type DelimiterRun = { canClose: boolean; canOpen: boolean; character: string; length: number }
export type EmphasisPairing<Run> = { closer: Run; closerOffset: number; opener: Run; openerOffset: number; used: number }
type EmphasisPairing<Run> = { closer: Run; closerOffset: number; opener: Run; openerOffset: number; used: number }
type Candidate<Run> = {
head: number
@@ -16,7 +16,6 @@ type Candidate<Run> = {
const unicodePunctuation = /[\p{P}\p{S}]/u
// spec/flavour.md, Canonical form: CommonMark's own can-open and can-close, which `~` follows too.
export function delimiterFlags(character: string, before: string, after: string): { canClose: boolean; canOpen: boolean } {
const left = isLeftFlanking(before, after)
const right = isRightFlanking(before, after)
@@ -28,7 +27,6 @@ export function isWordCharacter(character: string): boolean {
return character !== '' && !isWhitespace(character) && !isPunctuation(character)
}
// Flanking decides which delimiters may pair; this decides which ones do, and a pair it leaves out reads back as another document.
export function matchEmphasis<Run extends DelimiterRun>(runs: readonly Run[]): EmphasisPairing<Run>[] {
const pairings: EmphasisPairing<Run>[] = []
const bottoms = new Map<string, Candidate<Run> | undefined>()
-3
View File
@@ -257,9 +257,6 @@ function runLength(scan: string, index: number): number {
return length
}
function charAt(text: string, index: number): string {
return index < 0 ? '' : text.charAt(index)
}