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

Write an external image a link marks as [![alt](url)](href) in both writers

This commit is contained in:
2026-10-11 12:19:15 +02:00
committed by Lillem4n
parent cc40fb95f2
commit 580bc862f2
15 changed files with 86 additions and 39 deletions
+15 -4
View File
@@ -1,13 +1,24 @@
import type { AdfNode } from '../../adf/document.ts'
import type { LinkDefinition } from '../commonmark/link-syntax.ts'
import { centeredImage, externalMedia } from '../external-image.ts'
import { nodeAttrs, nodeContent } from '../../adf/document.ts'
import { nodeAttrs, nodeContent, nodeMarks } from '../../adf/document.ts'
import { serializeCanonicalJson } from '../../canonical-json.ts'
import { tryImageLine } from './inline-line.ts'
export function tryImage(node: AdfNode): string | undefined {
const [media] = nodeContent(node)
const { alt, url } = media === undefined ? {} : nodeAttrs(media)
if (media === undefined) return undefined
const { alt, url } = nodeAttrs(media)
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
const [mark] = nodeMarks(media)
const link = mark === undefined ? undefined : linkDefinition(nodeAttrs(mark))
if (mark !== undefined && link === undefined) return undefined
const read = centeredImage([externalMedia(alt ?? '', url, link)])
return serializeCanonicalJson(node, 'compact') === serializeCanonicalJson(read, 'compact') ? tryImageLine(alt, url, link) : undefined
}
function linkDefinition({ href, title }: Readonly<Record<string, unknown>>): LinkDefinition | undefined {
if (typeof href !== 'string') return undefined
if (title === undefined) return { destination: href }
return typeof title === 'string' ? { destination: href, title } : undefined
}