added image size

This commit is contained in:
2026-05-14 18:07:08 +02:00
parent ceb3750add
commit 2ace527c9f
4 changed files with 27 additions and 4 deletions
+16 -2
View File
@@ -79,8 +79,22 @@ const KATEX_TAGS = [
'mpadded', 'menclose',
];
export function renderMarkdown(src: string): string {
const html = renderer.parse(src, { async: false }) as string;
export interface ImageDim { w: number; h: number }
export type ImageDims = Record<string, ImageDim>;
function injectDimensions(html: string, dims?: ImageDims): string {
if (!dims) return html;
return html.replace(/<img\s+src="([^"]+)"([^>]*?)\s*\/?>/g, (match, src, rest) => {
const d = dims[src];
if (!d) return match;
if (/\swidth\s*=/.test(rest) || /\sheight\s*=/.test(rest)) return match;
return `<img src="${src}" width="${d.w}" height="${d.h}"${rest} />`;
});
}
export function renderMarkdown(src: string, dims?: ImageDims): string {
let html = renderer.parse(src, { async: false }) as string;
html = injectDimensions(html, dims);
return DOMPurify.sanitize(html, {
ADD_TAGS: [...KATEX_TAGS, 'figure', 'figcaption'],
ADD_ATTR: ['aria-hidden', 'style', 'id', 'class', 'encoding', 'mathvariant', 'displaystyle', 'scriptlevel', 'loading'],