Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(unlazy-img): not properly handling mdit urls & bad format detection predicates #319

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions packages/markdown-it-unlazy-img/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ export interface UnlazyImagesOptions {
* @default { mapGlobPatterns: ['**\/.vitepress/cache/@nolebase/vitepress-plugin-thumbnail-hash/thumbhashes/map.json', '**\/thumbhashes/map.json'] }
*/
thumbhash?: ThumbnailImageThumbhashOptions
/**
* Log format not supported warning
*/
logFormatNotSupportedWarning?: boolean
}

const logModulePrefix = `${cyan(`@nolebase/markdown-it-unlazy-img`)}${gray(':')}`
Expand Down Expand Up @@ -196,7 +200,7 @@ export const UnlazyImages: () => PluginWithOptions<UnlazyImagesOptions> = () =>

const imageRule = md.renderer.rules.image!

md.renderer.rules.image = (tokens, idx, options, env, self) => {
md.renderer.rules.image = (tokens, idx, mdOptions, env, self) => {
thumbhashMap = ensureThumbhashMap(thumbhash, thumbhashMap)

if (!env.path && !env.relativePath)
Expand All @@ -206,17 +210,20 @@ export const UnlazyImages: () => PluginWithOptions<UnlazyImagesOptions> = () =>

const imgSrc = token.attrGet('src')
if (!imgSrc)
return imageRule(tokens, idx, options, env, self)
return imageRule(tokens, idx, mdOptions, env, self)
// Skip external URLs
if (EXTERNAL_URL_RE.test(imgSrc))
return imageRule(tokens, idx, options, env, self)
return imageRule(tokens, idx, mdOptions, env, self)
// Skip unsupported image formats
if (['.png', '.jpg', '.jpeg'].every(ext => imgSrc.endsWith(ext))) {
console.warn(`${logModulePrefix} ${yellow('[WARN]')} unsupported image format for ${imgSrc}`)
return imageRule(tokens, idx, options, env, self)
if (!(['.png', '.jpg', '.jpeg'].some(ext => imgSrc.endsWith(ext)))) {
if (options?.logFormatNotSupportedWarning) {
console.warn(`${logModulePrefix} ${yellow('[WARN]')} unsupported image format for ${imgSrc}`)
}

return imageRule(tokens, idx, mdOptions, env, self)
}

let resolvedImgSrc = imgSrc
let resolvedImgSrc = decodeURIComponent(imgSrc)

const props: {
[name: string]: string | undefined
Expand Down Expand Up @@ -266,7 +273,7 @@ export const UnlazyImages: () => PluginWithOptions<UnlazyImagesOptions> = () =>
if (!matchedThumbhashData) {
// Usually this should not happen
console.warn(`${logModulePrefix} ${yellow(`[WARN]`)} thumbhash data not found for ${resolvedImgSrc}`)
return imageRule(tokens, idx, options, env, self)
return imageRule(tokens, idx, mdOptions, env, self)
}

// Apply all the attributes as
Expand Down