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
+12 -1
View File
@@ -1,6 +1,7 @@
import type { ConvertFault } from '../result.ts'
import { failure, faulted, success, type ConvertFault, type Result } from '../result.ts'
import { isJsonValue, overNested, type JsonValue } from '../json-value.ts'
import { largestNesting } from '../nesting.ts'
import { longestOutput, tooLong } from '../length-bound.ts'
import { serializeCanonicalJson } from '../canonical-json.ts'
export type AdfAttributes = { [key: string]: JsonValue }
@@ -48,6 +49,16 @@ export function adfDocumentFault(value: unknown): ConvertFault | undefined {
return nestingFault(content)
}
// The checks every writer opens with; `undefined` where the document holds no `content` key, which each format spells its own way.
export function writableContent(document: AdfDocument): Result<readonly AdfNode[] | undefined> {
const fault = adfDocumentFault(document)
if (fault !== undefined) return faulted(fault, [])
if (document.version !== 1) return failure('unsupported-document-version', `no format this library writes carries ADF version ${document.version}`, [])
if (document.content === undefined) return success(undefined)
// Every character of text is written at least once, so this much text cannot fit, and none of it is written.
return heldTextLength(document.content) > longestOutput ? tooLong() : success(document.content)
}
export function attributeNestingMessage(key: string, type: string): string {
return `the ${key} attribute of ${type} nests deeper than the ${largestNesting} levels an attribute carries`
}
+3 -3
View File
@@ -28,14 +28,14 @@ export function isMarkType(type: string): type is MarkType {
return Object.hasOwn(markAttributes, type)
}
export type LinkTarget = { href: string; title?: string }
export type LinkAttrs = { href: string; title?: string }
export function linkTargetMark({ href, title }: LinkTarget): AdfMark {
export function linkMarkFrom({ href, title }: LinkAttrs): AdfMark {
return { attrs: title === undefined ? { href } : { href, title }, type: 'link' }
}
// `undefined` where `href` is no string, or `title` is there and no string; any other key is the caller's to check.
export function linkTarget(attrs: AdfAttributes): LinkTarget | undefined {
export function linkAttrs(attrs: AdfAttributes): LinkAttrs | undefined {
const { href, title } = attrs
if (typeof href !== 'string') return undefined
if (title === undefined) return { href }
@@ -1,29 +1,28 @@
import type { AdfNode } from './document.ts'
import { linkTarget, linkTargetMark, type LinkTarget } from './mark-attributes.ts'
import { linkAttrs, linkMarkFrom, type LinkAttrs } from './mark-attributes.ts'
import { nodeAttrs, nodeContent, nodeMarks } from './document.ts'
import { serializeCanonicalJson } from '../canonical-json.ts'
// `alt` undefined where the media holds none: an empty alt builds no `alt`, so a media holding an empty one is no plain image.
export type ExternalImage = { alt: string | undefined; link: LinkTarget | undefined; url: string }
export type PlainImage = { alt: string | undefined; link: LinkAttrs | undefined; url: string }
// The plain image, the one shape each format writes as its own image element: a centered `mediaSingle` holding one external `media`, which a link may mark.
export function centeredImage(content: AdfNode[]): AdfNode {
return { attrs: { layout: 'center' }, content, type: 'mediaSingle' }
}
export function externalMedia(alt: string, url: string, link?: LinkTarget): AdfNode {
export function externalMedia(alt: string, url: string, link?: LinkAttrs): AdfNode {
const media: AdfNode = { attrs: alt === '' ? { type: 'external', url } : { alt, type: 'external', url }, type: 'media' }
return link === undefined ? media : { ...media, marks: [linkTargetMark(link)] }
return link === undefined ? media : { ...media, marks: [linkMarkFrom(link)] }
}
// `undefined` unless the node is exactly the plain image its parts build.
export function plainImage(node: AdfNode): ExternalImage | undefined {
// The plain image, the one shape each format writes as its own image element: a centered `mediaSingle` holding one external `media`, which a link may mark. `undefined` unless the node is exactly the plain image its parts build.
export function plainImage(node: AdfNode): PlainImage | undefined {
const [media] = nodeContent(node)
if (media === undefined) return undefined
const { alt, url } = nodeAttrs(media)
if (typeof url !== 'string' || (alt !== undefined && typeof alt !== 'string')) return undefined
const [mark] = nodeMarks(media)
const link = mark === undefined ? undefined : linkTarget(nodeAttrs(mark))
const link = mark === undefined ? undefined : linkAttrs(nodeAttrs(mark))
const read = centeredImage([externalMedia(alt ?? '', url, link)])
return serializeCanonicalJson(node, 'compact') === serializeCanonicalJson(read, 'compact') ? { alt, link, url } : undefined
}