Skip to content

Commit 64f7233

Browse files
committed
fix regex to match :storage reference, added comment to explain what it does.
1 parent 9d81e4b commit 64f7233

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

browser/main/lib/dataApi/attachmentManagement.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,15 @@ function migrateAttachments (markdownContent, storagePath, noteKey) {
227227
* @returns {String} postprocessed HTML in which all :storage references are mapped to the actual paths.
228228
*/
229229
function fixLocalURLS (renderedHTML, storagePath) {
230-
return renderedHTML.replace(new RegExp('/?' + STORAGE_FOLDER_PLACEHOLDER + '.*?', 'g'), function (match) {
230+
/*
231+
A :storage reference is like `:storage/3b6f8bd6-4edd-4b15-96e0-eadc4475b564/f939b2c3.jpg`.
232+
233+
- `STORAGE_FOLDER_PLACEHOLDER` will match `:storage`
234+
- `(?:(?:\\\/|%5C)[\\w.]+)+` will match `/3b6f8bd6-4edd-4b15-96e0-eadc4475b564/f939b2c3.jpg`
235+
- `(?:\\\/|%5C)[\\w.]+` will either match `/3b6f8bd6-4edd-4b15-96e0-eadc4475b564` or `/f939b2c3.jpg`
236+
- `(?:\\\/|%5C)` match the path seperator. `\\\/` for posix systems and `%5C` for windows.
237+
*/
238+
return renderedHTML.replace(new RegExp('/?' + STORAGE_FOLDER_PLACEHOLDER + '(?:(?:\\\/|%5C)[\\w.]+)+', 'g'), function (match) {
231239
var encodedPathSeparators = new RegExp(mdurl.encode(path.win32.sep) + '|' + mdurl.encode(path.posix.sep), 'g')
232240
return match.replace(encodedPathSeparators, path.sep).replace(new RegExp('/?' + STORAGE_FOLDER_PLACEHOLDER, 'g'), 'file:///' + path.join(storagePath, DESTINATION_FOLDER))
233241
})

0 commit comments

Comments
 (0)