import { App , TFile } from 'obsidian' ; import OzanClearImages from './main' ; const imageRegex = /.*(jpe?g|png|gif|svg|bmp)/i ; const imageExtensions = new Set ( [ 'jpeg' , 'jpg' , 'png' , 'gif' , 'svg' , 'bmp' ] ) ; export const getUnusedImages = ( app ) => { var allImagesInVault = getAllImagesInVault ( app ) ; var unusedImages = [ ] ; var usedImagesSet ; usedImagesSet = getImagePathSetForVault ( app ) ; allImagesInVault . forEach ( ( img ) => { if ( ! usedImagesSet . has ( img . path ) ) unusedImages . push ( img ) ; } ) ; return unusedImages ; } ; const getAllImagesInVault = ( app ) : TFile [ ] => { let allFiles = app . vault . getFiles ( ) ; let images = [ ] ; for ( let i = 0 ; i < allFiles . length ; i ++ ) { if ( imageExtensions . has ( allFiles [ i ] . extension . toLowerCase ( ) ) ) { images . push ( allFiles [ i ] ) ; } } return images ; } ; const getImagePathSetForVault = ( app ) : Set < string > => { var imagesSet = new Set ( ) ; var resolvedLinks = app . metadataCache . resolvedLinks ; if ( resolvedLinks ) { for ( const [ mdFile , links ] of Object . entries ( resolvedLinks ) ) { for ( const [ filePath , nr ] of Object . entries ( resolvedLinks [ mdFile ] ) ) { var imageMatch = filePath . match ( imageRegex ) ; if ( imageMatch ) imagesSet . add ( imageMatch [ 0 ] ) ; } } } let mdFiles = app . vault . getMarkdownFiles ( ) ; mdFiles . forEach ( ( mdFile ) => { let fileCache = app . metadataCache . getFileCache ( mdFile ) ; if ( fileCache . frontmatter ) { let frontmatter = fileCache . frontmatter ; for ( let k of Object . keys ( frontmatter ) ) { if ( typeof frontmatter [ k ] === 'string' && pathIsAnImage ( frontmatter [ k ] ) ) { imagesSet . add ( frontmatter [ k ] ) ; } } } } ) ; return imagesSet ; } ; const pathIsAnImage = ( path ) => { var match = path . match ( imageRegex ) ; return match ? true : false ; } ; export const deleteFilesInTheList = async ( fileList , plugin , app ) : Promise < { deletedImages : number ; textToView : string ; } > => { var deleteOption = plugin . settings . deleteOption ; var deletedImages = 0 ; let textToView = '' ; for ( let file of fileList ) { if ( fileIsInExcludedFolder ( file , plugin ) ) { console . log ( 'File not referenced but excluded: ' + file . path ) ; } else { if ( deleteOption === '.trash' ) { await app . vault . trash ( file , false ) ; textToView += `[+] Moved to Obsidian Trash: ` + file . path + '</br>' ; } else if ( deleteOption === 'system-trash' ) { await app . vault . trash ( file , true ) ; textToView += `[+] Moved to System Trash: ` + file . path + '</br>' ; } else if ( deleteOption === 'permanent' ) { await app . vault . delete ( file ) ; textToView += `[+] Deleted Permanently: ` + file . path + '</br>' ; } deletedImages ++ ; } } return { deletedImages , textToView } ; } ; const fileIsInExcludedFolder = ( file , plugin ) : boolean => { var excludedFoldersSettings = plugin . settings . excludedFolders ; var excludeSubfolders = plugin . settings . excludeSubfolders ; if ( excludedFoldersSettings === '' ) { return false ; } else { var excludedFolderPaths = new Set ( excludedFoldersSettings . split ( ',' ) . map ( ( folderPath ) => { return folderPath . trim ( ) ; } ) ) ; if ( excludeSubfolders ) { for ( let exludedFolderPath of excludedFolderPaths ) { var pathRegex = new RegExp ( exludedFolderPath + '.*' ) ; if ( file . parent . path . match ( pathRegex ) ) { return true ; } } } else { if ( excludedFolderPaths . has ( file . parent . path ) ) { return true ; } } return false ; } } ; export const getFormattedDate = ( ) => { let dt = new Date ( ) ; return dt . toLocaleDateString ( 'en-GB' , { year : 'numeric' , month : '2-digit' , day : '2-digit' , hour : '2-digit' , minute : '2-digit' , second : '2-digit' , } ) ; } ;