3
0
mirror of https://github.com/larvit/adf-codec.git synced 2026-11-19 14:45:44 +01:00

Write indentation as data-indent-level in HTML and read an opened details as the same expand

This commit is contained in:
2026-11-15 11:28:38 +01:00
committed by Lillem4n
parent 03e219ed74
commit ed03d42d8b
14 changed files with 122 additions and 75 deletions
-36
View File
@@ -1,36 +0,0 @@
import type { AdfAttributes, AdfMark, AdfNode } from '../adf/document.ts'
import type { AttributeVocabulary } from '../adf/attribute-vocabulary.ts'
import type { Read } from '../result.ts'
import { dataAttributes, readDataAttributes } from './data-attributes.ts'
import { nodeAttrs, nodeMarks } from '../adf/document.ts'
// The `text-align` value each alignment names.
function alignments(): ReadonlyMap<string, string> {
return new Map([
['center', 'center'],
['end', 'right'],
])
}
// A paragraph's or heading's attributes: its alignment mark as a `style` attribute, then the rest as `dataAttributes` writes them; `undefined` where it holds a mark besides one alignment the style spells.
export function textBlockAttributes(node: AdfNode, vocabulary: AttributeVocabulary, written: readonly string[]): string | undefined {
const [mark, ...others] = nodeMarks(node)
if (mark === undefined) return dataAttributes(node, vocabulary, written)
const attrs = nodeAttrs(mark)
const align = attrs['align']
const textAlign = others.length === 0 && mark.type === 'alignment' && Object.keys(attrs).length === 1 && typeof align === 'string' ? alignments().get(align) : undefined
const bare: AdfNode = { ...node }
delete bare.marks
const data = textAlign === undefined ? undefined : dataAttributes(bare, vocabulary, written)
return data === undefined ? undefined : ` style="text-align:${textAlign}"${data}`
}
// The attributes and marks a paragraph's or heading's start tag holds, as `textBlockAttributes` writes them.
export function readTextBlockAttributes(pairs: readonly (readonly [string, string])[], element: string, vocabulary: AttributeVocabulary, written: readonly string[]): Read<{ attrs: AdfAttributes; marks: AdfMark[] }> {
const [first] = pairs
const align = first?.[0] === 'style' ? [...alignments()].find(([, textAlign]) => first[1] === `text-align:${textAlign}`)?.[0] : undefined
if (first?.[0] === 'style' && align === undefined) return { fault: { code: 'unmappable-html', message: `style on <${element}> holds text-align:center or text-align:right` } }
const data = readDataAttributes(align === undefined ? pairs : pairs.slice(1), element, vocabulary, written)
if (data.fault !== undefined) return data
return { value: { attrs: data.value, marks: align === undefined ? [] : [{ attrs: { align }, type: 'alignment' }] } }
}
+17 -3
View File
@@ -106,7 +106,7 @@ test('writes an item\'s first paragraph as its element on the item\'s line where
assert.equal(listed(held), `<ul>\n<li><p data-local-id="${ids.paragraph}">Bolt</p></li>\n</ul>\n`)
assert.equal(listed(held, { content: [item(paragraph(text('M8')))], type: 'bulletList' }), `<ul>\n<li><p data-local-id="${ids.paragraph}">Bolt</p>\n<ul>\n<li>M8</li>\n</ul>\n</li>\n</ul>\n`)
assert.equal(listed({ attrs: { localId: ids.paragraph }, type: 'paragraph' }), `<ul>\n<li><p data-local-id="${ids.paragraph}"></p></li>\n</ul>\n`)
assert.equal(listed({ content: [text('Bolt')], marks: [{ attrs: { level: 1 }, type: 'indentation' }], type: 'paragraph' }).startsWith('<ul>\n<li>\n<div class="adf-carry"'), true)
assert.equal(listed({ content: [text('Bolt')], marks: [{ attrs: { level: 1 }, type: 'alignment' }], type: 'paragraph' }).startsWith('<ul>\n<li>\n<div class="adf-carry"'), true)
})
test('writes the attributes an element cannot express as data-* attributes on it, sorted, a value that is no string as JSON', () => {
@@ -234,10 +234,24 @@ test('carries a paragraph or heading holding an alignment HTML cannot spell, or
assert.equal(carriedBlockOf({ attrs: { align: 'start' }, type: 'alignment' }), true)
assert.equal(carriedBlockOf({ attrs: { align: 'center', x: 1 }, type: 'alignment' }), true)
assert.equal(carriedBlockOf({ type: 'alignment' }), true)
assert.equal(carriedBlockOf({ attrs: { level: 1 }, type: 'indentation' }), true)
assert.equal(carriedBlockOf({ attrs: { level: 7 }, type: 'indentation' }), true)
assert.equal(carriedBlockOf({ attrs: { level: 0 }, type: 'indentation' }), true)
assert.equal(carriedBlockOf({ attrs: { level: 1.5 }, type: 'indentation' }), true)
assert.equal(carriedBlockOf({ attrs: { level: '2' }, type: 'indentation' }), true)
assert.equal(carriedBlockOf({ attrs: { level: 1, x: 1 }, type: 'indentation' }), true)
assert.equal(carriedBlockOf({ type: 'indentation' }), true)
assert.equal(carriedBlockOf({ attrs: { level: 1 }, type: 'indentation' }, { attrs: { level: 2 }, type: 'indentation' }), true)
assert.equal(carriedBlockOf({ attrs: { align: 'center' }, type: 'alignment' }, { attrs: { align: 'end' }, type: 'alignment' }), true)
assert.equal(carriedBlockOf({ attrs: { align: 'center' }, type: 'alignment' }, { attrs: { level: 1 }, type: 'indentation' }), true)
assert.equal(carriedBlock({ attrs: { level: 1 }, content: [text('x')], marks: [{ attrs: { level: 1 }, type: 'indentation' }], type: 'heading' }), true)
assert.equal(carriedBlock({ attrs: { level: 1 }, content: [text('x')], marks: [{ attrs: { level: 7 }, type: 'indentation' }], type: 'heading' }), true)
})
test('writes a paragraph or heading marked with an indentation of 1 to 6 as a data-indent-level attribute', () => {
const indented = (level: number, node: AdfNode = paragraph(text('x'))): string => html(adfToHtml(document({ ...node, marks: [{ attrs: { level }, type: 'indentation' }] })))
assert.equal(indented(2), '<p data-indent-level="2">x</p>\n')
assert.equal(indented(6, { attrs: { level: 2 }, content: [text('x')], type: 'heading' }), '<h2 data-indent-level="6">x</h2>\n')
assert.equal(indented(1, { attrs: { localId: ids.paragraph }, type: 'paragraph' }), `<p data-indent-level="1" data-local-id="${ids.paragraph}"></p>\n`)
assert.equal(html(adfToHtml(document({ content: [item({ content: [text('x')], marks: [{ attrs: { level: 3 }, type: 'indentation' }], type: 'paragraph' })], type: 'bulletList' }))), '<ul>\n<li><p data-indent-level="3">x</p></li>\n</ul>\n')
})
test('carries every other node in an adf-carry element holding its JSON', () => {
+1 -1
View File
@@ -19,7 +19,7 @@ import { longestOutput, tooLong, withinEngineLimits } from '../../length-bound.t
import { mediaImage } from '../media-element.ts'
import { plainImage } from '../../adf/plain-image.ts'
import { serializeCanonicalJson } from '../../canonical-json.ts'
import { textBlockAttributes } from '../alignment-element.ts'
import { textBlockAttributes } from '../text-block-attributes.ts'
// depth: the nesting level of the block list holding the node, as `largestNesting` counts it; `attributes`: the `<li>`'s, `<td>`'s or `<th>`'s, built when its list or table checked it; `lead`: what stands before the inline content of a task or decision item.
type Block = { depth: number; kind: 'block'; node: AdfNode; path: ConvertErrorPath }
+1 -1
View File
@@ -1,7 +1,7 @@
import type { AdfNode } from '../adf/document.ts'
import { blockNodes } from '../adf/block-nodes.ts'
import { nodeContent } from '../adf/document.ts'
import { textBlockAttributes } from './alignment-element.ts'
import { textBlockAttributes } from './text-block-attributes.ts'
// Where a list item's first paragraph stands: `bare` on the item's line, its `element` there, or a `block` on a line of its own.
export type FirstParagraphForm = 'bare' | 'block' | 'element'
+17 -2
View File
@@ -253,6 +253,17 @@ test('refuses a mark element the writer does not write', () => {
}
})
test('reads a paragraph or heading with a data-indent-level attribute as indented', () => {
const indentation = (level: number): AdfMark => ({ attrs: { level }, type: 'indentation' })
assert.deepEqual(read(htmlToAdf('<p data-indent-level="2">x</p>\n')), document({ content: [text('x')], marks: [indentation(2)], type: 'paragraph' }))
assert.deepEqual(read(htmlToAdf('<h2 data-indent-level="6">x</h2>\n')), document({ attrs: { level: 2 }, content: [text('x')], marks: [indentation(6)], type: 'heading' }))
assert.deepEqual(read(htmlToAdf(`<p data-indent-level="1" data-local-id="${ids.paragraph}"></p>\n`)), document({ attrs: { localId: ids.paragraph }, marks: [indentation(1)], type: 'paragraph' }))
assert.deepEqual(read(htmlToAdf('<ul>\n<li><p data-indent-level="3">x</p></li>\n</ul>\n')), document({ content: [item({ content: [text('x')], marks: [indentation(3)], type: 'paragraph' })], type: 'bulletList' }))
for (const html of ['<p data-indent-level="0">x</p>\n', '<p data-indent-level="7">x</p>\n', '<p data-indent-level="02">x</p>\n', '<p data-indent-level="1.5">x</p>\n', '<p data-indent-level="">x</p>\n', '<p data-indent-level="2" style="text-align:center">x</p>\n', '<p style="text-align:center" data-indent-level="2">x</p>\n', `<p data-local-id="${ids.paragraph}" data-indent-level="2">x</p>\n`, '<p data-indent-level="2" data-indent-level="2">x</p>\n', '<blockquote data-indent-level="2">\n<p>x</p>\n</blockquote>\n']) {
assert.notEqual(refusal(htmlToAdf(html)), 'read', html)
}
})
test('reads a paragraph or heading with a style attribute as aligned, right as end', () => {
const alignment = (align: string): AdfMark => ({ attrs: { align }, type: 'alignment' })
assert.deepEqual(read(htmlToAdf('<p style="text-align:center">x</p>\n')), document({ content: [text('x')], marks: [alignment('center')], type: 'paragraph' }))
@@ -694,7 +705,7 @@ const pinnedRefusals: readonly (readonly [string, string])[] = [
['<div class="adf-panel" data-panel-type="info">\n</div>\n', 'unmappable-html at /content/0/content/0: a block element of the dialect stands here: found "</div>\\n"'],
['<div class="adf-panel" data-panel-type="info"><p>a</p></div>\n', 'unmappable-html at /content/0: <div> holds </div> straight after it, or a line feed and its first block: found "<p>a</p></div>\\n"'],
['<div id="x" class="adf-panel">\n<p>a</p>\n</div>\n', 'unmappable-html at /content/0: a <div> of the dialect is a carry element, holding class="adf-carry" and then data-adf alone: found "<div id=\\"x\\" class=\\"adf-p"…'],
['<details class="adf-expand" open="">\n<p>a</p>\n</details>\n', 'unmappable-html at /content/0: <details> takes no open attribute'],
['<details class="adf-expand" open="x">\n<p>a</p>\n</details>\n', 'unmappable-html at /content/0: <details> takes no open attribute'],
['<details class="adf-expand" data-title="a">\n<p>a</p>\n</details>\n', 'unsupported-node-shape at /content/0: <details> takes no data-title attribute'],
['<details>\n<p>a</p>\n</details>\n', 'unmappable-html at /content/0: <details> starts with class="adf-expand" or class="adf-nested-expand": found "<details>\\n<p>a</p>\\n</det"…'],
['<details class="adf-expand">\n</details>\n', 'unmappable-html at /content/0/content/0: a block element of the dialect stands here: found "</details>\\n"'],
@@ -784,6 +795,8 @@ test('reads an expand and a nested expand back from their details, the title fro
assert.deepEqual(expand('<details class="adf-expand" data-local-id="x1">\n<summary>Details &amp; more</summary>\n<p>Inside</p>\n</details>\n'), document({ attrs: { localId: 'x1', title: 'Details & more' }, content: [paragraph(text('Inside'))], type: 'expand' }))
assert.deepEqual(expand('<details class="adf-nested-expand">\n<summary>T</summary>\n<p>Inside</p>\n</details>\n'), document({ attrs: { title: 'T' }, content: [paragraph(text('Inside'))], type: 'nestedExpand' }))
assert.deepEqual(expand('<details class="adf-expand">\n<p>Inside</p>\n</details>\n'), document({ content: [paragraph(text('Inside'))], type: 'expand' }))
assert.deepEqual(expand('<details class="adf-expand" data-local-id="x1" open="">\n<summary>T</summary>\n<p>Inside</p>\n</details>\n'), document({ attrs: { localId: 'x1', title: 'T' }, content: [paragraph(text('Inside'))], type: 'expand' }))
assert.deepEqual(expand('<details class="adf-nested-expand" open="">\n<p>Inside</p>\n</details>\n'), document({ content: [paragraph(text('Inside'))], type: 'nestedExpand' }))
assert.deepEqual(expand('<details class="adf-expand">\n<summary></summary>\n<p>Inside</p>\n</details>\n'), document({ attrs: { title: '' }, content: [paragraph(text('Inside'))], type: 'expand' }))
assert.deepEqual(expand('<details class="adf-expand">\n<summary>T</summary>\n</details>\n'), document({ attrs: { title: 'T' }, type: 'expand' }))
assert.deepEqual(expand('<details class="adf-expand"></details>\n'), document({ type: 'expand' }))
@@ -792,7 +805,9 @@ test('reads an expand and a nested expand back from their details, the title fro
test('refuses an expand the writer does not write', () => {
const refused = (source: string): string => refusal(htmlToAdf(source))
assert.equal(refused('<details class="adf-expand" open="">\n<p>a</p>\n</details>\n'), 'unmappable-html at /content/0 line 1 offset 0')
assert.equal(refused('<details class="adf-expand" open="x">\n<p>a</p>\n</details>\n'), 'unmappable-html at /content/0 line 1 offset 0')
assert.equal(refused('<details class="adf-expand" open="" data-local-id="x1">\n<p>a</p>\n</details>\n'), 'unmappable-html at /content/0 line 1 offset 0')
assert.equal(refused('<details class="adf-expand" open="" open="">\n<p>a</p>\n</details>\n'), 'unmappable-html at /content/0 line 1 offset 0')
assert.equal(refused('<details class="adf-expand" data-title="a">\n<p>a</p>\n</details>\n'), 'unsupported-node-shape at /content/0 line 1 offset 0')
assert.equal(refused('<details>\n<p>a</p>\n</details>\n'), 'unmappable-html at /content/0 line 1 offset 0')
assert.equal(refused('<details class="adf-expand">\n</details>\n'), 'unmappable-html at /content/0/content/0 line 2 offset 29')
+3 -2
View File
@@ -237,12 +237,13 @@ function readPanel(html: string, tag: StartTag, place: Place, open: Open): Resul
return readContents(html, tag, built('panel', data.value, []), place, open, 'blocks')
}
// An expand's title stands in a `<summary>` on the first line, its blocks a line each after it.
// An expand's title stands in a `<summary>` on the first line, its blocks a line each after it. A browser appends `open=""` to a `<details>` its user opened: the state is the viewer's, so it reads as the same expand.
function readDetails(html: string, tag: StartTag, place: Place, open: Open): Result<BlockStep, ParseError> {
const [first, ...pairs] = tag.attributes
const type = first?.[0] === 'class' ? expandOfClass(first[1]) : undefined
if (type === undefined) return refused('unmappable-html', `<details> starts with class="${expandClasses.expand}" or class="${expandClasses.nestedExpand}": ${foundAt(html, tag.start)}`, html, tag.start, place)
const data = readDataAttributes(pairs, tag.name, blockNodes[type].attributes, ['title'])
const opened = pairs.at(-1)?.[0] === 'open' && pairs.at(-1)?.[1] === ''
const data = readDataAttributes(opened ? pairs.slice(0, -1) : pairs, tag.name, blockNodes[type].attributes, ['title'])
if (data.fault !== undefined) return refused(data.fault.code, data.fault.message, html, tag.start, place)
const summary = html.startsWith('\n<summary>', tag.end) ? readSummary(html, tag.end + 1, place) : undefined
if (summary?.ok === false) return summary
+2 -2
View File
@@ -4,13 +4,13 @@ import type { Read } from '../../result.ts'
import { misread, refused, type Place, type ReadNode, type StartTag } from './source.ts'
import { readDataAttributes } from '../data-attributes.ts'
import { readInline } from './inline.ts'
import { readTextBlockAttributes } from '../alignment-element.ts'
import { readTextBlockAttributes } from '../text-block-attributes.ts'
import { success } from '../../result.ts'
// `own`: the attributes the element itself writes, such as a heading's level.
export function readInlineBlock(html: string, tag: StartTag, type: string, vocabulary: AttributeVocabulary, own: AdfAttributes, place: Place): ReadNode {
const written = Object.keys(own)
// Only a paragraph or heading takes an alignment, as a `style` attribute.
// Only a paragraph or heading takes an alignment or an indentation, as the attribute that spells it.
const aligned = type === 'paragraph' || type === 'heading'
const held = aligned ? readTextBlockAttributes(tag.attributes, tag.name, vocabulary, written) : readUnmarked(tag, vocabulary, written)
if (held.fault !== undefined) return refused(held.fault.code, held.fault.message, html, tag.start, place)
+54
View File
@@ -0,0 +1,54 @@
import type { AdfAttributes, AdfMark, AdfNode } from '../adf/document.ts'
import type { AttributeVocabulary } from '../adf/attribute-vocabulary.ts'
import type { Read } from '../result.ts'
import { dataAttributes, readDataAttributes } from './data-attributes.ts'
import { nodeAttrs, nodeMarks } from '../adf/document.ts'
// The `text-align` value each alignment names.
function alignments(): ReadonlyMap<string, string> {
return new Map([
['center', 'center'],
['end', 'right'],
])
}
const indentAttribute = 'data-indent-level'
const indentLevels = ['1', '2', '3', '4', '5', '6']
// A paragraph's or heading's attributes: its one alignment or indentation mark as the attribute that spells it, then the rest as `dataAttributes` writes them; `undefined` where it holds a mark the attribute does not spell, or a second one.
export function textBlockAttributes(node: AdfNode, vocabulary: AttributeVocabulary, written: readonly string[]): string | undefined {
const [mark, ...others] = nodeMarks(node)
if (mark === undefined) return dataAttributes(node, vocabulary, written)
const spelled = others.length === 0 ? markAttribute(mark) : undefined
const bare: AdfNode = { ...node }
delete bare.marks
const data = spelled === undefined ? undefined : dataAttributes(bare, vocabulary, written)
return data === undefined ? undefined : `${spelled}${data}`
}
function markAttribute(mark: AdfMark): string | undefined {
const attrs = nodeAttrs(mark)
if (Object.keys(attrs).length !== 1) return undefined
const { align, level } = attrs
if (mark.type === 'alignment' && typeof align === 'string') return alignments().has(align) ? ` style="text-align:${alignments().get(align)}"` : undefined
return mark.type === 'indentation' && typeof level === 'number' && indentLevels.includes(String(level)) ? ` ${indentAttribute}="${level}"` : undefined
}
// The attributes and marks a paragraph's or heading's start tag holds, as `textBlockAttributes` writes them.
export function readTextBlockAttributes(pairs: readonly (readonly [string, string])[], element: string, vocabulary: AttributeVocabulary, written: readonly string[]): Read<{ attrs: AdfAttributes; marks: AdfMark[] }> {
const [first] = pairs
const mark = first === undefined ? undefined : readMark(first, element)
if (mark?.fault !== undefined) return mark
const data = readDataAttributes(mark === undefined ? pairs : pairs.slice(1), element, vocabulary, written)
if (data.fault !== undefined) return data
return { value: { attrs: data.value, marks: mark === undefined ? [] : [mark.value] } }
}
function readMark([name, spelled]: readonly [string, string], element: string): Read<AdfMark> | undefined {
if (name === indentAttribute) {
return indentLevels.includes(spelled) ? { value: { attrs: { level: Number(spelled) }, type: 'indentation' } } : { fault: { code: 'unmappable-html', message: `${indentAttribute} on <${element}> holds a whole number from 1 to 6` } }
}
if (name !== 'style') return undefined
const align = [...alignments()].find(([, textAlign]) => spelled === `text-align:${textAlign}`)?.[0]
return align === undefined ? { fault: { code: 'unmappable-html', message: `style on <${element}> holds text-align:center or text-align:right` } } : { value: { attrs: { align }, type: 'alignment' } }
}