From 4e30d4b8fbe9994892e6476a768eed34f7a2577a Mon Sep 17 00:00:00 2001 From: Baptiste Augrain Date: Mon, 27 May 2019 01:01:00 +0200 Subject: [PATCH] fix URLs by using the correct path separator ('/' for an url and not `path.sep`) --- browser/main/lib/dataApi/attachmentManagement.js | 7 +++++-- tests/dataApi/attachmentManagement.test.js | 14 +++++++------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/browser/main/lib/dataApi/attachmentManagement.js b/browser/main/lib/dataApi/attachmentManagement.js index a5adf685..725bdc11 100644 --- a/browser/main/lib/dataApi/attachmentManagement.js +++ b/browser/main/lib/dataApi/attachmentManagement.js @@ -241,6 +241,10 @@ function migrateAttachments (markdownContent, storagePath, noteKey) { * @returns {String} postprocessed HTML in which all :storage references are mapped to the actual paths. */ function fixLocalURLS (renderedHTML, storagePath) { + const encodedWin32SeparatorRegex = /%5C/g + const storageRegex = new RegExp('/?' + STORAGE_FOLDER_PLACEHOLDER, 'g') + const storageUrl = 'file:///' + path.join(storagePath, DESTINATION_FOLDER).replace(/\\/g, '/') + /* A :storage reference is like `:storage/3b6f8bd6-4edd-4b15-96e0-eadc4475b564/f939b2c3.jpg`. @@ -250,8 +254,7 @@ function fixLocalURLS (renderedHTML, storagePath) { - `(?:\\\/|%5C)` match the path seperator. `\\\/` for posix systems and `%5C` for windows. */ return renderedHTML.replace(new RegExp('/?' + STORAGE_FOLDER_PLACEHOLDER + '(?:(?:\\\/|%5C)[-.\\w]+)+', 'g'), function (match) { - var encodedPathSeparators = new RegExp(mdurl.encode(path.win32.sep) + '|' + mdurl.encode(path.posix.sep), 'g') - return match.replace(encodedPathSeparators, path.sep).replace(new RegExp('/?' + STORAGE_FOLDER_PLACEHOLDER, 'g'), 'file:///' + path.join(storagePath, DESTINATION_FOLDER)) + return match.replace(encodedWin32SeparatorRegex, '/').replace(storageRegex, storageUrl) }) } diff --git a/tests/dataApi/attachmentManagement.test.js b/tests/dataApi/attachmentManagement.test.js index 96394d9f..0f420bcc 100644 --- a/tests/dataApi/attachmentManagement.test.js +++ b/tests/dataApi/attachmentManagement.test.js @@ -304,21 +304,21 @@ it('should replace the all ":storage" path with the actual storage path', functi ' \n' + '

Headline

\n' + '

\n' + - ' dummyImage.png\n' + + ' dummyImage.png\n' + '

\n' + '

\n' + - ' dummyPDF.pdf\n' + + ' dummyPDF.pdf\n' + '

\n' + '

\n' + - ' dummyImage2.jpg\n' + + ' dummyImage2.jpg\n' + '

\n' + '
\n' +
     '            \n' +
-    '            \n' +
+    '            \n' +
     '        
\n' + '
\n' +
     '            \n' +
-    '            \n' +
+    '            \n' +
     '        
\n' + ' \n' + '' @@ -353,10 +353,10 @@ it('should replace the ":storage" path with the actual storage path when they ha ' \n' + '

Headline

\n' + '

\n' + - ' dummyImage.png\n' + + ' dummyImage.png\n' + '

\n' + '

\n' + - ' dummyPDF.pdf\n' + + ' dummyPDF.pdf\n' + '

\n' + ' \n' + ''