diff --git a/src/plain-text/adf-to-plain-text.test.ts b/src/plain-text/adf-to-plain-text.test.ts index 6ccfe23..df610e2 100644 --- a/src/plain-text/adf-to-plain-text.test.ts +++ b/src/plain-text/adf-to-plain-text.test.ts @@ -96,6 +96,19 @@ test('writes nothing for a block holding no text, and parts the blocks around it assert.equal(plain(adfToPlainText(document(paragraph(), line('a'), { type: 'rule' }, paragraph(), line('b'), bullets(item())))), 'a\n\nb') }) +test('writes a text node holding nothing as nothing', () => { + assert.equal(plain(adfToPlainText(document(line('a'), paragraph(text('')), line('b')))), 'a\n\nb') +}) + +test('keeps the text of a paragraph on its line, and an item\'s blocks a line apart, whatever they hold', () => { + assert.equal(plain(adfToPlainText(document(bullets(item(paragraph(text('a'), text('b'))))))), '• ab') + assert.equal(plain(adfToPlainText(document(bullets(item(line('a')), item({ content: [line('b')], type: 'blockquote' }))))), '• a\n• b') + assert.equal(plain(adfToPlainText(document(bullets(item(bullets(item(line('x'))), line('z')))))), '• • x\n z') + assert.equal(plain(adfToPlainText(document(bullets(item(bullets(item()), line('x')))))), '• x') + const inline: AdfNode = paragraph({ text: 'a', type: 'unknownInline' }, { text: 'b', type: 'unknownInline' }) + assert.equal(plain(adfToPlainText(document(inline))), 'ab') +}) + test('numbers an ordered list from its first number, and widens the indent to the marker', () => { const list: AdfNode = { attrs: { order: 9 }, content: [item(line('nine'), paragraph(text('a'), { type: 'hardBreak' }), line('b')), item(line('ten'), bullets(item(line('x'))))], type: 'orderedList' } assert.equal(plain(adfToPlainText(document(list))), '9. nine\n a\n b\n10. ten\n • x') @@ -106,6 +119,11 @@ test('keeps a marker for an item whose first content is a nested list', () => { assert.equal(plain(adfToPlainText(document(bullets(item(bullets(item(line('x')))), item(line('y')))))), '• • x\n• y') }) +test('writes a task item holding blocks as an item', () => { + const tasks: AdfNode = { content: [{ attrs: { localId: 'a', state: 'TODO' }, content: [line('a'), line('b')], type: 'blockTaskItem' }], type: 'taskList' } + assert.equal(plain(adfToPlainText(document(tasks))), '• a\n b') +}) + test('writes a task list and a decision list as bullets', () => { const tasks: AdfNode = { content: [{ attrs: { state: 'DONE' }, content: [text('Write the spec')], type: 'taskItem' }, { attrs: { state: 'TODO' }, content: [text('Ship')], type: 'taskItem' }], type: 'taskList' } const decisions: AdfNode = { content: [{ attrs: { state: 'DECIDED' }, content: [text('Use Node')], type: 'decisionItem' }], type: 'decisionList' } @@ -148,6 +166,7 @@ test('refuses what adfToHtml refuses, with the same code and path', () => { { type: 'doc', version: Number.NaN }, { type: 'doc', version: 2 }, document(deep(largestNesting + 1)), + document(line('a'), deep(largestNesting + 1)), document(paragraph(text('See '), { attrs: { url: 'https://example.com/' }, marks: [{ attrs: { href: 'https://example.com/' }, type: 'link' }], type: 'inlineCard' })), ] for (const input of refused) { @@ -156,6 +175,23 @@ test('refuses what adfToHtml refuses, with the same code and path', () => { } }) +test('counts a table\'s levels as adfToHtml does', () => { + const deep = (levels: number): AdfNode => { + let node = line('x') + for (let level = 0; level < levels; level += 1) node = { content: [node], type: 'blockquote' } + return node + } + const outcomes = new Set() + for (const cell of ['tableCell', 'tableHeader']) { + for (let levels = largestNesting - 3; levels <= largestNesting + 1; levels += 1) { + const table: AdfNode = { content: [{ content: [{ content: [deep(levels)], type: cell }], type: 'tableRow' }], type: 'table' } + assert.equal(refusal(adfToPlainText(document(table))), refusal(adfToHtml(document(table)))) + outcomes.add(refusal(adfToPlainText(document(table))).slice(0, 12)) + } + } + assert.deepEqual([...outcomes].sort(), ['unsupported-', 'written']) +}) + test('writes blocks nested 500 levels', () => { let node = line('x') for (let level = 0; level < largestNesting; level += 1) node = { content: [node], type: 'blockquote' } @@ -170,7 +206,9 @@ test('writes blocks nested 500 levels', () => { test('refuses text past V8\'s longest string, which the indent of a deep list can add', () => { let list: AdfNode = { content: [text('a\n'.repeat(400000))], type: 'codeBlock' } for (let level = 0; level < 400; level += 1) list = bullets(item(list)) - const refused = adfToPlainText(document(list, list)) + let tooDeep: AdfNode = line('x') + for (let level = 0; level <= largestNesting; level += 1) tooDeep = { content: [tooDeep], type: 'blockquote' } + const refused = adfToPlainText(document(list, list, tooDeep)) assert.equal(!refused.ok && refused.error.code, 'unsupported-node-shape') assert.equal(!refused.ok && refused.error.message.includes('536870888'), true) }) diff --git a/src/plain-text/adf-to-plain-text.ts b/src/plain-text/adf-to-plain-text.ts index e960309..69ac1c1 100644 --- a/src/plain-text/adf-to-plain-text.ts +++ b/src/plain-text/adf-to-plain-text.ts @@ -7,11 +7,11 @@ import { longestOutput, tooLong, withinEngineLimits } from '../length-bound.ts' import { nodeAttrs, nodeContent, readAdfDocument, writableContent } from '../adf/document.ts' import { shownText } from '../adf/shown-text.ts' -// `gap`: the line feeds the next text needs before it, at least; `leave`: the state a list item restores; `marker`: the list marker of the item a node is; `depth`: the nesting level, as `largestNesting` counts it; `parent`: the node holding this one, from which its path is built when a refusal needs it. +// `gap`: the line feeds the next text needs before it, at least; `leave`: the state a list item restores, its `length` telling whether the item wrote anything; `marker`: the list marker of the item a node is; `depth`: the nesting level, as `largestNesting` counts it; `parent`: the node holding this one, from which its path is built when a refusal needs it. type Place = { index: number; parent: Place | undefined } type Step = | { kind: 'gap'; lines: number } - | { kind: 'leave'; indent: string; lead: string | undefined; written: number } + | { kind: 'leave'; indent: string; lead: string | undefined; length: number } | { depth: number; kind: 'node'; marker: string | undefined; node: AdfNode; place: Place | undefined } // `lines`: the line feeds between a node's children; `level`: how much deeper they nest. @@ -37,19 +37,15 @@ function writeText(content: readonly AdfNode[]): Result { let gap = 0 let indent = '' let lead: string | undefined - let written = 0 function emit(text: string): void { if (text === '') return - if (lead !== undefined && gap === 0) gap = 1 - const atLineStart = written === 0 || gap > 0 - const head = atLineStart ? `${written === 0 ? '' : '\n'.repeat(gap)}${lead ?? indent}` : '' - const body = indent === '' ? text : text.replaceAll(/\n(?=[^\n])/g, `\n${indent}`) + const head = length === 0 || gap > 0 ? `${length === 0 ? '' : '\n'.repeat(gap)}${lead ?? indent}` : '' + const body = text.replaceAll(/\n(?=[^\n])/g, `\n${indent}`) pieces.push(head, body) length += head.length + body.length gap = 0 lead = undefined - written += 1 } const pending: Step[] = childSteps(content, { level: 1, lines: 2 }, 0, undefined, () => undefined).reverse() @@ -60,18 +56,17 @@ function writeText(content: readonly AdfNode[]): Result { } if (step.kind === 'leave') { indent = step.indent - lead = step.written === written ? step.lead : undefined + lead = step.length === length ? step.lead : undefined continue } const { depth, marker, node, place } = step if (depth > largestNesting) return failure('unsupported-nesting-depth', `the document nests deeper than the ${largestNesting} levels the writer carries`, pathTo(place)) if (marker !== undefined) { - pending.push({ indent, kind: 'leave', lead, written }) + pending.push({ indent, kind: 'leave', lead, length }) lead = lead === undefined ? `${indent}${marker}` : `${lead}${marker}` indent = `${indent}${' '.repeat(marker.length)}` } if (node.type === 'hardBreak') gap = Math.max(gap, 1) - else if (node.type === 'text') emit(node.text ?? '') else if (shownTextTypes.includes(node.type)) emit(shownText(node) ?? '') else { if (typeof node.text === 'string') emit(node.text) @@ -87,7 +82,7 @@ function writeText(content: readonly AdfNode[]): Result { function childSteps(nodes: readonly AdfNode[], layout: Layout, depth: number, parent: Place | undefined, markerFor: (index: number) => string | undefined): Step[] { const steps: Step[] = [] for (const [index, node] of nodes.entries()) { - if (index > 0 && layout.lines > 0) steps.push({ kind: 'gap', lines: layout.lines }) + if (index > 0) steps.push({ kind: 'gap', lines: layout.lines }) steps.push({ depth, kind: 'node', marker: markerFor(index), node, place: { index, parent } }) } return steps