diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index f31ff2c5..64f18e25 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -926,10 +926,7 @@ export default class CodeEditor extends React.Component { handlePaste (editor, forceSmartPaste) { const { storageKey, noteKey, fetchUrlTitle, enableSmartPaste } = this.props - const isURL = str => { - const matcher = /^(?:\w+:)?\/\/([^\s\.]+\.\S{2}|localhost[\:?\d]*)\S*$/ - return matcher.test(str) - } + const isURL = str => /(?:^\w+:|^)\/\/(?:[^\s\.]+\.\S{2}|localhost[\:?\d]*)/.test(str) const isInLinkTag = editor => { const startCursor = editor.getCursor('start') @@ -1002,7 +999,7 @@ export default class CodeEditor extends React.Component { } else { const image = clipboard.readImage() if (!image.isEmpty()) { - attachmentManagement.handlePastNativeImage( + attachmentManagement.handlePasteNativeImage( this, storageKey, noteKey, @@ -1147,7 +1144,7 @@ export default class CodeEditor extends React.Component { iconv.encodingExists(_charset) ? _charset : 'utf-8' - resolve(iconv.decode(new Buffer(buff), charset).toString()) + resolve(iconv.decode(Buffer.from(buff), charset).toString()) } catch (e) { reject(e) } diff --git a/browser/main/lib/dataApi/attachmentManagement.js b/browser/main/lib/dataApi/attachmentManagement.js index 6a0315f7..407fa659 100644 --- a/browser/main/lib/dataApi/attachmentManagement.js +++ b/browser/main/lib/dataApi/attachmentManagement.js @@ -231,11 +231,11 @@ function fixLocalURLS (renderedHTML, storagePath) { A :storage reference is like `:storage/3b6f8bd6-4edd-4b15-96e0-eadc4475b564/f939b2c3.jpg`. - `STORAGE_FOLDER_PLACEHOLDER` will match `:storage` - - `(?:(?:\\\/|%5C)[\\w.]+)+` will match `/3b6f8bd6-4edd-4b15-96e0-eadc4475b564/f939b2c3.jpg` - - `(?:\\\/|%5C)[\\w.]+` will either match `/3b6f8bd6-4edd-4b15-96e0-eadc4475b564` or `/f939b2c3.jpg` + - `(?:(?:\\\/|%5C)[-.\\w]+)+` will match `/3b6f8bd6-4edd-4b15-96e0-eadc4475b564/f939b2c3.jpg` + - `(?:\\\/|%5C)[-.\\w]+` will either match `/3b6f8bd6-4edd-4b15-96e0-eadc4475b564` or `/f939b2c3.jpg` - `(?:\\\/|%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) { + 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)) }) @@ -287,7 +287,7 @@ function handleAttachmentDrop (codeEditor, storageKey, noteKey, dropEvent) { * @param {String} noteKey Key of the current note * @param {DataTransferItem} dataTransferItem Part of the past-event */ -function handlePastImageEvent (codeEditor, storageKey, noteKey, dataTransferItem) { +function handlePasteImageEvent (codeEditor, storageKey, noteKey, dataTransferItem) { if (!codeEditor) { throw new Error('codeEditor has to be given') } @@ -331,7 +331,7 @@ function handlePastImageEvent (codeEditor, storageKey, noteKey, dataTransferItem * @param {String} noteKey Key of the current note * @param {NativeImage} image The native image */ -function handlePastNativeImage (codeEditor, storageKey, noteKey, image) { +function handlePasteNativeImage (codeEditor, storageKey, noteKey, image) { if (!codeEditor) { throw new Error('codeEditor has to be given') } @@ -429,7 +429,14 @@ function replaceNoteKeyWithNewNoteKey (noteContent, oldNoteKey, newNoteKey) { * @returns {String} Input without the references */ function removeStorageAndNoteReferences (input, noteKey) { - return input.replace(new RegExp(mdurl.encode(path.sep), 'g'), path.sep).replace(new RegExp(STORAGE_FOLDER_PLACEHOLDER + '(' + escapeStringRegexp(path.sep) + noteKey + ')?', 'g'), DESTINATION_FOLDER) + return input.replace(new RegExp('/?' + STORAGE_FOLDER_PLACEHOLDER + '.*?("|])', 'g'), function (match) { + const temp = match + .replace(new RegExp(mdurl.encode(path.win32.sep), 'g'), path.sep) + .replace(new RegExp(mdurl.encode(path.posix.sep), 'g'), path.sep) + .replace(new RegExp(escapeStringRegexp(path.win32.sep), 'g'), path.sep) + .replace(new RegExp(escapeStringRegexp(path.posix.sep), 'g'), path.sep) + return temp.replace(new RegExp(STORAGE_FOLDER_PLACEHOLDER + '(' + escapeStringRegexp(path.sep) + noteKey + ')?', 'g'), DESTINATION_FOLDER) + }) } /** @@ -584,8 +591,8 @@ module.exports = { fixLocalURLS, generateAttachmentMarkdown, handleAttachmentDrop, - handlePastImageEvent, - handlePastNativeImage, + handlePasteImageEvent, + handlePasteNativeImage, getAttachmentsInMarkdownContent, getAbsolutePathsOfAttachmentsInContent, removeStorageAndNoteReferences, diff --git a/tests/dataApi/attachmentManagement.test.js b/tests/dataApi/attachmentManagement.test.js index a4cc8082..53558de2 100644 --- a/tests/dataApi/attachmentManagement.test.js +++ b/tests/dataApi/attachmentManagement.test.js @@ -128,6 +128,7 @@ it('should test that copyAttachment don\'t uses a random file name if not intend it('should replace the all ":storage" path with the actual storage path', function () { const storageFolder = systemUnderTest.DESTINATION_FOLDER + const noteKey = '9c9c4ba3-bc1e-441f-9866-c1e9a806e31c' const testInput = '\n' + '
\n' + @@ -136,14 +137,18 @@ it('should replace the all ":storage" path with the actual storage path', functi ' \n' + '\n' +
- '
\n' +
+ '
\n' +
'
\n' + - ' dummyPDF.pdf\n' + + ' dummyPDF.pdf\n' + '
\n' + '\n' +
- '
\n' +
+ '
\n' +
'
\n' + + ' \n' + + '\n' + ' \n' + '' const storagePath = '<:storage' + mdurl.encode(path.sep) + noteKey + mdurl.encode(path.sep) + 'f939b2c3.jpg\n' + + '
\n' +
- '
\n' +
+ '
\n' +
'
\n' + - ' dummyPDF.pdf\n' + + ' dummyPDF.pdf\n' + '
\n' + '\n' +
- '
\n' +
+ '
\n' +
'
\n' + + ' \n' + + '\n' + ' \n' + '' const actual = systemUnderTest.fixLocalURLS(testInput, storagePath) @@ -171,6 +180,7 @@ it('should replace the all ":storage" path with the actual storage path', functi it('should replace the ":storage" path with the actual storage path when they have different path separators', function () { const storageFolder = systemUnderTest.DESTINATION_FOLDER + const noteKey = '9c9c4ba3-bc1e-441f-9866-c1e9a806e31c' const testInput = '\n' + ' \n' + @@ -179,10 +189,10 @@ it('should replace the ":storage" path with the actual storage path when they ha ' \n' + 'file:///' + storagePath + path.sep + storageFolder + path.sep + noteKey + path.sep + 'f939b2c3.jpg\n' + + '
\n' +
- '
\n' +
+ '
\n' +
'
\n' + - ' dummyPDF.pdf\n' + + ' dummyPDF.pdf\n' + '
\n' + ' \n' + '' @@ -195,10 +205,10 @@ it('should replace the ":storage" path with the actual storage path when they ha ' \n' + '\n' +
- '
\n' +
+ '
\n' +
'
\n' + - ' dummyPDF.pdf\n' + + ' dummyPDF.pdf\n' + '
\n' + ' \n' + '' @@ -251,28 +261,17 @@ it('should test that getAttachmentsInMarkdownContent finds all attachments when it('should test that getAbsolutePathsOfAttachmentsInContent returns all absolute paths', function () { const dummyStoragePath = 'dummyStoragePath' - const testInput = - '\n' + - ' \n' + - ' //header\n' + - ' \n' + - ' \n' + - '\n' +
- '
\n' +
- '
\n' + - ' dummyPDF.pdf\n' + - '
\n' + - '\n' +
- '
\n' +
- '
\n' +
- '
\n' +
+ '
\n' +
'
\n' + - ' dummyPDF.pdf\n' + + ' dummyPDF.pdf\n' + '
\n' + '\n' +
- '
\n' +
+ '
\n' +
'