Removed data-slug requirement for non-preview images

This commit is contained in:
Stephen Tse 2025-05-03 18:49:51 -07:00
parent 435e6cf899
commit 6cc665d8db

View File

@ -77,10 +77,6 @@ const OptimizeImages: Plugin<[Options], HtmlRoot> = (opts: Options) => {
if (isAbsoluteURL(src)) return // Skip External images if (isAbsoluteURL(src)) return // Skip External images
const ext = getFileExtension(src) const ext = getFileExtension(src)
if (!ext || !supportedImageExts.has(ext)) return if (!ext || !supportedImageExts.has(ext)) return
// `data-slug` is set by the OFM markdown transformer.
// This is the absolute file path compared to `src`, which can be relative.
const fullSlug = node.properties["dataSlug"] as FullSlug
if (!fullSlug) return
const width = const width =
node.properties.width && node.properties.width !== "auto" node.properties.width && node.properties.width !== "auto"
@ -100,6 +96,11 @@ const OptimizeImages: Plugin<[Options], HtmlRoot> = (opts: Options) => {
// Replace original image source with preview image if custom dimension is defined // Replace original image source with preview image if custom dimension is defined
if (width || height) { if (width || height) {
// `data-slug` is set by the OFM markdown transformer.
// This is the absolute file path compared to `src`, which can be relative.
const fullSlug = node.properties["dataSlug"] as FullSlug
if (!fullSlug) return
node.properties.src = src.replace( node.properties.src = src.replace(
new RegExp(`(?:${ext}|${targetOptimizedImageExt})$`), new RegExp(`(?:${ext}|${targetOptimizedImageExt})$`),
`-preview${shouldOptimizeImage ? targetOptimizedImageExt : ext}`, `-preview${shouldOptimizeImage ? targetOptimizedImageExt : ext}`,