fix: remove encodeURI for image links#128
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
💤 Files with no reviewable changes (1)
📝 WalkthroughWalkthroughImage URL decoding now uses guarded ChangesImage link decoding update
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
src/utils.ts (2)
348-353: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winFallback path leaves the link still percent-encoded, which can break downstream resolution.
When
decodeURIComponentthrows, the fallback only strips../from the still-encodedurlEncodedImageLink. That value then flows intopath.parse(path.basename(imageLink)).name(fileName) andplugin.app.metadataCache.getFirstLinkpathDest(imageLink, contentPath), both of which expect a normal (decoded) vault-relative path. On decode failure, link resolution will likely silently miss the real file instead of crashing — an improvement over the previous unguardeddecodeURIcrash, but still a soft failure mode worth surfacing more clearly (e.g., skip/log the specific image rather than proceeding with a mismatched path).Also applies to: 861-866
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/utils.ts` around lines 348 - 353, The fallback in the image-link decoding logic still leaves `imageLink` percent-encoded after `decodeURIComponent` fails, which can break downstream lookup in `path.parse(...).name` and `plugin.app.metadataCache.getFirstLinkpathDest(...)`. Update the try/catch around the `decodeURIComponent(urlEncodedImageLink)` flow in `src/utils.ts` so the failure path does not continue with a mismatched encoded path; instead, surface the specific bad image via logging and skip processing that entry, or otherwise ensure the fallback produces a normalized vault-relative path before it reaches the file resolution logic.
347-358: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winDuplicate decode-with-fallback logic across two functions.
The identical
try { decodeURIComponent(...).replace(/\.\.\//g, "") } catch { ... }block is now duplicated verbatim intryCopyImageandtryCopyMarkdownByRead. Consider extracting a shared helper (e.g.safeDecodeImageLink(link: string): string) to avoid drift between the two copies going forward.♻️ Suggested helper extraction
+function safeDecodeImageLink(urlEncodedImageLink: string): string { + try { + return decodeURIComponent(urlEncodedImageLink).replace(/\.\.\//g, ""); + } catch (e) { + console.warn(`Unable to decode URI: ${urlEncodedImageLink}`); + return urlEncodedImageLink.replace(/\.\.\//g, ""); + } +}Then call
safeDecodeImageLink(urlEncodedImageLink)at both sites.Also applies to: 860-866
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/utils.ts` around lines 347 - 358, The decode-with-fallback block is duplicated in tryCopyImage and tryCopyMarkdownByRead, so extract it into a shared helper such as safeDecodeImageLink(link: string): string in src/utils.ts and have both call sites use it instead of inlining the try/catch. Keep the helper responsible for decodeURIComponent, the ../ sanitization, and the console.warn fallback so the behavior stays identical while avoiding drift between the two functions.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@src/utils.ts`:
- Around line 348-353: The fallback in the image-link decoding logic still
leaves `imageLink` percent-encoded after `decodeURIComponent` fails, which can
break downstream lookup in `path.parse(...).name` and
`plugin.app.metadataCache.getFirstLinkpathDest(...)`. Update the try/catch
around the `decodeURIComponent(urlEncodedImageLink)` flow in `src/utils.ts` so
the failure path does not continue with a mismatched encoded path; instead,
surface the specific bad image via logging and skip processing that entry, or
otherwise ensure the fallback produces a normalized vault-relative path before
it reaches the file resolution logic.
- Around line 347-358: The decode-with-fallback block is duplicated in
tryCopyImage and tryCopyMarkdownByRead, so extract it into a shared helper such
as safeDecodeImageLink(link: string): string in src/utils.ts and have both call
sites use it instead of inlining the try/catch. Keep the helper responsible for
decodeURIComponent, the ../ sanitization, and the console.warn fallback so the
behavior stays identical while avoiding drift between the two functions.
Fixes a bug where Chinese (and other non-ASCII) filenames were incorrectly encoded as URL-encoded strings (e.g.,
%E6%B5%8B%E8%AF%95.png) when exporting images withfileNameEncodedisabled. This happened becauseencodeURI()was applied to the file name, which is unnecessary since modern file systems natively support UTF-8 filenames. Additionally, thedecodeURIcall lacked error handling, which could cause export failures when filenames contained invalid percent-encoded sequences.Changes
encodeURI(fileName)call in the image export logic, so Chinese filenames are written as-is to the file system.try...catchblock arounddecodeURIComponentto gracefully handle malformed URI sequences, logging a warning and falling back to the original link if decoding fails.Testing
fileNameEncode.测试.png) into a note.%sequences to ensure no export crash occurs.No breaking changes expected.
Summary by CodeRabbit