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

Answer stability round 1: 0 🔴, 3 🟡; pin the image titles besideLink compares, drop two dead clauses, file item 129

This commit is contained in:
2026-10-23 03:32:36 +02:00
committed by Lillem4n
parent 28878122ee
commit 17722a0f57
3 changed files with 34 additions and 13 deletions
+10 -11
View File
@@ -6,7 +6,7 @@ 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, imageAside } from '../external-image.ts'
import { commonMarkLink, linkHref, linkMark } from '../mark-spellings.ts'
import { commonMarkLink, 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'
@@ -227,9 +227,8 @@ function directiveMarkPieces(scan: Scan, name: string, mark: AdfMark, slot: Slot
}
const refused = mark.type === 'link' ? refuseLinkDirective(scan, mark, nodes, index) : undefined
if (refused !== undefined) return refused
const attrs = nodeAttrs(mark)
const title = attrs['title']
const imageLinks = mark.type === 'link' ? besideLink(slot.imageLinks, linkHref(attrs) ?? '', typeof title === 'string' ? title : undefined) : slot.imageLinks
const link = mark.type === 'link' ? linkDefinition(nodeAttrs(mark)) : undefined
const imageLinks = link === undefined ? slot.imageLinks : besideLink(slot.imageLinks, link)
return success([...slot.pieces.map((piece) => ({ ...piece, nodes: applyMark(piece.nodes, mark) })), ...imageLinks])
}
@@ -298,17 +297,17 @@ function linkedAlt(piece: Piece, inLink: boolean): Piece[] {
if (inLink && piece.kind === 'imageLink') return []
if (piece.kind !== 'image' || (inLink && piece.link === undefined)) return [piece]
const alt: Piece = { kind: 'nodes', nodes: linkTo(piece.alt === '' ? [] : [{ text: piece.alt, type: 'text' }], piece.link ?? piece.definition) }
return piece.link === undefined ? [alt] : [alt, ...besideLink([imageLink(piece.definition)], piece.link.destination, piece.link.title)]
return piece.link === undefined ? [alt] : [alt, ...besideLink([imageLink(piece.definition)], piece.link)]
}
// As adfToPortableMarkdown writes it: a link to the image itself needs no (image), unless the image holds a title the link does not.
function besideLink(imageLinks: readonly ImageLink[], href: string, title: string | undefined): ImageLink[] {
return imageLinks.filter(({ image }) => image.destination !== href || (image.title !== undefined && image.title !== '' && image.title !== title))
function besideLink(imageLinks: readonly ImageLink[], link: LinkDefinition): ImageLink[] {
return imageLinks.filter(({ image }) => image.destination !== link.destination || (image.title !== undefined && image.title !== '' && image.title !== link.title))
}
function linkedImages(pieces: readonly Piece[]): ImageLink[] {
return pieces.flatMap((piece) => {
if (piece.kind === 'image') return piece.link === undefined ? [imageLink(piece.definition)] : []
if (piece.kind === 'image') return [imageLink(piece.definition)]
return piece.kind === 'imageLink' ? [piece] : []
})
}
@@ -372,9 +371,9 @@ function holdsOwnMarks(item: Inline | Piece): boolean {
return 'kind' in item && item.kind === 'ownMarks'
}
// Only a link a `nodes` piece spells, an image's, or an (image)'s makes the outer brackets literal; an `ownMarks` node's link is its own, so the own-marks guard in `closeLink` refuses it.
// Only a link a `nodes` piece spells, or an (image)'s, makes the outer brackets literal; an `ownMarks` node's link is its own, so the own-marks guard in `closeLink` refuses it.
function holdsLink(pieces: readonly Piece[]): boolean {
return pieces.some((piece) => piece.kind === 'imageLink' || (piece.kind === 'nodes' && marksLink(piece.nodes)) || (piece.kind === 'image' && piece.link !== undefined))
return pieces.some((piece) => piece.kind === 'imageLink' || (piece.kind === 'nodes' && marksLink(piece.nodes)))
}
function marksLink(nodes: readonly Inline[]): boolean {
@@ -461,7 +460,7 @@ function linkPieces(scan: Scan, inner: readonly Piece[], definition: LinkDefinit
if (!nodes.value.some((node) => marksItem(node, 'link')) && nodes.value.some((node) => isNode(node) && node.type !== 'hardBreak')) {
return failure('unsupported-node-shape', unlinkable, scan.path)
}
return success([{ kind: 'nodes', nodes: linkTo(nodes.value, definition) }, ...besideLink(linkedImages(inner), definition.destination, definition.title)])
return success([{ kind: 'nodes', nodes: linkTo(nodes.value, definition) }, ...besideLink(linkedImages(inner), definition)])
}
// CommonMark: no link nests inside another, though an image's description holds one.
@@ -1189,7 +1189,12 @@ test('writes no (image) after a link to the image itself, unless the image holds
assert.deepEqual(firstContent('See [![a](/u)](/u "t").\n'), [text('See '), marked('a', link('/u', 't')), text('.')])
assert.deepEqual(firstContent('See [![a](/u "t")](/u).\n'), [text('See '), marked('a', link('/u')), text(' ('), marked('image', link('/u', 't')), text(').')])
assert.deepEqual(firstContent('[b ![a](/u) ![c](/w)](/u)\n'), [marked('b a c', link('/u')), text(' ('), marked('image', link('/w')), text(')')])
assert.deepEqual(firstContent('See [![a](/u "")](/u).\n'), [text('See '), marked('a', link('/u')), text('.')])
const titled = { attrs: { href: 'a\nb', title: 't' }, type: 'link' }
assert.deepEqual(firstContent('!adf:link[b ![a](a
b)]{href="a\\nb"}\n'), [marked('b a', { attrs: { href: 'a\nb' }, type: 'link' })])
assert.deepEqual(firstContent('!adf:link[b ![a](a
b "t")]{href="a\\nb" title=t}\n'), [marked('b a', titled)])
assert.deepEqual(firstContent('!adf:link[!adf:underline[![a]()]]{href="a\\nb"}\n'), [marked('a', { attrs: { href: 'a\nb' }, type: 'link' }, { type: 'underline' }), text(' ('), marked('image', link('')), text(')')])
assert.deepEqual(firstContent('!adf:link[b ![a](a
b "u")]{href="a\\nb" title=t}\n'), [marked('b a', titled), text(' ('), marked('image', link('a\nb', 'u')), text(')')])
})
test('carries the mark a spelling nested inside its own kind names once', () => {