From 6f01afd4f6d2f205af044938c7fd31963a902da1 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Sat, 14 Nov 2026 21:46:14 +0100 Subject: [PATCH] Kill adfToPlainText's cell and card mutants --- src/plain-text/adf-to-plain-text.test.ts | 16 ++++++++++++++++ src/plain-text/adf-to-plain-text.ts | 6 +++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/plain-text/adf-to-plain-text.test.ts b/src/plain-text/adf-to-plain-text.test.ts index ffa072b..1b21729 100644 --- a/src/plain-text/adf-to-plain-text.test.ts +++ b/src/plain-text/adf-to-plain-text.test.ts @@ -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 = { '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') diff --git a/src/plain-text/adf-to-plain-text.ts b/src/plain-text/adf-to-plain-text.ts index 11f7571..23528d0 100644 --- a/src/plain-text/adf-to-plain-text.ts +++ b/src/plain-text/adf-to-plain-text.ts @@ -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.