4.3: the markdown property #80

Merged
lilleman merged 12 commits from markdown-property into main 2026-09-15 22:48:43 +02:00
4 changed files with 63 additions and 6 deletions
Showing only changes of commit aeb54d98d1 - Show all commits
@@ -0,0 +1,31 @@
{
"content": [
{
"content": [
{
"text": "`a ``b``",
"type": "text"
}
],
"type": "paragraph"
},
{
"content": [
{
"text": "`a",
"type": "text"
},
{
"type": "hardBreak"
},
{
"text": "```b``",
"type": "text"
}
],
"type": "paragraph"
}
],
"type": "doc",
"version": 1
}
@@ -0,0 +1,4 @@
\`a \`\`b``
\`a\
\`\`\`b``
+2
View File
@@ -54,6 +54,8 @@ normalizes to it through the round-trip.
- Entity references in input decode to their characters; output backslash-escapes only where text
would otherwise parse as syntax, scanning the assembled line rather than each text node: escape
the leading delimiter of a construct that would otherwise open, re-scan from there, and repeat.
A backtick run escapes whole, and a lone backtick escapes wherever an escaped one follows it in
the same inline content: CommonMark reads no escape inside a code span, so `` \` `` closes one.
A paragraph's opening `[` escapes wherever its line reads as a link reference definition, which
resolves before any inline construct binds: a `]` inside a code span or `{attrs}` counts.
An emphasis delimiter run in text escapes where CommonMark can open **or** close with it, so
+26 -6
View File
@@ -67,9 +67,20 @@ function escape(segments: readonly InlineSegment[], container: LineContainer): A
const scan = segments.map((segment) => segment.text).join('')
const escapings: InlineEscaping[] = []
for (const segment of segments) for (let index = 0; index < segment.text.length; index += 1) escapings.push(segment.escaping)
const escaped = new Set<number>()
const escaped = escapedIndexes(scan, escapings, container)
const placements: number[] = []
let output = ''
for (let index = 0; index < scan.length; index += 1) {
if (escaped.has(index)) output += '\\'
placements.push(output.length)
output += scan.charAt(index)
}
const line = container === 'paragraph' && opensLinkDefinition(output) ? `\\${output}` : output
return { line, unspellableRun: unspellableRun(segments, output, placements) }
}
function escapedIndexes(scan: string, escapings: readonly InlineEscaping[], container: LineContainer): Set<number> {
const escaped = new Set<number>()
const linkClose = lastLinkClose(scan, escapings)
let line = scanLine(scan, 0)
for (let index = 0; index < scan.length; index += 1) {
@@ -84,14 +95,21 @@ function escape(segments: readonly InlineSegment[], container: LineContainer): A
(escaping === 'bracketed-link-target' &&
((scan.charAt(index) === '`' && opensCodeSpan(scan, index, escaped)) || (scan.charAt(index) === ':' && opensInlineDirective(scan, index))))
) {
output += '\\'
escaped.add(index)
}
placements.push(output.length)
output += scan.charAt(index)
}
const spelled = container === 'paragraph' && opensLinkDefinition(output) ? `\\${output}` : output
return { line: spelled, unspellableRun: unspellableRun(segments, output, placements) }
escapeLoneBackticks(scan, escapings, escaped)
return escaped
}
// CommonMark reads no escape inside a code span, so an escaped backtick still closes one a bare lone backtick before it opens.
function escapeLoneBackticks(scan: string, escapings: readonly InlineEscaping[], escaped: Set<number>): void {
let escapedAfter = false
for (let index = scan.length - 1; index >= 0; index -= 1) {
if (scan.charAt(index) !== '`') continue
if (escapedAfter && escapings[index] !== 'none' && scan.charAt(index - 1) !== '`' && scan.charAt(index + 1) !== '`') escaped.add(index)
if (escaped.has(index)) escapedAfter = true
}
}
function unspellableRun(segments: readonly InlineSegment[], output: string, placements: readonly number[]): NodeRange | undefined {
@@ -248,6 +266,8 @@ function lastLinkClose(scan: string, escapings: readonly (InlineEscaping | undef
}
function opensCodeSpan(scan: string, index: number, escaped: ReadonlySet<number>): boolean {
// A run escapes whole: a rest left bare would be a raw run of another length for a closer.
if (scan.charAt(index - 1) === '`' && escaped.has(index - 1)) return true
if (!startsRun(scan, index, escaped)) return false
const opener = backtickRun(scan, index)
return closingBacktickRun(scan, index + opener, opener) !== undefined