mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-19 14:45:44 +01:00
Answer stability round 1: 2 🔴, 7 🟡 (0 misread); look marks up in a Map, judge delimiters by whole code points, report a container's first refusal and test the mutants left
This commit is contained in:
@@ -344,6 +344,7 @@ for (const [panelType, colour] of [['error', '#ffebe6'], ['info', '#deebff'], ['
|
||||
|
||||
const external = { type: 'external', url: 'https://example.com/a.png' }
|
||||
const riddenBlocks: [string, AdfNode][] = [
|
||||
['an image whose mediaSingle holds no content', { attrs: { layout: 'center' }, type: 'mediaSingle' }],
|
||||
['an image whose mediaSingle holds no media', { attrs: { layout: 'center' }, content: [paragraph(text('a'))], type: 'mediaSingle' }],
|
||||
['an image whose mediaSingle holds its caption first', { attrs: { layout: 'center' }, content: [{ content: [text('c')], type: 'caption' }, { attrs: external, type: 'media' }], type: 'mediaSingle' }],
|
||||
['an image whose mediaSingle holds two media', { attrs: { layout: 'center' }, content: [{ attrs: external, type: 'media' }, { attrs: external, type: 'media' }], type: 'mediaSingle' }],
|
||||
@@ -361,9 +362,10 @@ const riddenBlocks: [string, AdfNode][] = [
|
||||
['a blockquote holding no content', { type: 'blockquote' }],
|
||||
['a list holding a paragraph', { content: [paragraph(text('a'))], type: 'bulletList' }],
|
||||
['a list holding a list item and a paragraph', { content: [item(paragraph(text('a'))), paragraph(text('b'))], type: 'bulletList' }],
|
||||
['a blockquote holding a list that holds a quote', { content: [list('bulletList', item(paragraph(text('a')), { content: [paragraph(text('b')), paragraph(text('c'))], type: 'blockquote' }))], type: 'blockquote' }],
|
||||
['a blockquote holding a panel that holds a quote', quote(panel('info', quote(paragraph(text('a')), paragraph(text('b')))))],
|
||||
['a list item holding a localId', list('bulletList', { attrs: { localId: 'i1' }, content: [paragraph(text('a'))], type: 'listItem' })],
|
||||
['a list item opening with a list', list('bulletList', item(list('bulletList', item(paragraph(text('a'))))))],
|
||||
['a panel whose panelType is an array', { attrs: { panelType: ['info'] }, content: [paragraph(text('a'))], type: 'panel' }],
|
||||
['a panel holding another attribute', { attrs: { panelIcon: ':x:', panelType: 'info' }, content: [paragraph(text('a'))], type: 'panel' }],
|
||||
['a table holding a paragraph', { attrs: plainTable, content: [paragraph(text('a'))], type: 'table' }],
|
||||
['a table holding a row and a paragraph', { attrs: plainTable, content: [{ content: [cell('tableCell', text('a'))], type: 'tableRow' }, paragraph(text('b'))], type: 'table' }],
|
||||
@@ -456,7 +458,10 @@ test('writes a link whose href starts with ~ with its text, since [~x] names a u
|
||||
const riddenMarks: [string, AdfMark][] = [
|
||||
['a colour followed by more', { attrs: { color: '#ff0000x' }, type: 'textColor' }],
|
||||
['a colour led by more', { attrs: { color: 'x#ff0000' }, type: 'textColor' }],
|
||||
['a colour held in an array', { attrs: { color: ['#ff0000'] }, type: 'textColor' }],
|
||||
['a colour holding another attribute', { attrs: { color: '#ff0000', x: 1 }, type: 'textColor' }],
|
||||
['a mark of type __proto__', { type: '__proto__' }],
|
||||
['a mark of type toString', { type: 'toString' }],
|
||||
['a subsup holding another attribute', { attrs: { type: 'sub', x: 1 }, type: 'subsup' }],
|
||||
['a mark of another type holding an href', { attrs: { href: 'https://example.com' }, type: 'fancyLink' }],
|
||||
['a link whose href is no string', { attrs: { href: 5 }, type: 'link' }],
|
||||
@@ -481,9 +486,29 @@ for (const [name, node] of riddenInlines) {
|
||||
})
|
||||
}
|
||||
|
||||
test('refuses a carry past 500 levels by its path in a list', () => {
|
||||
test('refuses a carry past 500 levels in a list by the path of the first', () => {
|
||||
let value: AdfAttributes[string] = 1
|
||||
for (let level = 0; level < largestNesting - 1; level += 1) value = [value]
|
||||
const deep: AdfNode = { attrs: { a: value }, type: 'unknownInline' }
|
||||
assert.equal(refusal(adfToLosslessJiraWiki(document(list('bulletList', item(paragraph(deep)))))), 'unsupported-nesting-depth at /content/0/content/0/content/0/content/0')
|
||||
assert.equal(refusal(adfToLosslessJiraWiki(document(list('bulletList', item(paragraph(deep)), item(paragraph(deep)))))), 'unsupported-nesting-depth at /content/0/content/0/content/0/content/0')
|
||||
})
|
||||
|
||||
test('carries a text holding no text', () => {
|
||||
assert.equal(wiki(paragraph(text('a'), { type: 'text' })), `a${inline('{"type":"text"}')}`)
|
||||
})
|
||||
|
||||
test('carries two inline nodes that each ride as two carries', () => {
|
||||
const first: AdfNode = { attrs: { a: 1 }, type: 'unknownInline' }
|
||||
const second: AdfNode = { attrs: { a: 2 }, type: 'unknownInline' }
|
||||
assert.equal(wiki(paragraph(first, second)), `${inline(JSON.stringify(first))}${inline(JSON.stringify(second))}`)
|
||||
})
|
||||
|
||||
test('reads a letter outside the Basic Multilingual Plane beside a delimiter as a letter', () => {
|
||||
const strong = text('x', { type: 'strong' })
|
||||
assert.equal(wiki(paragraph(text('\u{20000}'), strong)), `\u{20000}${inline(JSON.stringify(strong))}`)
|
||||
assert.equal(wiki(paragraph(strong, text('\u{20000}'))), `${inline(JSON.stringify(strong))}\u{20000}`)
|
||||
})
|
||||
|
||||
test('writes a delimiter followed by whitespace at a line start as itself', () => {
|
||||
assert.equal(line('+ a'), '+ a')
|
||||
})
|
||||
|
||||
@@ -9,7 +9,7 @@ import { spelledBlocks, type Spelling } from './spelled-blocks.ts'
|
||||
// `prefix`: the markers of the lists holding the block, one character per level.
|
||||
type Pending = string | { node: AdfNode; prefix: string }
|
||||
|
||||
const listMarkers: Readonly<Record<string, string>> = { bulletList: '*', orderedList: '#' }
|
||||
const listMarkers: ReadonlyMap<string, string> = new Map([['bulletList', '*'], ['orderedList', '#']])
|
||||
|
||||
export function adfToLosslessJiraWiki(document: AdfDocument): Result<string> {
|
||||
return withinEngineLimits(() => {
|
||||
@@ -51,7 +51,7 @@ function blockPieces(node: AdfNode, prefix: string, spelled: ReadonlyMap<AdfNode
|
||||
const text = spelled.get(node)?.text
|
||||
if (text !== undefined) return [text]
|
||||
const content = nodeContent(node)
|
||||
const marker = listMarkers[node.type]
|
||||
const marker = listMarkers.get(node.type)
|
||||
if (marker !== undefined) return listPieces(content, `${prefix}${marker}`)
|
||||
if (node.type === 'table') return tablePieces(content)
|
||||
const [only] = content
|
||||
|
||||
@@ -17,7 +17,7 @@ type SpelledMark = { delimiters: Delimiters; mark: AdfMark }
|
||||
|
||||
type Token = { checked: boolean; kind: 'atom' | 'close' | 'open'; text: string }
|
||||
|
||||
const characterDelimiters: Readonly<Record<string, string>> = { em: '_', strike: '-', strong: '*', underline: '+' }
|
||||
const characterDelimiters: ReadonlyMap<string, string> = new Map([['em', '_'], ['strike', '-'], ['strong', '*'], ['underline', '+']])
|
||||
const spellableUrl = /^[^\s[\]{}|\\]+$/
|
||||
const spellableColour = /^#[0-9a-f]{6}$/i
|
||||
|
||||
@@ -32,7 +32,7 @@ export function inlineLine(nodes: readonly AdfNode[], path: ConvertErrorPath): R
|
||||
// A group that opens with a delimiter or a carry opens with punctuation, which reads as the line's edge does.
|
||||
const following = next === undefined ? undefined : atoms[next.first]
|
||||
const after = following?.marks.length === 0 ? following.spelled?.text : undefined
|
||||
const written = groupSpelling(atoms.slice(first, last + 1), line.at(-1), after?.[0])
|
||||
const written = groupSpelling(atoms.slice(first, last + 1), lastCharacter(line), after === undefined ? undefined : firstCharacter(after))
|
||||
if (written === undefined) {
|
||||
const carry = inlineCarry(nodes.slice(first, last + 1), [...path, 'content', first])
|
||||
if (!carry.ok) return carry
|
||||
@@ -91,7 +91,7 @@ function hrefSpelling(node: AdfNode, nodes: readonly AdfNode[], index: number):
|
||||
// `undefined` where wiki markup has no spelling for the mark.
|
||||
function markDelimiters(mark: AdfMark): Delimiters | undefined {
|
||||
const attrs = nodeAttrs(mark)
|
||||
const character = characterDelimiters[mark.type]
|
||||
const character = characterDelimiters.get(mark.type)
|
||||
if (character !== undefined) return mark.attrs === undefined ? { checked: true, close: character, open: character } : undefined
|
||||
if (mark.type === 'code') return mark.attrs === undefined ? { checked: true, close: '}}', open: '{{' } : undefined
|
||||
if (mark.type === 'subsup') {
|
||||
@@ -147,7 +147,7 @@ function groupSpelling(atoms: readonly Atom[], before: string | undefined, after
|
||||
open = marks
|
||||
}
|
||||
for (const mark of open.slice().reverse()) tokens.push(delimiterToken(mark, 'close'))
|
||||
return tokens.every((token, index) => delimiterReads(token, tokens[index - 1]?.text.at(-1) ?? before, tokens[index + 1]?.text[0] ?? after)) ? tokens.map(({ text }) => text).join('') : undefined
|
||||
return tokens.every((token, index) => delimiterReads(token, lastCharacter(tokens[index - 1]?.text ?? '') ?? before, firstCharacter(tokens[index + 1]?.text ?? '') ?? after)) ? tokens.map(({ text }) => text).join('') : undefined
|
||||
}
|
||||
|
||||
function delimiterToken({ delimiters: { checked, close, open } }: SpelledMark, kind: 'close' | 'open'): Token {
|
||||
@@ -161,3 +161,13 @@ function delimiterReads({ checked, kind }: Token, before: string | undefined, af
|
||||
const inside = kind === 'open' ? after : before
|
||||
return (outside === undefined || !/[\p{L}\p{N}]/u.test(outside)) && inside !== undefined && !/\s/u.test(inside)
|
||||
}
|
||||
|
||||
// Whole code points, so a letter outside the Basic Multilingual Plane reads as a letter.
|
||||
function firstCharacter(text: string): string | undefined {
|
||||
const point = text.codePointAt(0)
|
||||
return point === undefined ? undefined : String.fromCodePoint(point)
|
||||
}
|
||||
|
||||
function lastCharacter(text: string): string | undefined {
|
||||
return /.$/su.exec(text)?.[0]
|
||||
}
|
||||
|
||||
@@ -29,7 +29,8 @@ export function spelledBlocks(content: readonly AdfNode[]): Result<Map<AdfNode,
|
||||
if (spelling !== undefined) spelled.set(node, spelling)
|
||||
} else if (containerHolds(node)) {
|
||||
pending.push({ exit: true, node, path })
|
||||
for (const [index, child] of nodeContent(node).entries()) pending.push({ exit: false, node: child, path: [...path, 'content', index] })
|
||||
const children = nodeContent(node).map((child, index): Step => ({ exit: false, node: child, path: [...path, 'content', index] }))
|
||||
for (const child of children.reverse()) pending.push(child)
|
||||
} else {
|
||||
const spelling = leafSpelling(node, path)
|
||||
if (!spelling.ok) return spelling
|
||||
|
||||
Reference in New Issue
Block a user