mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-20 07:05:44 +01:00
Answer architecture round 5: 0 🔴, 0 🟡 (0 misread); shownImage returns no textless state, item 143 and the card rule name their whole reach
This commit is contained in:
@@ -48,9 +48,9 @@ test('shows an image a link to another target marks by its alt, else the target,
|
||||
assert.deepEqual(shownImage({ attrs: { type: 'external', url: moon }, type: 'media' }, ' '), { after: moon, link: undefined, text: moon })
|
||||
})
|
||||
|
||||
test('shows an image with no URL by its alt, else by no text, a note standing in its place', () => {
|
||||
test('shows an image with no URL by its alt, else nothing, a note standing in its place', () => {
|
||||
assert.deepEqual(shownImage({ attrs: { alt: 'The moon', type: 'file' }, type: 'media' }, target), { after: undefined, link: undefined, text: 'The moon' })
|
||||
assert.deepEqual(shownImage({ attrs: { type: 'file' }, type: 'mediaInline' }, undefined), { after: undefined, link: undefined, text: undefined })
|
||||
assert.equal(shownImage({ attrs: { type: 'file' }, type: 'mediaInline' }, undefined), undefined)
|
||||
assert.deepEqual(shownImage({ attrs: { alt: 'The moon', type: 'external', url: ' ' }, type: 'media' }, undefined), { after: undefined, link: undefined, text: 'The moon' })
|
||||
})
|
||||
|
||||
|
||||
@@ -39,11 +39,11 @@ export function referenceName(node: AdfNode): string | undefined {
|
||||
}
|
||||
}
|
||||
|
||||
// What an image shows in place of itself, `href` the target of the link marking it, if any: `text`, else a note; `link`, its URL, which `text` links to where no link marks it; `after`, its URL, shown after a link going elsewhere.
|
||||
export function shownImage(media: AdfNode, href: string | undefined): { after: string | undefined; link: string | undefined; text: string | undefined } {
|
||||
// What an image shows in place of itself, `href` the target of the link marking it, if any: `text`; `link`, its URL, which `text` links to where no link marks it; `after`, its URL, shown after a link going elsewhere. `undefined` where it shows no text, so a note stands in its place.
|
||||
export function shownImage(media: AdfNode, href: string | undefined): { after: string | undefined; link: string | undefined; text: string } | undefined {
|
||||
const alt = visibleText(nodeAttrs(media)['alt'])
|
||||
const url = shownUrl(media)
|
||||
if (url === undefined) return { after: undefined, link: undefined, text: alt }
|
||||
if (url === undefined) return alt === undefined ? undefined : { after: undefined, link: undefined, text: alt }
|
||||
if (href === undefined) return { after: undefined, link: url, text: alt ?? url }
|
||||
if (href === url) return { after: undefined, link: undefined, text: alt ?? url }
|
||||
return { after: url, link: undefined, text: alt ?? visibleText(href) ?? url }
|
||||
|
||||
@@ -55,11 +55,11 @@ function shownPiece(node: AdfNode, lineFeed: string): string {
|
||||
return linkedText(node, shownText(node), shownUrl(node), lineFeed)
|
||||
}
|
||||
|
||||
// As portable markdown writes it inline, except that a link around a note stays (docs/decisions.md §The HTML dialect).
|
||||
// As portable markdown writes it inline, except that a link around a note stays and an image's own `text`, outside ADF's schema, shows first (docs/decisions.md §The HTML dialect).
|
||||
function linkedImage(media: AdfNode, lineFeed: string): string {
|
||||
const { after, link, text } = shownImage(media, shownLink(media)?.href)
|
||||
const shown = linkedText(media, text, link, lineFeed)
|
||||
return after === undefined ? shown : `${shown} (${linked('image', { href: after })})`
|
||||
const image = shownImage(media, shownLink(media)?.href)
|
||||
const shown = linkedText(media, image?.text, image?.link, lineFeed)
|
||||
return image?.after === undefined ? shown : `${shown} (${linked('image', { href: image.after })})`
|
||||
}
|
||||
|
||||
function linkedText(node: AdfNode, standIn: string | undefined, url: string | undefined, lineFeed: string): string {
|
||||
|
||||
@@ -59,14 +59,16 @@ function cardLeaves(card: AdfNode, marks: readonly AdfMark[], container: LineCon
|
||||
return linkedLeaves(shownText(card) ?? href, href, marks, container)
|
||||
}
|
||||
|
||||
function linkedLeaves(text: string | undefined, href: string, marks: readonly AdfMark[], container: LineContainer): AdfNode[] {
|
||||
function linkedLeaves(text: string, href: string, marks: readonly AdfMark[], container: LineContainer): AdfNode[] {
|
||||
return textLeaves(text, [...marks.filter((mark) => mark.type !== 'link'), { attrs: { href }, type: 'link' }], container)
|
||||
}
|
||||
|
||||
// An image standing inline is a link to it: the reader builds no node for an inline image, reading it as its alt text linked, and inside a link as the link's text, then (image).
|
||||
function mediaLeaves(media: AdfNode, marks: readonly AdfMark[], container: LineContainer): AdfNode[] {
|
||||
const { after, link, text } = shownImage(media, shownLink(media)?.href)
|
||||
const shown = link === undefined ? textLeaves(text, marks, container, referenceName(media)) : linkedLeaves(text, link, marks, container)
|
||||
const image = shownImage(media, shownLink(media)?.href)
|
||||
if (image === undefined) return textLeaves(undefined, marks, container, referenceName(media))
|
||||
const { after, link, text } = image
|
||||
const shown = link === undefined ? textLeaves(text, marks, container) : linkedLeaves(text, link, marks, container)
|
||||
return after === undefined ? shown : [...shown, ...imageAside((word) => linkedLeaves(word, after, marks, container))]
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user