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

Answer architecture round 1: 1 🔴, 3 🟡; name the other-target link and no-alt case, pin both, file items 117 and 118

This commit is contained in:
2026-10-14 22:24:48 +02:00
committed by Lillem4n
parent d55f754e37
commit 928752a58d
5 changed files with 31 additions and 9 deletions
@@ -247,8 +247,10 @@ test('keeps both targets of a linked external image standing inline, the image l
assert.equal(inCell, '| h |\n| --- |\n| [Moon](https://example.com/docs "Docs") ([image](https://example.com/moon.png)) |\n')
const read = portableMarkdownToAdf(inCell)
assert.equal(read.ok && portableDocument(read.value), inCell)
assert.equal(portable(table(said('See'), node('mediaSingle', {}, linked('https://example.com/docs')))), '| h |\n| --- |\n| See [https://example.com/moon.png](https://example.com/docs "Docs") ([image](https://example.com/moon.png)) |\n')
assert.equal(portable(table(said('See'), node('mediaSingle', {}, linked('https://example.com/docs')))), '| h |\n| --- |\n| See [https://example.com/docs](https://example.com/docs "Docs") ([image](https://example.com/moon.png)) |\n')
assert.equal(portable(paragraph(text('See '), linked('https://example.com/moon.png', 'Moon'))), 'See [Moon](https://example.com/moon.png "Docs")\n')
assert.equal(portable(paragraph(text('See '), linked('https://example.com/moon.png'))), 'See [https://example.com/moon.png](https://example.com/moon.png "Docs")\n')
assert.equal(portable(paragraph(text('See '), linked(''))), 'See [https://example.com/moon.png](<> "Docs") ([image](https://example.com/moon.png))\n')
})
test('keeps an external image wherever it stands and spells a stored file as its alt text, else a note', () => {
+7 -4
View File
@@ -80,15 +80,18 @@ function idMention(id: unknown): string | undefined {
return typeof id === 'string' && id !== '' ? `@${id}` : undefined
}
// An image standing inline is a link to it: CommonMark's inline image reads back as no node.
// An image standing inline is a link to it: CommonMark's inline image reads back as no node, and one inside a link as the link's text.
function mediaLeaves(attrs: Readonly<AdfAttributes>, marks: readonly AdfMark[], container: LineContainer): AdfNode[] {
const alt = nonEmpty(attrs['alt'])
const url = nonEmpty(attrs['url'])
if (attrs['type'] !== 'external' || url === undefined) return textLeaves(alt, marks, container, 'image')
const href = nodeAttrs(marks.find((mark) => mark.type === 'link') ?? {})['href']
if (href === url) return textLeaves(alt ?? url, marks, container)
const link = marks.find((mark) => mark.type === 'link')
const href = link === undefined ? undefined : nodeAttrs(link)['href']
if (typeof href !== 'string') return linkedLeaves(alt ?? url, url, marks, container)
return [...textLeaves(alt ?? url, marks, container), textLeaf(' (', []), ...linkedLeaves('image', url, marks, container), textLeaf(')', [])]
if (href === url) return textLeaves(alt ?? url, marks, container)
const linkedAlt = textLeaves(alt ?? nonEmpty(href) ?? url, marks, container)
const image = linkedLeaves('image', url, marks, container)
return [...linkedAlt, textLeaf(' (', []), ...image, textLeaf(')', [])]
}
function noteLeaves(name: string): AdfNode[] {