35b - the writer spells the plain flavour and escapes text that would read as its markers

This commit is contained in:
2026-09-29 11:42:46 +02:00
parent 7c15b02258
commit 06103ea2ba
10 changed files with 305 additions and 107 deletions
+22
View File
@@ -1,3 +1,5 @@
import { isWordCharacter } from './commonmark/emphasis-matching.ts'
export type Flavour = 'lossless' | 'plain'
type AlertMarker = { folded: boolean; length: number; panelType: string }
@@ -45,6 +47,26 @@ export function readAlertMarker(text: string): AlertMarker | undefined {
return { folded: marker[2] !== '', length: marker[0].length, panelType: panelType ?? 'info' }
}
// A marker leads text that ends at it or goes on past whitespace or a hard break.
export function leadingMarker<T extends { length: number }>(text: string, read: (text: string) => T | undefined): T | undefined {
const marker = read(text)
if (marker === undefined) return undefined
const rest = text.slice(marker.length)
return rest === '' || /^(?:[ \t\n]|\\\n)/.test(rest) ? marker : undefined
}
// A delimiter is bounded outside by the code point beyond it, and flanks by the character inside it.
export function highlightFlanking(source: string, index: number): { closes: boolean; opens: boolean } {
const end = index + highlightDelimiter.length
const before = Array.from(source.slice(Math.max(0, index - 2), index)).at(-1) ?? ''
const after = Array.from(source.slice(end, end + 2))[0] ?? ''
return { closes: flanks(before) && !isWordCharacter(after), opens: flanks(after) && !isWordCharacter(before) }
}
function flanks(character: string): boolean {
return character !== '' && !/\s/.test(character)
}
export function taskMarker(state: unknown): string {
return state === 'DONE' ? '[x]' : '[ ]'
}