markdown render image with size params (#89)

This commit is contained in:
spencerwooo
2021-09-04 14:39:00 +01:00
parent 526de1bd72
commit 047508e5da
+24 -3
View File
@@ -26,16 +26,37 @@ const MarkdownPreview: FunctionComponent<{ file: any; path: string; standalone?:
const isUrlAbsolute = url => url.indexOf('://') > 0 || url.indexOf('//') === 0 const isUrlAbsolute = url => url.indexOf('://') > 0 || url.indexOf('//') === 0
// Custom renderer to render images with relative path // Custom renderer to render images with relative path
const relativeImagePathRenderer = { const relativeImagePathRenderer = {
img: ({ alt, src, title, style }: { alt?: string; src?: string; title?: string; style?: CSSProperties }) => { img: ({
alt,
src,
title,
width,
height,
style,
}: {
alt?: string
src?: string
title?: string
width?: string | number
height?: string | number
style?: CSSProperties
}) => {
if (isUrlAbsolute(src as string)) { if (isUrlAbsolute(src as string)) {
return ( return (
// eslint-disable-next-line @next/next/no-img-element // eslint-disable-next-line @next/next/no-img-element
<img alt={alt} src={src} title={title} style={style} /> <img alt={alt} src={src} title={title} width={width} height={height} style={style} />
) )
} }
return ( return (
// eslint-disable-next-line @next/next/no-img-element // eslint-disable-next-line @next/next/no-img-element
<img alt={alt} src={`/api?path=${parentPath}/${src}&raw=true`} title={title} style={style} /> <img
alt={alt}
src={`/api?path=${parentPath}/${src}&raw=true`}
title={title}
width={width}
height={height}
style={style}
/>
) )
}, },
} }