mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-19 22:55:44 +01:00
Answer principles round 1: 3 🔴, 8 🟡 (0 misread); attributes as data-*, carried content as paragraphs, links and notes, no character HTML cannot hold, mutants killed
This commit is contained in:
@@ -1,18 +1,20 @@
|
||||
import type { AdfDocument, AdfNode } from '../../adf/document.ts'
|
||||
import { anchorOpener, inlineHtml, writableText } from './inline.ts'
|
||||
import { anchorOpener, optionalAttribute, writableValue } from './escaping.ts'
|
||||
import { blockNodeModel } from '../../adf/block-nodes.ts'
|
||||
import { carriedBlock, carryElement } from './carry-element.ts'
|
||||
import { failure, success, type ConvertErrorPath, type Result } from '../../result.ts'
|
||||
import { holdsOnlyAttributes, isUnmarkedSpellableText, nodeAttrs, nodeContent, writableContent } from '../../adf/document.ts'
|
||||
import { inlineHtml, writableText } from './inline.ts'
|
||||
import { largestNesting } from '../../nesting.ts'
|
||||
import { longestOutput, tooLong, withinEngineLimits } from '../../length-bound.ts'
|
||||
import { htmlValue, optionalAttribute } from './escaping.ts'
|
||||
import { plainImage } from '../../adf/plain-image.ts'
|
||||
import { serializeCanonicalJson } from '../../canonical-json.ts'
|
||||
import { vocabularyPairs } from '../../adf/attribute-vocabulary.ts'
|
||||
|
||||
// depth: the nesting level of the block list holding the node, as `largestNesting` counts it; inList: the node is a list's item.
|
||||
type Block = { depth: number; inList: boolean; node: AdfNode; path: ConvertErrorPath }
|
||||
type Container = { attributes: string; isList: boolean; tag: string }
|
||||
type Pending = Block | string
|
||||
// depth: the nesting level of the block list holding the node, as `largestNesting` counts it; an item's attributes are its element's, checked by its list.
|
||||
type Block = { depth: number; kind: 'block'; node: AdfNode; path: ConvertErrorPath }
|
||||
type Item = { attributes: string; depth: number; kind: 'item'; node: AdfNode; path: ConvertErrorPath }
|
||||
type Pending = Block | Item | string
|
||||
|
||||
export function adfToHtml(document: AdfDocument): Result<string> {
|
||||
return withinEngineLimits(() => writeHtml(document))
|
||||
@@ -23,7 +25,7 @@ function writeHtml(document: AdfDocument): Result<string> {
|
||||
const content = writableContent(document)
|
||||
if (!content.ok) return content
|
||||
if (content.value === undefined) return success(`${carryElement('div', serializeCanonicalJson(document, 'compact'), '')}\n`)
|
||||
const pending: Pending[] = content.value.length === 0 ? [] : [...blockPieces(content.value, [], 0, false), '\n'].reverse()
|
||||
const pending: Pending[] = content.value.length === 0 ? [] : [...blockPieces(content.value, [], 0), '\n'].reverse()
|
||||
let html = ''
|
||||
for (let next = pending.pop(); next !== undefined; next = pending.pop()) {
|
||||
if (typeof next === 'string') {
|
||||
@@ -32,95 +34,131 @@ function writeHtml(document: AdfDocument): Result<string> {
|
||||
html += next
|
||||
continue
|
||||
}
|
||||
const pieces = writeBlock(next)
|
||||
if (next.depth > largestNesting) return failure('unsupported-nesting-depth', `the document nests deeper than the ${largestNesting} levels the writer carries`, next.path)
|
||||
const pieces = next.kind === 'item' ? writeItem(next) : writeBlock(next)
|
||||
if (!pieces.ok) return pieces
|
||||
for (const piece of pieces.value.reverse()) pending.push(piece)
|
||||
}
|
||||
return success(html)
|
||||
}
|
||||
|
||||
function blockPieces(nodes: readonly AdfNode[], path: ConvertErrorPath, depth: number, inList: boolean): Pending[] {
|
||||
function blockPieces(nodes: readonly AdfNode[], path: ConvertErrorPath, depth: number): Pending[] {
|
||||
const pieces: Pending[] = []
|
||||
for (const [index, node] of nodes.entries()) {
|
||||
if (index > 0) pieces.push('\n')
|
||||
pieces.push({ depth, inList, node, path: [...path, 'content', index] })
|
||||
pieces.push({ depth, kind: 'block', node, path: [...path, 'content', index] })
|
||||
}
|
||||
return pieces
|
||||
}
|
||||
|
||||
function writeBlock(block: Block): Result<Pending[]> {
|
||||
const { depth, node, path } = block
|
||||
if (depth > largestNesting) return failure('unsupported-nesting-depth', `the document nests deeper than the ${largestNesting} levels the writer carries`, path)
|
||||
if (block.inList) return writeItem(block)
|
||||
const container = containerElement(node)
|
||||
if (container !== undefined) return success(containerPieces(block, container))
|
||||
const container = node.type === 'blockquote' ? quotePieces(block) : listPieces(block)
|
||||
if (container !== undefined) return success(container)
|
||||
const html = tryLeafBlock(node, path) ?? carriedBlock(node, path, depth)
|
||||
return html.ok ? success([html.value]) : html
|
||||
}
|
||||
|
||||
// `undefined` where the node is no container, or it or a list's item holds what the elements cannot, so the whole node rides the carry.
|
||||
function containerElement(node: AdfNode): Container | undefined {
|
||||
if (node.type === 'blockquote') return holdsOnlyAttributes(node, []) ? { attributes: '', isList: false, tag: 'blockquote' } : undefined
|
||||
if (node.type === 'bulletList') return holdsOnlyAttributes(node, []) && listItemsWritable(node) ? { attributes: '', isList: true, tag: 'ul' } : undefined
|
||||
if (node.type !== 'orderedList' || !holdsOnlyAttributes(node, ['order']) || !listItemsWritable(node)) return undefined
|
||||
// The attributes a node holds besides those its element writes, as `data-*` attributes sorted by key, a value that is no string as JSON; `undefined` where the node holds what no attribute writes: a mark, text, an empty key, or an attribute its type does not hold as that kind (docs/decisions.md §The HTML dialect).
|
||||
function dataAttributes(node: AdfNode, written: readonly string[]): string | undefined {
|
||||
const model = blockNodeModel(node.type)
|
||||
if (model === undefined || !holdsOnlyAttributes(node, Object.keys(model.attributes))) return undefined
|
||||
const pairs = vocabularyPairs(nodeAttrs(node), model.attributes, written)
|
||||
if (pairs === undefined) return undefined
|
||||
let attributes = ''
|
||||
for (const { key, kind, value } of pairs.sort((left, right) => (left.key < right.key ? -1 : 1))) {
|
||||
const attribute = optionalAttribute(`data-${key.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`)}`, kind === 'string' ? value : serializeCanonicalJson(value, 'compact'))
|
||||
if (attribute === undefined) return undefined
|
||||
attributes += attribute
|
||||
}
|
||||
return attributes
|
||||
}
|
||||
|
||||
function quotePieces({ depth, node, path }: Block): Pending[] | undefined {
|
||||
const attributes = dataAttributes(node, [])
|
||||
return attributes === undefined ? undefined : containerPieces('blockquote', attributes, node.content === undefined ? undefined : blockPieces(node.content, path, depth + 1))
|
||||
}
|
||||
|
||||
// `undefined` where the list, or one of its items, holds what the elements cannot, so the whole list rides the carry.
|
||||
function listPieces({ depth, node, path }: Block): Pending[] | undefined {
|
||||
const element = listElement(node)
|
||||
if (element === undefined) return undefined
|
||||
const items: Pending[] = []
|
||||
// The items' blocks sit one level below the list, as a markdown list's do.
|
||||
for (const [index, item] of nodeContent(node).entries()) {
|
||||
const attributes = item.type === 'listItem' ? dataAttributes(item, []) : undefined
|
||||
if (attributes === undefined) return undefined
|
||||
if (index > 0) items.push('\n')
|
||||
items.push({ attributes, depth: depth + 1, kind: 'item', node: item, path: [...path, 'content', index] })
|
||||
}
|
||||
return containerPieces(element.tag, element.attributes, node.content === undefined ? undefined : items)
|
||||
}
|
||||
|
||||
function listElement(node: AdfNode): { attributes: string; tag: string } | undefined {
|
||||
if (node.type === 'bulletList') {
|
||||
const attributes = dataAttributes(node, [])
|
||||
return attributes === undefined ? undefined : { attributes, tag: 'ul' }
|
||||
}
|
||||
if (node.type !== 'orderedList') return undefined
|
||||
const order = nodeAttrs(node)['order']
|
||||
if (order === undefined) return { attributes: '', isList: true, tag: 'ol' }
|
||||
return typeof order === 'number' && Number.isSafeInteger(order) && !Object.is(order, -0) ? { attributes: ` start="${order}"`, isList: true, tag: 'ol' } : undefined
|
||||
if (order !== undefined && (typeof order !== 'number' || !Number.isSafeInteger(order) || Object.is(order, -0))) return undefined
|
||||
const attributes = dataAttributes(node, ['order'])
|
||||
return attributes === undefined ? undefined : { attributes: `${order === undefined ? '' : ` start="${order}"`}${attributes}`, tag: 'ol' }
|
||||
}
|
||||
|
||||
function listItemsWritable(list: AdfNode): boolean {
|
||||
return nodeContent(list).every((item) => item.type === 'listItem' && holdsOnlyAttributes(item, []))
|
||||
// `children` undefined where the node holds no `content` key, so its element holds nothing.
|
||||
function containerPieces(tag: string, attributes: string, children: Pending[] | undefined): Pending[] {
|
||||
if (children === undefined) return [`<${tag}${attributes}></${tag}>`]
|
||||
return [`<${tag}${attributes}>\n`, ...children, `\n</${tag}>`]
|
||||
}
|
||||
|
||||
function containerPieces({ depth, node, path }: Block, { attributes, isList, tag }: Container): Pending[] {
|
||||
if (node.content === undefined) return [`<${tag}${attributes}></${tag}>`]
|
||||
return [`<${tag}${attributes}>\n`, ...blockPieces(node.content, path, depth + 1, isList), `\n</${tag}>`]
|
||||
}
|
||||
|
||||
// An item's blocks sit at its own level, one below its list, as a markdown list's do; its first paragraph stands bare where it holds content, panel pick, docs/decisions.md §The HTML dialect.
|
||||
function writeItem({ depth, node, path }: Block): Result<Pending[]> {
|
||||
if (node.content === undefined) return success(['<li></li>'])
|
||||
const blocks = blockPieces(node.content, path, depth, false)
|
||||
// An item's blocks sit at its own level, one below its list; its first paragraph stands bare where it holds content and no attribute, panel pick, docs/decisions.md §The HTML dialect.
|
||||
function writeItem({ attributes, depth, node, path }: Item): Result<Pending[]> {
|
||||
if (node.content === undefined) return success([`<li${attributes}></li>`])
|
||||
const blocks = blockPieces(node.content, path, depth)
|
||||
const [first] = node.content
|
||||
if (first === undefined || first.type !== 'paragraph' || !holdsOnlyAttributes(first, []) || nodeContent(first).length === 0) return success(['<li>\n', ...blocks, '\n</li>'])
|
||||
if (first === undefined || first.type !== 'paragraph' || dataAttributes(first, []) !== '' || nodeContent(first).length === 0) return success([`<li${attributes}>\n`, ...blocks, '\n</li>'])
|
||||
const bare = inlineHtml(nodeContent(first), [...path, 'content', 0])
|
||||
if (!bare.ok) return bare
|
||||
// The bare paragraph's inline content stands in for its block.
|
||||
return success(blocks.length === 1 ? [`<li>${bare.value}</li>`] : [`<li>${bare.value}`, ...blocks.slice(1), '\n</li>'])
|
||||
return success(blocks.length === 1 ? [`<li${attributes}>${bare.value}</li>`] : [`<li${attributes}>${bare.value}`, ...blocks.slice(1), '\n</li>'])
|
||||
}
|
||||
|
||||
function tryLeafBlock(node: AdfNode, path: ConvertErrorPath): Result<string> | undefined {
|
||||
if (node.type === 'paragraph' && holdsOnlyAttributes(node, [])) return inlineElement('p', nodeContent(node), path)
|
||||
if (node.type === 'heading' && holdsOnlyAttributes(node, ['level'])) {
|
||||
if (node.type === 'paragraph') return inlineElement('p', dataAttributes(node, []), nodeContent(node), path)
|
||||
if (node.type === 'heading') {
|
||||
const level = nodeAttrs(node)['level']
|
||||
if (typeof level === 'number' && Number.isInteger(level) && level >= 1 && level <= 6) return inlineElement(`h${level}`, nodeContent(node), path)
|
||||
if (typeof level === 'number' && Number.isInteger(level) && level >= 1 && level <= 6) return inlineElement(`h${level}`, dataAttributes(node, ['level']), nodeContent(node), path)
|
||||
}
|
||||
const html = tryCodeBlock(node) ?? tryImage(node) ?? (node.type === 'rule' && holdsOnlyAttributes(node, []) && nodeContent(node).length === 0 ? '<hr>' : undefined)
|
||||
const html = tryCodeBlock(node) ?? tryImage(node) ?? tryRule(node)
|
||||
return html === undefined ? undefined : success(html)
|
||||
}
|
||||
|
||||
function inlineElement(tag: string, nodes: readonly AdfNode[], path: ConvertErrorPath): Result<string> {
|
||||
// `undefined` where the attributes are, so the node rides the carry.
|
||||
function inlineElement(tag: string, attributes: string | undefined, nodes: readonly AdfNode[], path: ConvertErrorPath): Result<string> | undefined {
|
||||
if (attributes === undefined) return undefined
|
||||
const inline = inlineHtml(nodes, path)
|
||||
return inline.ok ? success(`<${tag}>${inline.value}</${tag}>`) : inline
|
||||
return inline.ok ? success(`<${tag}${attributes}>${inline.value}</${tag}>`) : inline
|
||||
}
|
||||
|
||||
// A class holds the language as one token, so a language holding whitespace rides the carry.
|
||||
function tryCodeBlock(node: AdfNode): string | undefined {
|
||||
if (node.type !== 'codeBlock' || !holdsOnlyAttributes(node, ['language'])) return undefined
|
||||
if (node.type !== 'codeBlock') return undefined
|
||||
const attributes = dataAttributes(node, ['language'])
|
||||
const language = nodeAttrs(node)['language']
|
||||
if (language !== undefined && (typeof language !== 'string' || !/^[^\t\n\f\r ]+$/.test(language))) return undefined
|
||||
if (attributes === undefined || (language !== undefined && (typeof language !== 'string' || !/^[^\t\n\f\r ]+$/.test(language)))) return undefined
|
||||
const className = optionalAttribute('class', language === undefined ? undefined : `language-${language}`)
|
||||
if (className === undefined) return undefined
|
||||
if (node.content === undefined) return `<pre><code${className}></code></pre>`
|
||||
if (node.content === undefined) return `<pre${attributes}><code${className}></code></pre>`
|
||||
const [only, ...others] = node.content
|
||||
const code = only === undefined || others.length > 0 || !isUnmarkedSpellableText(only) ? undefined : writableText(only.text)
|
||||
return code === undefined ? undefined : `<pre><code${className}>${code}</code></pre>`
|
||||
return code === undefined ? undefined : `<pre${attributes}><code${className}>${code}</code></pre>`
|
||||
}
|
||||
|
||||
function tryImage(node: AdfNode): string | undefined {
|
||||
const image = node.type === 'mediaSingle' ? plainImage(node) : undefined
|
||||
if (image === undefined) return undefined
|
||||
const src = htmlValue(image.url)
|
||||
const src = writableValue(image.url)
|
||||
const alt = optionalAttribute('alt', image.alt)
|
||||
if (src === undefined || alt === undefined) return undefined
|
||||
const element = `<img src="${src}"${alt}>`
|
||||
@@ -128,3 +166,8 @@ function tryImage(node: AdfNode): string | undefined {
|
||||
const anchor = anchorOpener(image.link)
|
||||
return anchor === undefined ? undefined : `<p>${anchor}${element}</a></p>`
|
||||
}
|
||||
|
||||
function tryRule(node: AdfNode): string | undefined {
|
||||
const attributes = node.type === 'rule' && nodeContent(node).length === 0 ? dataAttributes(node, []) : undefined
|
||||
return attributes === undefined ? undefined : `<hr${attributes}>`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user