3
0
mirror of https://github.com/larvit/adf-codec.git synced 2026-11-18 22:25:44 +01:00

Kill adfToPlainText's cell and card mutants

This commit is contained in:
2026-11-14 21:46:14 +01:00
committed by Lillem4n
parent d5935c6ac0
commit 6f01afd4f6
2 changed files with 19 additions and 3 deletions
+16
View File
@@ -278,6 +278,18 @@ test('writes a table inside a list item under the item\'s indent', () => {
assert.equal(plain(adfToPlainText(document(bullets(item(line('t'), table(cells('a', 'b'), cells('c', 'd'))))))), '• t\n a | b\n c | d')
})
test('writes the text nodes of a cell without a space between them, and a list in a nested table behind its own marker', () => {
const marked: AdfNode = { marks: [{ type: 'strong' }], text: 'b', type: 'text' }
assert.equal(plain(adfToPlainText(document(table(row(cell(paragraph(text('a'), marked)), cell(line('c'))))))), 'ab | c')
const listed = row(cell(bullets(item(line('p')), item(line('q')))), cell(line('y')))
assert.equal(plain(adfToPlainText(document(bullets(item(line('t'), table(listed)))))), '• t\n • p • q | y')
})
test('writes a blank line and a line feed ending a code fragment in a cell as one space each', () => {
const code = (...fragments: string[]): AdfNode => ({ content: fragments.map((fragment) => text(fragment)), type: 'codeBlock' })
assert.equal(plain(adfToPlainText(document(table(row(cell(code('c\n\nd')), cell(code('e\n', 'f'))))))), 'c d | e f')
})
const external: AdfNode = { attrs: { alt: 'Team photo', type: 'external', url: 'https://example.com/team.png' }, type: 'media' }
const uploaded: AdfNode = { attrs: { alt: 'Quarterly chart', id: 'f', type: 'file' }, type: 'media' }
@@ -373,6 +385,10 @@ const refusedDocuments: Record<string, AdfDocument> = {
'a card with a link mark': document(paragraph(text('See '), { attrs: { url: 'https://example.com/' }, marks: [{ attrs: { href: 'https://example.com/' }, type: 'link' }], type: 'inlineCard' })),
}
test('names the limit it refuses a document for', () => {
assert.equal(plain(adfToPlainText(document(deepQuote(largestNesting + 1)))), `unsupported-nesting-depth: the document nests deeper than the ${largestNesting} levels the writer carries`)
})
for (const [name, input] of Object.entries(refusedDocuments)) {
test(`refuses ${name} as adfToHtml does, with the same code and path`, () => {
assert.notEqual(refusal(adfToHtml(input)), 'written')
+3 -3
View File
@@ -116,7 +116,7 @@ function emit(writing: Writing, text: string): void {
writing.gap = 0
writing.lead = undefined
}
writing.literal = (end > 0 ? 0 : writing.literal) + (writing.cell === undefined ? text.length - end : 0)
writing.literal = (end > 0 ? 0 : writing.literal) + (text.length - end)
}
function endBeforeLineFeeds(text: string): number {
@@ -149,9 +149,9 @@ function childSteps(nodes: readonly AdfNode[], layout: Layout, depth: number, pa
// A card shows its address, or its name and then its address.
function cardText(card: AdfNode): string {
const text = shownText(card)
const text = shownText(card) ?? ''
const url = shownUrl(card)
return text === undefined || url === undefined || url === text ? text ?? '' : `${text} (${url})`
return url === undefined || url === text ? text : `${text} (${url})`
}
// An image shows its alt text, else its address, else `[image]`; then the target of the link around it, then its own address where the alt text stood in for it.