import assert from 'node:assert/strict' import test from 'node:test' import type { AdfDocument, AdfMark, AdfNode } from '../../adf/document.ts' import type { Result } from '../../result.ts' import { adfToLosslessMarkdown, losslessMarkdownToAdf } from '../../index.ts' function document(...content: AdfNode[]): AdfDocument { return { content, type: 'doc', version: 1 } } function paragraph(...content: AdfNode[]): AdfNode { return content.length === 0 ? { type: 'paragraph' } : { content, type: 'paragraph' } } function markdown(result: Result): string { return result.ok ? result.value : `${result.error.code}: ${result.error.message}` } function readsBack(doc: AdfDocument): string { const spelled = markdown(adfToLosslessMarkdown(doc)) assert.deepEqual(losslessMarkdownToAdf(spelled), { ok: true, value: doc }, `reading ${JSON.stringify(spelled)}`) return spelled } test('spells a code span opening a line, whose backticks open no fence', () => { const fence: AdfNode = { marks: [{ type: 'code' }], text: '``', type: 'text' } assert.equal(readsBack(document(paragraph(fence))), '``` `` ```\n') assert.equal(readsBack(document(paragraph({ text: 'a', type: 'text' }, { type: 'hardBreak' }, fence))), 'a\\\n``` `` ```\n') }) test('escapes the delimiter row a hard break leaves opening a pipe table with no leading pipe', () => { const broken = (second: string): string => markdown(adfToLosslessMarkdown(document(paragraph({ text: 'a | b', type: 'text' }, { type: 'hardBreak' }, { text: second, type: 'text' })))) assert.equal(broken('--- | ---'), 'a | b\\\n\\--- | ---\n') assert.equal(broken(':--- | ---:'), 'a | b\\\n\\:--- | ---:\n') assert.equal(broken('c | d'), 'a | b\\\nc | d\n') }) test('escapes only text that would otherwise open a construct', () => { const emitted = (text: string): string => markdown(adfToLosslessMarkdown(document(paragraph({ text, type: 'text' })))) assert.equal(emitted('
'), '\\
\n') assert.equal(emitted(' e'), 'a \\ e\n') assert.equal(emitted('a c'), 'a \\ c\n') const later = paragraph({ text: 'a', type: 'text' }, { type: 'hardBreak' }, { text: ' \\