import he from "he"; import type { Wikilink } from "./wikiLink"; import type { AstroIntegrationLogger } from "astro"; const imageDisplayClassNames: Record = { 'float-left': 'figure-image-float-left', 'float-right': 'figure-image-float-right', 'left': 'figure-image-float-left', 'right': 'figure-image-float-right', }; const imageSizing = (text: string): { width: number; height?: number; }|null => { if (!Number.isNaN(+text)) { return { width: Number.parseInt(text) }; } const [w, h] = text.split('x') as [string, string]; if (!w || !h) { return null; } if (!Number.isNaN(+w) && !Number.isNaN(+h)) { return { width: Number.parseInt(w as string), height: Number.parseInt(h as string) }; } return null; } const URL_REGEX = /(? { const matches = Array.from(caption.matchAll(URL_REGEX)); if (matches.length === 0) { return caption; } let captionTpl = caption; for (const match of matches) { const [url] = match; const urlObj = new URL(url.startsWith('http') ? url : `https://${url}`); captionTpl = captionTpl.replace(url, `${urlObj.hostname}`); } return captionTpl; } export const renderImage = async ( rendered: { metadata: { imagePaths: string[], } }, link: Wikilink, logger: AstroIntegrationLogger ) => { const title = (link.link.href as string).split('/').reverse()?.[0]; let caption: string|undefined = undefined; const altFragments = ((link.link.caption as string) ?? '').split('|'); const figureProps: Record = { alt: title, class: 'figure-image', }; const imageProps: Record = { src: link.link.href, alt: link.link.title, index: rendered.metadata.imagePaths.indexOf(link.link.href as string) }; for (const fragment of altFragments) { const sizing = imageSizing(fragment); if (sizing) { imageProps.width = sizing.width ?? undefined; imageProps.height = sizing.height ?? undefined; figureProps.style = imageProps.style = `${sizing.width ? `width: ${sizing.width}px; ` : ''}${sizing.height ? `height: ${sizing.height}px; ` : ''}` continue; } const layoutClassName = imageDisplayClassNames[fragment]; if (layoutClassName) { figureProps.class = [figureProps.class, layoutClassName].join(' '); continue; } caption = fragment; } const imageElement = ``; const captionElement = caption && caption !== title ? `
${captionChildren(caption)}
` : '' return `
${imageElement}${captionElement}
`; }