chore: update code structure for improved readability and maintainability

This commit is contained in:
2026-05-10 19:34:06 +10:00
parent ed797df9b4
commit 924ebfa164
6 changed files with 41 additions and 11 deletions

View File

@@ -424,6 +424,18 @@
/** uploadId -> resolve(relativePath) */
const _uploadResolvers = new Map();
/**
* Splits markdown image target into src and optional title segment.
* @param {string} target
* @returns {{ src: string, suffix: string }}
*/
function splitImageTarget(target) {
const trimmed = target.trim();
const m = trimmed.match(/^(\S+)(\s+["'][\s\S]*["'])?$/);
if (!m) return { src: trimmed, suffix: '' };
return { src: m[1], suffix: m[2] ?? '' };
}
/**
* Uploads a blob URL to the extension to save to media/.
* Returns a Promise that resolves once mediaSaved is received.
@@ -552,9 +564,10 @@
* @returns {string}
*/
function cleanImagePaths(markdown) {
return markdown.replace(/(\!\[[^\]]*\]\()([^)]+)(\))/g, (_m, open, src, close) => {
const rel = blobToRelative.get(src) ?? webviewToRelative.get(src);
return rel ? `${open}${rel}${close}` : `${open}${src}${close}`;
return markdown.replace(/(\!\[[^\]]*\]\()([^)]+)(\))/g, (_m, open, target, close) => {
const { src, suffix } = splitImageTarget(target);
const rel = blobToRelative.get(src) ?? webviewToRelative.get(src);
return rel ? `${open}${rel}${suffix}${close}` : `${open}${src}${suffix}${close}`;
});
}