From 87a530612f92ed063e76837ace129368b1cf24b5 Mon Sep 17 00:00:00 2001 From: Baptiste Augrain Date: Thu, 25 Jun 2020 22:50:08 +0200 Subject: [PATCH] - use newest test - remove useless binding - regroup function --- browser/main/Detail/MarkdownNoteDetail.js | 4 +- .../main/lib/dataApi/attachmentManagement.js | 33 +++--------- tests/dataApi/attachmentManagement.test.js | 52 +++++++++++-------- 3 files changed, 39 insertions(+), 50 deletions(-) diff --git a/browser/main/Detail/MarkdownNoteDetail.js b/browser/main/Detail/MarkdownNoteDetail.js index 794358d5..856f266b 100755 --- a/browser/main/Detail/MarkdownNoteDetail.js +++ b/browser/main/Detail/MarkdownNoteDetail.js @@ -460,7 +460,7 @@ class MarkdownNoteDetail extends React.Component { storageKey={note.storage} noteKey={note.key} linesHighlighted={note.linesHighlighted} - onChange={this.handleUpdateContent.bind(this)} + onChange={this.handleUpdateContent} ignorePreviewPointerEvents={ignorePreviewPointerEvents} getNote={this.getNote} RTL={config.editor.rtlEnabled && this.state.RTL} @@ -475,7 +475,7 @@ class MarkdownNoteDetail extends React.Component { storageKey={note.storage} noteKey={note.key} linesHighlighted={note.linesHighlighted} - onChange={this.handleUpdateContent.bind(this)} + onChange={this.handleUpdateContent} ignorePreviewPointerEvents={ignorePreviewPointerEvents} getNote={this.getNote} RTL={config.editor.rtlEnabled && this.state.RTL} diff --git a/browser/main/lib/dataApi/attachmentManagement.js b/browser/main/lib/dataApi/attachmentManagement.js index cd7c8db8..33e00b0b 100644 --- a/browser/main/lib/dataApi/attachmentManagement.js +++ b/browser/main/lib/dataApi/attachmentManagement.js @@ -706,14 +706,15 @@ function replaceNoteKeyWithNewNoteKey(noteContent, oldNoteKey, newNoteKey) { } /** - * @description Deletes all :storage and noteKey references from the given input. - * @param input Input in which the references should be deleted + * @description replace all :storage references with given destination folder. + * @param input Input in which the references should be replaced * @param noteKey Key of the current note + * @param destinationFolder Destination folder of the attachements * @returns {String} Input without the references */ -function removeStorageAndNoteReferences(input, noteKey) { +function replaceStorageReferences(input, noteKey, destinationFolder) { return input.replace( - new RegExp('/?' + STORAGE_FOLDER_PLACEHOLDER + '.*?("|\\))', 'g'), + new RegExp('/?' + STORAGE_FOLDER_PLACEHOLDER + '[^"\\)<\\s]+', 'g'), function(match) { return match .replace(new RegExp(mdurl.encode(path.win32.sep), 'g'), path.posix.sep) @@ -735,33 +736,12 @@ function removeStorageAndNoteReferences(input, noteKey) { ')?', 'g' ), - DESTINATION_FOLDER + destinationFolder ) } ) } -/** - * @description replace all :storage references with given destination folder. - * @param input Input in which the references should be replaced - * @param noteKey Key of the current note - * @param destinationFolder Destination folder of the attachements - * @returns {String} Input without the references - */ -function replaceStorageReferences(input, noteKey, destinationFolder) { - return input.replace( - new RegExp( - STORAGE_FOLDER_PLACEHOLDER + - '(' + - escapeStringRegexp(path.sep) + - noteKey + - ')?', - 'g' - ), - destinationFolder || DESTINATION_FOLDER - ) -} - /** * @description Deletes the attachment folder specified by the given storageKey and noteKey * @param storageKey Key of the storage of the note to be deleted @@ -1122,7 +1102,6 @@ module.exports = { getAttachmentsInMarkdownContent, getAbsolutePathsOfAttachmentsInContent, importAttachments, - removeStorageAndNoteReferences, removeAttachmentsByPaths, replaceStorageReferences, deleteAttachmentFolder, diff --git a/tests/dataApi/attachmentManagement.test.js b/tests/dataApi/attachmentManagement.test.js index c3b40eac..6adee614 100644 --- a/tests/dataApi/attachmentManagement.test.js +++ b/tests/dataApi/attachmentManagement.test.js @@ -702,14 +702,15 @@ it('should remove the all ":storage" and noteKey references', function() { '

\n' + ' \n' + '' - const actual = systemUnderTest.removeStorageAndNoteReferences( + const actual = systemUnderTest.replaceStorageReferences( testInput, - noteKey + noteKey, + systemUnderTest.DESTINATION_FOLDER ) expect(actual).toEqual(expectedOutput) }) -it('should make sure that "removeStorageAndNoteReferences" works with markdown content as well', function() { +it('should make sure that "replaceStorageReferences" works with markdown content as well', function() { const noteKey = 'noteKey' const testInput = 'Test input' + @@ -736,9 +737,10 @@ it('should make sure that "removeStorageAndNoteReferences" works with markdown c systemUnderTest.DESTINATION_FOLDER + path.posix.sep + 'pdf.pdf)' - const actual = systemUnderTest.removeStorageAndNoteReferences( + const actual = systemUnderTest.replaceStorageReferences( testInput, - noteKey + noteKey, + systemUnderTest.DESTINATION_FOLDER ) expect(actual).toEqual(expectedOutput) }) @@ -803,7 +805,11 @@ it('should replace the all ":storage" references', function() { '

\n' + ' \n' + '' - const actual = systemUnderTest.replaceStorageReferences(testInput, noteKey) + const actual = systemUnderTest.replaceStorageReferences( + testInput, + noteKey, + systemUnderTest.DESTINATION_FOLDER + ) expect(actual).toEqual(expectedOutput) }) @@ -811,30 +817,34 @@ it('should make sure that "replaceStorageReferences" works with markdown content const noteKey = 'noteKey' const testInput = 'Test input' + - '![' + + '![imageName](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + - path.sep + + path.win32.sep + noteKey + - path.sep + - 'image.jpg](imageName}) \n' + - '[' + + path.win32.sep + + 'image.jpg) \n' + + '[pdf](' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + - path.sep + + path.posix.sep + noteKey + - path.sep + - 'pdf.pdf](pdf})' + path.posix.sep + + 'pdf.pdf)' const expectedOutput = 'Test input' + - '![' + + '![imageName](' + systemUnderTest.DESTINATION_FOLDER + - path.sep + - 'image.jpg](imageName}) \n' + - '[' + + path.posix.sep + + 'image.jpg) \n' + + '[pdf](' + systemUnderTest.DESTINATION_FOLDER + - path.sep + - 'pdf.pdf](pdf})' - const actual = systemUnderTest.replaceStorageReferences(testInput, noteKey) + path.posix.sep + + 'pdf.pdf)' + const actual = systemUnderTest.replaceStorageReferences( + testInput, + noteKey, + systemUnderTest.DESTINATION_FOLDER + ) expect(actual).toEqual(expectedOutput) })