added image size
This commit is contained in:
@@ -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'],
|
||||
|
||||
Reference in New Issue
Block a user