3
0
mirror of https://github.com/larvit/adf-codec.git synced 2026-11-21 07:32:58 +01:00

Respell the wiki writer's backslashes and inline carries as Jira reads them

This commit is contained in:
2026-11-17 02:58:01 +01:00
committed by Lillem4n
parent 6776a3c1d3
commit f36c0378fe
8 changed files with 120 additions and 57 deletions
+27 -17
View File
@@ -8,16 +8,19 @@ const delimiters = '*+-^_~'
const lineStartMarkers = '#*-'
const lineStartWords = /^(?:bq|h[1-6])\./i
const smileyOpeners = '(:;'
const smileyCharacters = smileys.map((smiley) => Array.from(smiley))
const smileyCharacters = smileys.map((smiley) => ({ characters: Array.from(smiley), parenthesised: /[()]$/.test(smiley) }))
const unprotectable = /[\\\r\n]|https?:\/\//i
// spec/wiki.md, Inline: `undefined` where no backslash protects the text, so it rides the carry.
export function escapedText(text: string, lineStart: boolean, lineEnd: boolean): string | undefined {
if (unprotectable.test(text)) return undefined
// spec/wiki.md, Inline: `undefined` where no backslash protects the text, so it rides the carry. `inCell`: the text stands in a table cell, where an escaped `|` still ends the cell.
export function escapedText(text: string, lineStart: boolean, lineEnd: boolean, inCell: boolean): string | undefined {
if (unprotectable.test(text) || (inCell && text.includes('|'))) return undefined
if ((lineStart && /^\s/u.test(text)) || (lineEnd && /\s$/u.test(text))) return undefined
const characters = Array.from(text)
if (lineStart && lineStartWords.test(text)) return undefined
const edges: { end: Side; start: Side } = { end: lineEnd ? 'space' : 'punctuation', start: lineStart ? 'space' : 'punctuation' }
return characters.map((character, index) => (escapes(character, characters, index, edges, lineStart) ? `\\${character}` : character)).join('')
const stops = smileyStops(characters, edges.end)
if (stops === undefined) return undefined
return characters.map((character, index) => (stops.has(index) || escapes(character, characters, index, edges, lineStart) ? `\\${character}` : character)).join('')
}
function escapes(character: string, characters: readonly string[], index: number, edges: { end: Side; start: Side }, lineStart: boolean): boolean {
@@ -26,23 +29,30 @@ function escapes(character: string, characters: readonly string[], index: number
const before = previous === undefined ? edges.start : side(previous)
const after = next === undefined ? edges.end : side(next)
if (alwaysEscaped.includes(character)) return true
if (lineStart && index === 0 && (lineStartMarkers.includes(character) || lineStartWords.test(characters.join('')))) return true
if (lineStart && index === 0 && lineStartMarkers.includes(character)) return true
// A delimiter both of whose neighbours are words, or both spaces, neither opens nor closes a run.
if (delimiters.includes(character) && !(before === after && before !== 'punctuation')) return true
if (character === '?' && next === '?') return true
if (character === '!' && after !== 'space') return true
return smileyOpeners.includes(character) && opensSmiley(characters, index, edges.end)
return character === '!' && after !== 'space'
}
// A smiley reads wherever no letter or digit follows it; one the node's edge cuts may close in the next node's spelling.
function opensSmiley(characters: readonly string[], index: number, end: Side): boolean {
return smileyCharacters.some((spelled) => {
const held = characters.slice(index, index + spelled.length)
if (held.some((character, offset) => character !== spelled[offset])) return false
if (held.length < spelled.length) return end !== 'space'
const next = characters[index + spelled.length]
return (next === undefined ? end : side(next)) !== 'word'
})
// A smiley reads wherever no letter or digit follows it; one the node's edge cuts may close in the next node's spelling. `undefined` where a smiley reads that no backslash stops: Jira keeps one before `:`, `;` and a letter, so only a `(` or a closing `)` takes it.
function smileyStops(characters: readonly string[], end: Side): Set<number> | undefined {
const stops = new Set<number>()
for (const [index, character] of characters.entries()) {
if (!smileyOpeners.includes(character)) continue
for (const { characters: spelled, parenthesised } of smileyCharacters) {
const held = characters.slice(index, index + spelled.length)
if (held.some((heldCharacter, offset) => heldCharacter !== spelled[offset])) continue
const cut = held.length < spelled.length
const next = characters[index + spelled.length]
if (cut ? end === 'space' : (next === undefined ? end : side(next)) === 'word') continue
if (character === '(') stops.add(index)
else if (!cut && parenthesised) stops.add(index + spelled.length - 1)
else return undefined
}
}
return stops
}
function side(character: string): Side {