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

Answer architecture round 1: 2 🔴, 6 🟡 (0 misread); one escaping path refusing null characters, block writers returning their pieces, the HTML writer split, shared entry checks, lint between formats

This commit is contained in:
2026-10-23 14:51:15 +02:00
committed by Lillem4n
parent cc029b0bc9
commit e234905c27
19 changed files with 324 additions and 269 deletions
+7 -10
View File
@@ -1,22 +1,22 @@
import type { AdfDocument, AdfNode } from '../../adf/document.ts'
import type { BlockNodeModel } from '../../adf/block-nodes.ts'
import type { WrittenFlavour } from '../flavour.ts'
import { adfDocumentFault, heldTextLength, holdsOnlyAttributes, isUnmarkedSpellableText, nodeAttrs, nodeContent } from '../../adf/document.ts'
import { blockDirectiveForm, documentSpelling } from '../directive/block.ts'
import { blockNodeModel, blockNodes } from '../../adf/block-nodes.ts'
import { carriedBlock } from '../opaque-carry.ts'
import { emitInlineLine } from './inline-line.ts'
import { failure, faulted, success, type ConvertErrorPath, type Result } from '../../result.ts'
import { failure, success, type ConvertErrorPath, type Result } from '../../result.ts'
import { fencedCodeBlock } from '../commonmark/backtick-runs.ts'
import { flavourClaims } from '../flavour.ts'
import { holdsNullCharacter, isBlankLine, isThematicBreak, markerInterruptsParagraph } from '../commonmark/grammar.ts'
import { holdsOnlyAttributes, isUnmarkedSpellableText, nodeAttrs, nodeContent, writableContent } from '../../adf/document.ts'
import { languageSlot, type LanguageSlot } from '../code-language.ts'
import { largestNesting } from '../../nesting.ts'
import { longestOutput, tooLong, withinEngineLimits, withinLongest } from '../../length-bound.ts'
import { spellBlockDirectiveOpener } from '../directive/block-spelling.ts'
import { spellDirectiveCloser, spellDirectiveOpener } from '../directive/syntax.ts'
import { tryImage } from './image.ts'
import { tryPipeTable } from './pipe-table.ts'
import { withinEngineLimits, withinLongest } from '../../length-bound.ts'
type BlockContainer = 'directive' | 'document' | 'list-item'
type Delimiters = readonly [string, string]
@@ -50,13 +50,10 @@ export function adfToLosslessMarkdown(document: AdfDocument): Result<string> {
}
export function writeMarkdown(document: AdfDocument, writtenAs: WrittenAs): Result<string> {
const fault = adfDocumentFault(document)
if (fault !== undefined) return faulted(fault, [])
if (document.version !== 1) return failure('unsupported-document-version', `no markdown spelling carries ADF version ${document.version}`, [])
if (document.content === undefined) return success(`${documentSpelling}\n`)
// Every character of text is spelled at least once, so this much text cannot fit, and none of it is spelled.
if (heldTextLength(document.content) > longestOutput) return tooLong()
const walk = walkBlocks(document.content, [], 0, { ...writtenAs, memo: undefined })
const content = writableContent(document)
if (!content.ok) return content
if (content.value === undefined) return success(`${documentSpelling}\n`)
const walk = walkBlocks(content.value, [], 0, { ...writtenAs, memo: undefined })
if (!walk.ok) return walk
const text = joinBlocks(walk.value.blocks, 'document')
return withinLongest(text === '' ? '' : `${text}\n`)
+3 -3
View File
@@ -1,12 +1,12 @@
import type { AdfNode } from '../../adf/document.ts'
import type { WrittenFlavour } from '../flavour.ts'
import { plainImage } from '../../adf/external-image.ts'
import { targetDefinition } from '../mark-spellings.ts'
import { attrsDefinition } from '../mark-spellings.ts'
import { plainImage } from '../../adf/plain-image.ts'
import { tryImageLine } from './inline-line.ts'
export function tryImage(node: AdfNode, flavour: WrittenFlavour): string | undefined {
const image = plainImage(node)
if (image === undefined) return undefined
const { alt, link, url } = image
return tryImageLine(alt, url, flavour, link === undefined ? undefined : targetDefinition(link))
return tryImageLine(alt, url, flavour, link === undefined ? undefined : attrsDefinition(link))
}
+6 -6
View File
@@ -7,7 +7,7 @@ import { escapeUnbalanced, spellLinkTarget } from './commonmark/link-syntax.ts'
import { holdsDirectivePrefix, spellAttributes, spellVocabulary } from './directive/syntax.ts'
import { holdsEntityReference } from './commonmark/entity-references.ts'
import { isAutolink } from './commonmark/grammar.ts'
import { isMarkType, linkTarget, linkTargetMark, markAttributes, type LinkTarget } from '../adf/mark-attributes.ts'
import { isMarkType, linkAttrs, linkMarkFrom, markAttributes, type LinkAttrs } from '../adf/mark-attributes.ts'
import { spellEmptyKeys } from './directive/empty-keys.ts'
import { vocabularyPairs } from '../adf/attribute-vocabulary.ts'
@@ -38,19 +38,19 @@ export function markSpelling(type: string): MarkSpelling | undefined {
}
export function linkMark(definition: LinkDefinition): AdfMark {
return linkTargetMark(definitionTarget(definition))
return linkMarkFrom(definitionAttrs(definition))
}
export function definitionTarget({ destination, title }: LinkDefinition): LinkTarget {
export function definitionAttrs({ destination, title }: LinkDefinition): LinkAttrs {
return title === undefined ? { href: destination } : { href: destination, title }
}
export function linkDefinition(attrs: AdfAttributes): LinkDefinition | undefined {
const target = linkTarget(attrs)
return target === undefined ? undefined : targetDefinition(target)
const target = linkAttrs(attrs)
return target === undefined ? undefined : attrsDefinition(target)
}
export function targetDefinition({ href, title }: LinkTarget): LinkDefinition {
export function attrsDefinition({ href, title }: LinkAttrs): LinkDefinition {
return title === undefined ? { destination: href } : { destination: href, title }
}
+3 -3
View File
@@ -5,9 +5,9 @@ import type { EmphasisPairing } from '../commonmark/emphasis-matching.ts'
import type { InlineSource, InlineToken } from '../inline-tokens.ts'
import type { LineContainer } from '../line-container.ts'
import type { LinkDefinition } from '../commonmark/link-syntax.ts'
import { centeredImage, externalMedia } from '../../adf/external-image.ts'
import { centeredImage, externalMedia } from '../../adf/plain-image.ts'
import { imageAside } from '../image-aside.ts'
import { commonMarkLink, definitionTarget, linkDefinition, linkHref, linkMark } from '../mark-spellings.ts'
import { commonMarkLink, definitionAttrs, linkDefinition, linkHref, linkMark } from '../mark-spellings.ts'
import { decodeTextEscapes, trimTrailingSpace } from '../commonmark/grammar.ts'
import { failure, faulted, success, type ConvertErrorPath, type Result } from '../../result.ts'
import { flavourClaims } from '../flavour.ts'
@@ -319,7 +319,7 @@ function imageLink(definition: LinkDefinition): ImageLink {
function mediaSingle({ alt, definition, link }: Image): AdfNode {
const { title } = definition
const caption: AdfNode[] = title === undefined || title === '' ? [] : [{ content: [{ text: title, type: 'text' }], type: 'caption' }]
return centeredImage([externalMedia(alt, definition.destination, link === undefined ? undefined : definitionTarget(link)), ...caption])
return centeredImage([externalMedia(alt, definition.destination, link === undefined ? undefined : definitionAttrs(link)), ...caption])
}
// ADF holds no empty text node and a hard break takes no link, so the destination text carries the link ahead of the text's hard breaks; an empty destination leaves the breaks alone.
@@ -1,20 +1,20 @@
import type { AdfDocument, AdfNode } from '../../adf/document.ts'
import { adfDocumentFault, heldTextLength, nodeAttrs, nodeContent, nodeMarks } from '../../adf/document.ts'
import { blockNodeModel, isTaskItem } from '../../adf/block-nodes.ts'
import { centeredImage, externalMedia } from '../../adf/external-image.ts'
import { centeredImage, externalMedia } from '../../adf/plain-image.ts'
import { commonMarkSpelling, largestListMarker, writeMarkdown, type SpellingMemo } from '../emit/adf-to-markdown.ts'
import { failure, faulted, success, type ConvertErrorPath, type Result } from '../../result.ts'
import { failure, success, type ConvertErrorPath, type Result } from '../../result.ts'
import { flavourClaims } from '../flavour.ts'
import { inlineLeaves, isBlockNodeType, oneLine, portableLink, reduceInline, writableHref } from './inline-reduction.ts'
import { inlineNodeModel } from '../../adf/inline-nodes.ts'
import { languageSlot } from '../code-language.ts'
import { largestNesting } from '../../nesting.ts'
import { linkTarget } from '../../adf/mark-attributes.ts'
import { longestOutput, tooLong, withinEngineLimits } from '../../length-bound.ts'
import { linkAttrs } from '../../adf/mark-attributes.ts'
import { nodeAttrs, nodeContent, nodeMarks, writableContent } from '../../adf/document.ts'
import { portableWriting } from './block-spelling.ts'
import { splitTaskList } from './task-list.ts'
import { taskMarker } from './conventions.ts'
import { toEditorNormal } from './editor-normal.ts'
import { withinEngineLimits } from '../../length-bound.ts'
// depth: the level the node reduced stands at, counted as the emitter counts it.
type Reduction = { depth: number; memo: SpellingMemo; path: ConvertErrorPath }
@@ -59,11 +59,9 @@ export function adfToPortableMarkdown(document: AdfDocument): Result<string> {
}
export function reduceToPortable(document: AdfDocument): Result<AdfDocument> {
const fault = adfDocumentFault(document)
if (fault !== undefined) return faulted(fault, [])
if (document.version !== 1) return failure('unsupported-document-version', `no markdown spelling carries ADF version ${document.version}`, [])
// Measured on the caller's text, edge whitespace and all: `toEditorNormal`'s merge could build past the bound.
if (heldTextLength(nodeContent(document)) > longestOutput) return tooLong()
const checked = writableContent(document)
if (!checked.ok) return checked
// Two documents the editor holds equal write the same portable markdown (docs/decisions.md §Equality is deep).
const blocks = reduceBlocks(nodeContent(toEditorNormal(document)), { depth: 0, memo: new Map(), path: [] })
if (!blocks.ok) {
@@ -367,5 +365,5 @@ function reduceMedia(media: AdfNode, reduction: Reduction): Result<AdfNode[]> {
const alt = typeof held === 'string' ? oneLine(held).trim() : ''
const mark = nodeMarks(media).find(({ type }) => type === 'link')
const link = mark === undefined ? undefined : portableLink(mark, 'paragraph')
return success([centeredImage([externalMedia(alt, writableHref(url), link === undefined ? undefined : linkTarget(nodeAttrs(link)))])])
return success([centeredImage([externalMedia(alt, writableHref(url), link === undefined ? undefined : linkAttrs(nodeAttrs(link)))])])
}