publish(task 5): markdown bild-url-rewriter (mapping-basiert, =WxH-strip)
rewriteImageUrls(md, mapping) ersetzt alle - und [](link)-konstrukte per dateinamen-lookup durch blossom-urls aus dem mapping. absolute urls bleiben unverändert, unbekannte dateinamen bleiben stehen (kein strip). =WxH-größenhinweise werden entfernt. URL-dekodierung für leerzeichen-dateinamen (z.b. '03-config generieren.png' im moodle-post). resolveCoverUrl()-helper für den cover-lookup. 7 tests grün. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
bc2679aeba
commit
a6c5bd26e7
|
|
@ -0,0 +1,29 @@
|
|||
const IMG_RE = /!\[([^\]]*)\]\(([^)\s]+)(?:\s+=\d+x\d+)?\)/g
|
||||
|
||||
function isAbsolute(url: string): boolean {
|
||||
return /^(https?:)?\/\//i.test(url)
|
||||
}
|
||||
|
||||
export function rewriteImageUrls(md: string, mapping: Map<string, string>): string {
|
||||
return md.replace(IMG_RE, (full, alt: string, url: string) => {
|
||||
if (isAbsolute(url)) return full.replace(/\s+=\d+x\d+\)$/, ')')
|
||||
let decoded: string
|
||||
try {
|
||||
decoded = decodeURIComponent(url)
|
||||
} catch {
|
||||
decoded = url
|
||||
}
|
||||
const target = mapping.get(decoded) ?? mapping.get(url)
|
||||
if (!target) return full.replace(/\s+=\d+x\d+\)$/, ')')
|
||||
return ``
|
||||
})
|
||||
}
|
||||
|
||||
export function resolveCoverUrl(
|
||||
coverRaw: string | undefined,
|
||||
mapping: Map<string, string>,
|
||||
): string | undefined {
|
||||
if (!coverRaw) return undefined
|
||||
if (isAbsolute(coverRaw)) return coverRaw
|
||||
return mapping.get(coverRaw)
|
||||
}
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
import { assertEquals } from '@std/assert'
|
||||
import { rewriteImageUrls } from '../src/core/markdown.ts'
|
||||
|
||||
Deno.test('rewriteImageUrls: ersetzt  durch Mapping', () => {
|
||||
const mapping = new Map([['cat.png', 'https://blossom.example/hash.png']])
|
||||
const input = ''
|
||||
assertEquals(rewriteImageUrls(input, mapping), '')
|
||||
})
|
||||
|
||||
Deno.test('rewriteImageUrls: absolute URL bleibt unverändert', () => {
|
||||
const mapping = new Map([['cat.png', 'https://blossom.example/hash.png']])
|
||||
const input = ''
|
||||
assertEquals(rewriteImageUrls(input, mapping), input)
|
||||
})
|
||||
|
||||
Deno.test('rewriteImageUrls: entfernt =WxH-Suffix', () => {
|
||||
const mapping = new Map([['cat.png', 'https://blossom.example/hash.png']])
|
||||
const input = ''
|
||||
assertEquals(rewriteImageUrls(input, mapping), '')
|
||||
})
|
||||
|
||||
Deno.test('rewriteImageUrls: bild-in-link [](link)', () => {
|
||||
const mapping = new Map([['cat.png', 'https://blossom.example/hash.png']])
|
||||
const input = '[](https://target.example.com)'
|
||||
assertEquals(
|
||||
rewriteImageUrls(input, mapping),
|
||||
'[](https://target.example.com)',
|
||||
)
|
||||
})
|
||||
|
||||
Deno.test('rewriteImageUrls: mehrere Bilder im Text', () => {
|
||||
const mapping = new Map([
|
||||
['a.png', 'https://bl/a-hash.png'],
|
||||
['b.jpg', 'https://bl/b-hash.jpg'],
|
||||
])
|
||||
const input = 'Text  more  end'
|
||||
assertEquals(
|
||||
rewriteImageUrls(input, mapping),
|
||||
'Text  more  end',
|
||||
)
|
||||
})
|
||||
|
||||
Deno.test('rewriteImageUrls: lässt unbekannte Dateinamen stehen', () => {
|
||||
const mapping = new Map([['cat.png', 'https://bl/c.png']])
|
||||
const input = ''
|
||||
assertEquals(rewriteImageUrls(input, mapping), input)
|
||||
})
|
||||
|
||||
Deno.test('rewriteImageUrls: URL-Dekodierung für Leerzeichen-Namen', () => {
|
||||
const mapping = new Map([['file with spaces.png', 'https://bl/hash.png']])
|
||||
const input = ''
|
||||
assertEquals(rewriteImageUrls(input, mapping), '')
|
||||
})
|
||||
Loading…
Reference in New Issue