Skip to content

Commit f1b929c

Browse files
authored
Merge pull request #2848 from daiyam/fix-gallery
fix image path due to bad regex
2 parents 6960c8b + b1c6c04 commit f1b929c

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

browser/components/CodeEditor.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -888,10 +888,7 @@ export default class CodeEditor extends React.Component {
888888
handlePaste (editor, forceSmartPaste) {
889889
const { storageKey, noteKey, fetchUrlTitle, enableSmartPaste } = this.props
890890

891-
const isURL = str => {
892-
const matcher = /^(?:\w+:)?\/\/([^\s\.]+\.\S{2}|localhost[\:?\d]*)\S*$/
893-
return matcher.test(str)
894-
}
891+
const isURL = str => /(?:^\w+:|^)\/\/(?:[^\s\.]+\.\S{2}|localhost[\:?\d]*)/.test(str)
895892

896893
const isInLinkTag = editor => {
897894
const startCursor = editor.getCursor('start')
@@ -964,7 +961,7 @@ export default class CodeEditor extends React.Component {
964961
} else {
965962
const image = clipboard.readImage()
966963
if (!image.isEmpty()) {
967-
attachmentManagement.handlePastNativeImage(
964+
attachmentManagement.handlePasteNativeImage(
968965
this,
969966
storageKey,
970967
noteKey,
@@ -1109,7 +1106,7 @@ export default class CodeEditor extends React.Component {
11091106
iconv.encodingExists(_charset)
11101107
? _charset
11111108
: 'utf-8'
1112-
resolve(iconv.decode(new Buffer(buff), charset).toString())
1109+
resolve(iconv.decode(Buffer.from(buff), charset).toString())
11131110
} catch (e) {
11141111
reject(e)
11151112
}

browser/main/lib/dataApi/attachmentManagement.js

Lines changed: 13 additions & 5 deletions
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
})
@@ -279,7 +287,7 @@ function handleAttachmentDrop (codeEditor, storageKey, noteKey, dropEvent) {
279287
* @param {String} noteKey Key of the current note
280288
* @param {DataTransferItem} dataTransferItem Part of the past-event
281289
*/
282-
function handlePastImageEvent (codeEditor, storageKey, noteKey, dataTransferItem) {
290+
function handlePasteImageEvent (codeEditor, storageKey, noteKey, dataTransferItem) {
283291
if (!codeEditor) {
284292
throw new Error('codeEditor has to be given')
285293
}
@@ -323,7 +331,7 @@ function handlePastImageEvent (codeEditor, storageKey, noteKey, dataTransferItem
323331
* @param {String} noteKey Key of the current note
324332
* @param {NativeImage} image The native image
325333
*/
326-
function handlePastNativeImage (codeEditor, storageKey, noteKey, image) {
334+
function handlePasteNativeImage (codeEditor, storageKey, noteKey, image) {
327335
if (!codeEditor) {
328336
throw new Error('codeEditor has to be given')
329337
}
@@ -583,8 +591,8 @@ module.exports = {
583591
fixLocalURLS,
584592
generateAttachmentMarkdown,
585593
handleAttachmentDrop,
586-
handlePastImageEvent,
587-
handlePastNativeImage,
594+
handlePasteImageEvent,
595+
handlePasteNativeImage,
588596
getAttachmentsInMarkdownContent,
589597
getAbsolutePathsOfAttachmentsInContent,
590598
removeStorageAndNoteReferences,

tests/dataApi/attachmentManagement.test.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ it('should test that copyAttachment don\'t uses a random file name if not intend
128128

129129
it('should replace the all ":storage" path with the actual storage path', function () {
130130
const storageFolder = systemUnderTest.DESTINATION_FOLDER
131+
const noteKey = '9c9c4ba3-bc1e-441f-9866-c1e9a806e31c'
131132
const testInput =
132133
'<html>\n' +
133134
' <head>\n' +
@@ -136,14 +137,18 @@ it('should replace the all ":storage" path with the actual storage path', functi
136137
' <body data-theme="default">\n' +
137138
' <h2 data-line="0" id="Headline">Headline</h2>\n' +
138139
' <p data-line="2">\n' +
139-
' <img src=":storage' + mdurl.encode(path.sep) + '0.6r4zdgc22xp.png" alt="dummyImage.png" >\n' +
140+
' <img src=":storage' + mdurl.encode(path.sep) + noteKey + mdurl.encode(path.sep) + '0.6r4zdgc22xp.png" alt="dummyImage.png" >\n' +
140141
' </p>\n' +
141142
' <p data-line="4">\n' +
142-
' <a href=":storage' + mdurl.encode(path.sep) + '0.q2i4iw0fyx.pdf">dummyPDF.pdf</a>\n' +
143+
' <a href=":storage' + mdurl.encode(path.sep) + noteKey + mdurl.encode(path.sep) + '0.q2i4iw0fyx.pdf">dummyPDF.pdf</a>\n' +
143144
' </p>\n' +
144145
' <p data-line="6">\n' +
145-
' <img src=":storage' + mdurl.encode(path.sep) + 'd6c5ee92.jpg" alt="dummyImage2.jpg">\n' +
146+
' <img src=":storage' + mdurl.encode(path.sep) + noteKey + mdurl.encode(path.sep) + 'd6c5ee92.jpg" alt="dummyImage2.jpg">\n' +
146147
' </p>\n' +
148+
' <pre class="fence" data-line="8">\n' +
149+
' <span class="filename"></span>\n' +
150+
' <div class="gallery" data-autoplay="undefined" data-height="undefined">:storage' + mdurl.encode(path.sep) + noteKey + mdurl.encode(path.sep) + 'f939b2c3.jpg</div>\n' +
151+
' </pre>\n' +
147152
' </body>\n' +
148153
'</html>'
149154
const storagePath = '<<dummyStoragePath>>'
@@ -155,14 +160,18 @@ it('should replace the all ":storage" path with the actual storage path', functi
155160
' <body data-theme="default">\n' +
156161
' <h2 data-line="0" id="Headline">Headline</h2>\n' +
157162
' <p data-line="2">\n' +
158-
' <img src="file:///' + storagePath + path.sep + storageFolder + path.sep + '0.6r4zdgc22xp.png" alt="dummyImage.png" >\n' +
163+
' <img src="file:///' + storagePath + path.sep + storageFolder + path.sep + noteKey + path.sep + '0.6r4zdgc22xp.png" alt="dummyImage.png" >\n' +
159164
' </p>\n' +
160165
' <p data-line="4">\n' +
161-
' <a href="file:///' + storagePath + path.sep + storageFolder + path.sep + '0.q2i4iw0fyx.pdf">dummyPDF.pdf</a>\n' +
166+
' <a href="file:///' + storagePath + path.sep + storageFolder + path.sep + noteKey + path.sep + '0.q2i4iw0fyx.pdf">dummyPDF.pdf</a>\n' +
162167
' </p>\n' +
163168
' <p data-line="6">\n' +
164-
' <img src="file:///' + storagePath + path.sep + storageFolder + path.sep + 'd6c5ee92.jpg" alt="dummyImage2.jpg">\n' +
169+
' <img src="file:///' + storagePath + path.sep + storageFolder + path.sep + noteKey + path.sep + 'd6c5ee92.jpg" alt="dummyImage2.jpg">\n' +
165170
' </p>\n' +
171+
' <pre class="fence" data-line="8">\n' +
172+
' <span class="filename"></span>\n' +
173+
' <div class="gallery" data-autoplay="undefined" data-height="undefined">file:///' + storagePath + path.sep + storageFolder + path.sep + noteKey + path.sep + 'f939b2c3.jpg</div>\n' +
174+
' </pre>\n' +
166175
' </body>\n' +
167176
'</html>'
168177
const actual = systemUnderTest.fixLocalURLS(testInput, storagePath)

0 commit comments

Comments
 (0)