@@ -157,7 +157,7 @@ function handlePastImageEvent (codeEditor, storageKey, noteKey, dataTransferItem
157
157
/**
158
158
* @description Returns all attachment paths of the given markdown
159
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
160
+ * @returns {String[] } Array of the relative paths (starting with :storage) of the attachments of the given markdown
161
161
*/
162
162
function getAttachmentsInContent ( markdownContent ) {
163
163
const preparedInput = markdownContent . replace ( new RegExp ( mdurl . encode ( path . sep ) , 'g' ) , path . sep )
@@ -190,6 +190,49 @@ function removeStorageAndNoteReferences (input, noteKey) {
190
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
191
}
192
192
193
+ /**
194
+ * @description Deletes all attachments stored in the attachment folder of the give not that are not referenced in the markdownContent
195
+ * @param markdownContent Content of the note. All unreferenced notes will be deleted
196
+ * @param storageKey StorageKey of the current note. Is used to determine the belonging attachment folder.
197
+ * @param noteKey NoteKey of the current note. Is used to determine the belonging attachment folder.
198
+ */
199
+ function deleteAttachmentsNotPresentInNote ( markdownContent , storageKey , noteKey ) {
200
+ const targetStorage = findStorage . findStorage ( storageKey )
201
+ const attachmentFolder = path . join ( targetStorage . path , DESTINATION_FOLDER , noteKey )
202
+ const attachmentsInNote = getAttachmentsInContent ( markdownContent )
203
+ const attachmentsInNoteOnlyFileNames = [ ]
204
+ if ( attachmentsInNote ) {
205
+ for ( let i = 0 ; i < attachmentsInNote . length ; i ++ ) {
206
+ attachmentsInNoteOnlyFileNames . push ( attachmentsInNote [ i ] . replace ( new RegExp ( STORAGE_FOLDER_PLACEHOLDER + escapeStringRegexp ( path . sep ) + noteKey + escapeStringRegexp ( path . sep ) , 'g' ) , '' ) )
207
+ }
208
+ }
209
+
210
+ if ( fs . existsSync ( attachmentFolder ) ) {
211
+ fs . readdir ( attachmentFolder , ( err , files ) => {
212
+ if ( err ) {
213
+ console . error ( "Error reading directory '" + attachmentFolder + "'. Error:" )
214
+ console . error ( err )
215
+ return
216
+ }
217
+ files . forEach ( file => {
218
+ if ( ! attachmentsInNoteOnlyFileNames . includes ( file ) ) {
219
+ const absolutePathOfFile = path . join ( targetStorage . path , DESTINATION_FOLDER , noteKey , file )
220
+ fs . unlink ( absolutePathOfFile , ( err ) => {
221
+ if ( err ) {
222
+ console . error ( "Could not delete '%s'" , absolutePathOfFile )
223
+ console . error ( err )
224
+ return
225
+ }
226
+ console . info ( "File '" + absolutePathOfFile + "' deleted because it was not included in the content of the note" )
227
+ } )
228
+ }
229
+ } )
230
+ } )
231
+ } else {
232
+ console . info ( "Attachment folder ('" + attachmentFolder + "') did not exist.." )
233
+ }
234
+ }
235
+
193
236
module . exports = {
194
237
copyAttachment,
195
238
fixLocalURLS,
@@ -199,6 +242,7 @@ module.exports = {
199
242
getAttachmentsInContent,
200
243
getAbsolutePathsOfAttachmentsInContent,
201
244
removeStorageAndNoteReferences,
245
+ deleteAttachmentsNotPresentInNote,
202
246
STORAGE_FOLDER_PLACEHOLDER ,
203
247
DESTINATION_FOLDER
204
248
}
0 commit comments