@@ -3,6 +3,7 @@ const fs = require('fs')
33const path = require ( 'path' )
44const findStorage = require ( 'browser/lib/findStorage' )
55const mdurl = require ( 'mdurl' )
6+ const escapeStringRegexp = require ( 'escape-string-regexp' )
67
78const STORAGE_FOLDER_PLACEHOLDER = ':storage'
89const DESTINATION_FOLDER = 'attachments'
@@ -104,8 +105,8 @@ function handleAttachmentDrop (codeEditor, storageKey, noteKey, dropEvent) {
104105 const fileType = file [ 'type' ]
105106
106107 copyAttachment ( filePath , storageKey , noteKey ) . then ( ( fileName ) => {
107- let showPreview = fileType . startsWith ( 'image' )
108- let imageMd = generateAttachmentMarkdown ( originalFileName , path . join ( STORAGE_FOLDER_PLACEHOLDER , noteKey , fileName ) , showPreview )
108+ const showPreview = fileType . startsWith ( 'image' )
109+ const imageMd = generateAttachmentMarkdown ( originalFileName , path . join ( STORAGE_FOLDER_PLACEHOLDER , noteKey , fileName ) , showPreview )
109110 codeEditor . insertAttachmentMd ( imageMd )
110111 } )
111112}
@@ -139,26 +140,65 @@ function handlePastImageEvent (codeEditor, storageKey, noteKey, dataTransferItem
139140 const destinationDir = path . join ( targetStorage . path , DESTINATION_FOLDER , noteKey )
140141 createAttachmentDestinationFolder ( targetStorage . path , noteKey )
141142
142- let imageName = `${ uniqueSlug ( ) } .png`
143+ const imageName = `${ uniqueSlug ( ) } .png`
143144 const imagePath = path . join ( destinationDir , imageName )
144145
145146 reader . onloadend = function ( ) {
146147 base64data = reader . result . replace ( / ^ d a t a : i m a g e \/ p n g ; b a s e 6 4 , / , '' )
147148 base64data += base64data . replace ( '+' , ' ' )
148149 const binaryData = new Buffer ( base64data , 'base64' ) . toString ( 'binary' )
149150 fs . writeFile ( imagePath , binaryData , 'binary' )
150- let imageMd = generateAttachmentMarkdown ( imageName , imagePath , true )
151+ const imageMd = generateAttachmentMarkdown ( imageName , imagePath , true )
151152 codeEditor . insertAttachmentMd ( imageMd )
152153 }
153154 reader . readAsDataURL ( blob )
154155}
155156
157+ /**
158+ * @description Returns all attachment paths of the given markdown
159+ * @param {String } markdownContent content in which the attachment paths should be found
160+ * @returns {String[] } Array of the relativ paths (starting with :storage) of the attachments of the given markdown
161+ */
162+ function getAttachmentsInContent ( markdownContent ) {
163+ const preparedInput = markdownContent . replace ( new RegExp ( mdurl . encode ( path . sep ) , 'g' ) , path . sep )
164+ const regexp = new RegExp ( STORAGE_FOLDER_PLACEHOLDER + escapeStringRegexp ( path . sep ) + '([a-zA-Z0-9]|-)+' + escapeStringRegexp ( path . sep ) + '[a-zA-Z0-9]+(\\.[a-zA-Z0-9]+)?' , 'g' )
165+ return preparedInput . match ( regexp )
166+ }
167+
168+ /**
169+ * @description Returns an array of the absolute paths of the attachments referenced in the given markdown code
170+ * @param {String } markdownContent content in which the attachment paths should be found
171+ * @param {String } storagePath path of the current storage
172+ * @returns {String[] } Absolute paths of the referenced attachments
173+ */
174+ function getAbsolutePathsOfAttachmentsInContent ( markdownContent , storagePath ) {
175+ const temp = getAttachmentsInContent ( markdownContent )
176+ const result = [ ]
177+ for ( const relativePath of temp ) {
178+ result . push ( relativePath . replace ( new RegExp ( STORAGE_FOLDER_PLACEHOLDER , 'g' ) , path . join ( storagePath , DESTINATION_FOLDER ) ) )
179+ }
180+ return result
181+ }
182+
183+ /**
184+ * @description Deletes all :storage and noteKey references from the given input.
185+ * @param input Input in which the references should be deleted
186+ * @param noteKey Key of the current note
187+ * @returns {String } Input without the references
188+ */
189+ function removeStorageAndNoteReferences ( input , noteKey ) {
190+ 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 )
191+ }
192+
156193module . exports = {
157194 copyAttachment,
158195 fixLocalURLS,
159196 generateAttachmentMarkdown,
160197 handleAttachmentDrop,
161198 handlePastImageEvent,
199+ getAttachmentsInContent,
200+ getAbsolutePathsOfAttachmentsInContent,
201+ removeStorageAndNoteReferences,
162202 STORAGE_FOLDER_PLACEHOLDER ,
163203 DESTINATION_FOLDER
164204}
0 commit comments