| import type { Root as HastRoot } from 'hast'; |
| import { visit } from 'unist-util-visit'; |
| import type { DatabaseMessageExtra, DatabaseMessageExtraImageFile } from '$lib/types/database'; |
| import { AttachmentType, UrlProtocol } from '$lib/enums'; |
|
|
| |
| |
| |
| |
| export function rehypeResolveAttachmentImages(options: { attachments?: DatabaseMessageExtra[] }) { |
| return (tree: HastRoot) => { |
| visit(tree, 'element', (node) => { |
| if (node.tagName === 'img' && node.properties?.src) { |
| const src = String(node.properties.src); |
|
|
| |
| if (src.startsWith(UrlProtocol.DATA) || src.startsWith(UrlProtocol.HTTP)) { |
| return; |
| } |
|
|
| |
| const attachment = options.attachments?.find( |
| (a): a is DatabaseMessageExtraImageFile => |
| a.type === AttachmentType.IMAGE && a.name === src |
| ); |
|
|
| |
| if (attachment?.base64Url) { |
| node.properties.src = attachment.base64Url; |
| } |
| } |
| }); |
| }; |
| } |
|
|