/** Visible placeholder while inline attachment blobs load (not transparent). */
export const CID_IMAGE_PLACEHOLDER =
"data:image/svg+xml," +
encodeURIComponent(
'"
)
/** Normalize Content-ID / cid: URL references for lookup. */
export function normalizeCidKey(raw: string): string {
let key = raw.trim()
if (key.toLowerCase().startsWith("cid:")) {
key = key.slice(4).trim()
}
key = key.replace(/^<|>$/g, "").trim()
return key
}
/** Register blob/API URLs under common cid spellings (case, angle brackets). */
export function registerCidUrlAliases(
map: Record,
contentId: string,
url: string
): void {
const key = normalizeCidKey(contentId)
if (!key) return
map[key] = url
map[key.toLowerCase()] = url
map[`cid:${key}`] = url
map[`cid:${key.toLowerCase()}`] = url
}
export function lookupCidUrl(
map: Record,
cidReference: string
): string | undefined {
const key = normalizeCidKey(cidReference)
if (!key) return undefined
return (
map[key] ??
map[key.toLowerCase()] ??
map[`cid:${key}`] ??
map[`cid:${key.toLowerCase()}`]
)
}
export function resolveCidReference(
map: Record | undefined,
cidReference: string
): string {
const trimmed = cidReference.trim()
if (!trimmed.toLowerCase().startsWith("cid:")) return trimmed
return lookupCidUrl(map ?? {}, trimmed) ?? CID_IMAGE_PLACEHOLDER
}