3
0
mirror of https://github.com/larvit/adf-codec.git synced 2026-11-13 04:08:29 +01:00

Keep both targets of a linked external image standing inline in adfToPortableMarkdown

This commit is contained in:
2026-10-14 22:17:59 +02:00
parent 251c8193e6
commit 4298de84e8
7 changed files with 30 additions and 11 deletions
@@ -4,6 +4,7 @@ import test from 'node:test'
import type { AdfAttributes, AdfDocument, AdfMark, AdfNode } from '../../adf/document.ts'
import { adfToPortableMarkdown, reduceToPortable } from './adf-to-portable-markdown.ts'
import { largestNesting } from '../../nesting.ts'
import { portableMarkdownToAdf } from '../parse/markdown-to-adf.ts'
const code: AdfMark = { type: 'code' }
const em: AdfMark = { type: 'em' }
@@ -239,6 +240,17 @@ test('keeps the target of a link marking an external image', () => {
assert.equal(portable(linked('')), '[![Moon](https://example.com/moon.png)](<> "Docs")\n')
})
test('keeps both targets of a linked external image standing inline, the image last as a link reading image', () => {
const linked = (href: string, alt?: string): AdfNode => ({ attrs: { ...(alt === undefined ? {} : { alt }), type: 'external', url: 'https://example.com/moon.png' }, marks: [{ attrs: { href, title: 'Docs' }, type: 'link' }], type: 'media' })
const table = (...content: AdfNode[]): AdfNode => node('table', {}, row(cell('tableHeader', said('h'))), row(cell('tableCell', ...content)))
const inCell = portable(table(node('mediaSingle', {}, linked('https://example.com/docs', 'Moon'))))
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(paragraph(text('See '), linked('https://example.com/moon.png', 'Moon'))), 'See [Moon](https://example.com/moon.png "Docs")\n')
})
test('keeps an external image wherever it stands and spells a stored file as its alt text, else a note', () => {
const media = (attrs: AdfAttributes): AdfNode => ({ attrs, type: 'media' })
const caption: AdfNode = node('caption', {}, text('The moon.'))
+4 -1
View File
@@ -85,7 +85,10 @@ function mediaLeaves(attrs: Readonly<AdfAttributes>, marks: readonly AdfMark[],
const alt = nonEmpty(attrs['alt'])
const url = nonEmpty(attrs['url'])
if (attrs['type'] !== 'external' || url === undefined) return textLeaves(alt, marks, container, 'image')
return linkedLeaves(alt ?? url, url, marks, container)
const href = nodeAttrs(marks.find((mark) => mark.type === 'link') ?? {})['href']
if (href === url) return textLeaves(alt ?? url, marks, container)
if (typeof href !== 'string') return linkedLeaves(alt ?? url, url, marks, container)
return [...textLeaves(alt ?? url, marks, container), textLeaf(' (', []), ...linkedLeaves('image', url, marks, container), textLeaf(')', [])]
}
function noteLeaves(name: string): AdfNode[] {