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 [](href) in both writers
This commit is contained in:
@@ -626,6 +626,28 @@ test('spells the image form for exactly the centered external media shape', () =
|
||||
assert.equal(code(adfToLosslessMarkdown(document({ attrs: { layout: 'center' }, content: [{ ...media, text: 'x' }], type: 'mediaSingle' }))), 'unsupported-node-shape')
|
||||
})
|
||||
|
||||
test('spells a media only a link marks as the image the link wraps', () => {
|
||||
const url = 'https://example.com/moon.png'
|
||||
const linked = (alt: string | undefined, ...marks: AdfMark[]): AdfDocument =>
|
||||
document({ attrs: { layout: 'center' }, content: [{ attrs: alt === undefined ? { type: 'external', url } : { alt, type: 'external', url }, marks, type: 'media' }], type: 'mediaSingle' })
|
||||
const link = (attrs: AdfAttributes): AdfMark => ({ attrs, type: 'link' })
|
||||
const spelled = (written: AdfDocument, expected: string): void => {
|
||||
assert.equal(markdown(adfToLosslessMarkdown(written)), expected)
|
||||
assert.deepEqual(losslessMarkdownToAdf(expected), { ok: true, value: written })
|
||||
}
|
||||
spelled(linked('The moon', link({ href: 'https://example.com/docs' })), `[](https://example.com/docs)\n`)
|
||||
spelled(linked(undefined, link({ href: 'https://example.com/docs', title: 'Docs "v2"' })), `[](https://example.com/docs "Docs \\"v2\\"")\n`)
|
||||
spelled(linked('a [b] c', link({ href: '/a b' })), `[![a \\[b\\] c](${url})](</a b>)\n`)
|
||||
spelled(linked('m', link({ href: '' })), `[]()\n`)
|
||||
const fallback = (document: AdfDocument): boolean => markdown(adfToLosslessMarkdown(document)).startsWith('!adf:mediaSingle {layout=center}')
|
||||
assert.ok(fallback(linked('m', link({ href: '/d', id: 'x' }))))
|
||||
assert.ok(fallback(linked('m', link({ href: '/d' }), { attrs: { color: '#000', size: 1 }, type: 'border' })))
|
||||
assert.ok(fallback(linked('m', link({ href: 'a\\b' }))))
|
||||
assert.ok(fallback(linked('m', link({ title: 't' }))))
|
||||
const captioned: AdfNode = { attrs: { layout: 'center' }, content: [{ attrs: { alt: 'm', type: 'external', url }, type: 'media' }, { content: [{ text: 'c', type: 'text' }], type: 'caption' }], type: 'mediaSingle' }
|
||||
assert.ok(fallback(document(captioned)))
|
||||
})
|
||||
|
||||
test('spells a mediaSingle the image form does not fit as a directive', () => {
|
||||
const media: AdfNode = { attrs: { type: 'external', url: 'https://example.com/moon.png' }, type: 'media' }
|
||||
const single: AdfNode = { attrs: { layout: 'center' }, content: [media], marks: [{ type: 'border' }], type: 'mediaSingle' }
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import type { AdfMark, AdfNode } from '../../adf/document.ts'
|
||||
import type { InlineNodeModel } from '../../adf/inline-nodes.ts'
|
||||
import type { LineContainer } from '../line-container.ts'
|
||||
import type { LinkDefinition } from '../commonmark/link-syntax.ts'
|
||||
import type { WrittenFlavour } from '../portable/conventions.ts'
|
||||
import { assembleInlineLine, isSyntax, type InlineEscaping, type InlineSegment, type MarkRun, type NodeRange } from './line-escaping.ts'
|
||||
import { carriedInline } from '../opaque-carry.ts'
|
||||
import { commonMarkLink, linkHref, markSpelling, spellMarkAttributes } from '../mark-spellings.ts'
|
||||
import { escapeUnbalanced, spellDestination } from '../commonmark/link-syntax.ts'
|
||||
import { escapeUnbalanced, spellDestination, spellLinkTarget } from '../commonmark/link-syntax.ts'
|
||||
import { failure, success, type ConvertErrorPath, type Result } from '../../result.ts'
|
||||
import { flavourClaims, highlightDelimiter } from '../portable/conventions.ts'
|
||||
import { holdsNullCharacter, trimTrailingSpace } from '../commonmark/grammar.ts'
|
||||
@@ -67,12 +68,17 @@ export function tryPipeCell(nodes: readonly AdfNode[], path: ConvertErrorPath, f
|
||||
return emitted.value.line
|
||||
}
|
||||
|
||||
export function tryImageLine(alt: string | undefined, href: string): string | undefined {
|
||||
// `link` wraps the image as the link's whole text: `[](href)`.
|
||||
export function tryImageLine(alt: string | undefined, href: string, link?: LinkDefinition): string | undefined {
|
||||
if (alt !== undefined && (/^[ \t]|[ \t]$|[\n\r]/.test(alt) || holdsNullCharacter(alt))) return undefined
|
||||
const destination = spellDestination(href)
|
||||
if (destination === undefined) return undefined
|
||||
const description: InlineSegment[] = alt === undefined ? [] : [{ escaping: 'bracketed', text: alt }]
|
||||
return attemptLine([syntax('`)], 'paragraph', 'lossless').line
|
||||
if (link === undefined) return attemptLine([syntax('`)], 'paragraph', 'lossless').line
|
||||
const target = spellLinkTarget(link.destination, link.title)
|
||||
if (target === undefined) return undefined
|
||||
const image = [syntax('[, { escaping: 'bracketed-link-target', text: escapeUnbalanced(destination, '[', ']') } satisfies InlineSegment, syntax(')')]
|
||||
return attemptLine([...image, syntax(`](${target})`)], 'paragraph', 'lossless').line
|
||||
}
|
||||
|
||||
function emitLine(nodes: readonly AdfNode[], container: LineContainer, path: ConvertErrorPath, flavour: WrittenFlavour): Result<EmittedLine> {
|
||||
|
||||
Reference in New Issue
Block a user