From 1322688321dbfb2b34b4609969f1c52f3bd290ac Mon Sep 17 00:00:00 2001 From: Ozan Tellioglu Date: Sun, 5 Feb 2023 23:47:19 +0100 Subject: [PATCH] Skip Canvas Files during Clearing, Include Attachments within Canvas Files --- src/main.ts | 2 +- src/util.ts | 57 ++++++++++++++++++++++++++++++++++------------------- 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/src/main.ts b/src/main.ts index 018740b..5e44cbe 100644 --- a/src/main.ts +++ b/src/main.ts @@ -48,7 +48,7 @@ export default class OzanClearImages extends Plugin { // Compare Used Images with all images and return unused ones clearUnusedAttachments = async (type: 'all' | 'image') => { - var unusedAttachments: TFile[] = Util.getUnusedAttachments(this.app, type); + var unusedAttachments: TFile[] = await Util.getUnusedAttachments(this.app, type); var len = unusedAttachments.length; if (len > 0) { let logs = ''; diff --git a/src/util.ts b/src/util.ts index 4ad50ed..9a9a9f7 100644 --- a/src/util.ts +++ b/src/util.ts @@ -8,13 +8,13 @@ const bannerRegex = /!\[\[(.*?)\]\]/i; const imageExtensions: Set = new Set(['jpeg', 'jpg', 'png', 'gif', 'svg', 'bmp']); // Create the List of Unused Images -export const getUnusedAttachments = (app: App, type: 'image' | 'all') => { +export const getUnusedAttachments = async (app: App, type: 'image' | 'all') => { var allAttachmentsInVault: TFile[] = getAttachmentsInVault(app, type); var unusedAttachments: TFile[] = []; var usedAttachmentsSet: Set; // Get Used Attachments in All Markdown Files - usedAttachmentsSet = getAttachmentPathSetForVault(app, type); + usedAttachmentsSet = await getAttachmentPathSetForVault(app, type); // Compare All Attachments vs Used Attachments allAttachmentsInVault.forEach((attachment) => { @@ -29,7 +29,7 @@ const getAttachmentsInVault = (app: App, type: 'image' | 'all'): TFile[] => { let allFiles: TFile[] = app.vault.getFiles(); let attachments: TFile[] = []; for (let i = 0; i < allFiles.length; i++) { - if (allFiles[i].extension !== 'md') { + if (!['md', 'canvas'].includes(allFiles[i].extension)) { // Only images if (imageExtensions.has(allFiles[i].extension.toLowerCase())) { attachments.push(allFiles[i]); @@ -44,7 +44,7 @@ const getAttachmentsInVault = (app: App, type: 'image' | 'all'): TFile[] => { }; // New Method for Getting All Used Attachments -const getAttachmentPathSetForVault = (app: App, type: 'image' | 'all'): Set => { +const getAttachmentPathSetForVault = async (app: App, type: 'image' | 'all'): Promise> => { var attachmentsSet: Set = new Set(); var resolvedLinks = app.metadataCache.resolvedLinks; if (resolvedLinks) { @@ -59,27 +59,44 @@ const getAttachmentPathSetForVault = (app: App, type: 'image' | 'all'): Set { - 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') { - if (frontmatter[k].match(bannerRegex)) { - let fileName = frontmatter[k].match(bannerRegex)[1]; - let file = app.metadataCache.getFirstLinkpathDest(fileName, mdFile.path); - if (file) { - attachmentsSet.add(file.path); + // Loop Files and Check Frontmatter/Canvas + let allFiles = app.vault.getFiles(); + for (let i = 0; i < allFiles.length; i++) { + let obsFile = allFiles[i]; + // Check Frontmatter for md files + if (obsFile.extension === 'md') { + let fileCache = app.metadataCache.getFileCache(obsFile); + if (fileCache.frontmatter) { + let frontmatter = fileCache.frontmatter; + for (let k of Object.keys(frontmatter)) { + if (typeof frontmatter[k] === 'string') { + if (frontmatter[k].match(bannerRegex)) { + let fileName = frontmatter[k].match(bannerRegex)[1]; + let file = app.metadataCache.getFirstLinkpathDest(fileName, obsFile.path); + if (file) { + attachmentsSet.add(file.path); + } + } else if (pathIsAnImage(frontmatter[k])) { + attachmentsSet.add(frontmatter[k]); } - } else if (pathIsAnImage(frontmatter[k])) { - attachmentsSet.add(frontmatter[k]); } } } } - }); + // Check Canvas for links + else if (obsFile.extension === 'canvas') { + let fileRead = await app.vault.cachedRead(obsFile); + let canvasData = JSON.parse(fileRead); + if (canvasData.nodes && canvasData.nodes.length > 0) { + for (const node of canvasData.nodes) { + // node.type: 'text' | 'file' + if (node.type === 'file') { + attachmentsSet.add(node.file); + } + } + } + } + } return attachmentsSet; };