3
0
mirror of https://github.com/larvit/adf-codec.git synced 2026-11-18 22:25:44 +01:00

Answer architecture round 2: the decision's title states it whole, an image's title drops inside a link, the readers refuse nesting past 500 levels, one owner for the empty-alt rule

This commit is contained in:
2026-10-09 03:39:45 +02:00
committed by Lillem4n
parent 88aef1c8c4
commit 4454aaca92
6 changed files with 18 additions and 16 deletions
+1 -2
View File
@@ -4,11 +4,10 @@ import { nodeAttrs, nodeContent } from '../../adf/document.ts'
import { serializeCanonicalJson } from '../../canonical-json.ts'
import { tryImageLine } from './inline-line.ts'
// An empty `alt` takes the directive form: `![](url)` reads back with no `alt`.
export function tryImage(node: AdfNode): string | undefined {
const [media] = nodeContent(node)
const { alt, url } = media === undefined ? {} : nodeAttrs(media)
if (typeof url !== 'string' || (alt !== undefined && typeof alt !== 'string') || alt === '') return undefined
if (typeof url !== 'string' || (alt !== undefined && typeof alt !== 'string')) return undefined
const read = centeredImage([externalMedia(alt ?? '', url)])
return serializeCanonicalJson(node, 'compact') === serializeCanonicalJson(read, 'compact') ? tryImageLine(alt, url) : undefined
}
+2 -1
View File
@@ -1,10 +1,11 @@
import type { AdfNode } from '../adf/document.ts'
// spec/flavour.md, The CommonMark image: the shape `![alt](url)` reads to, and the only one written back as it.
// spec/flavour.md, The CommonMark image: `![alt](url)` reads to `centeredImage([externalMedia(alt, url)])`, the only shape written back as it.
export function centeredImage(content: AdfNode[]): AdfNode {
return { attrs: { layout: 'center' }, content, type: 'mediaSingle' }
}
// `![](url)` builds no `alt`, so a media holding an empty one takes the directive form.
export function externalMedia(alt: string, url: string): AdfNode {
return { attrs: alt === '' ? { type: 'external', url } : { alt, type: 'external', url }, type: 'media' }
}