Skip to content

Commit

Permalink
feat: Provide user-configuration for folders to update on any modific…
Browse files Browse the repository at this point in the history
…ation

This change provides a user-configuration for folders to "force update" on
any unrelated modification. This is useful for scenarios where files that
contain queries need to be update more frequently. The command palette
will still update all notes as before.
  • Loading branch information
stevenwcarter committed Nov 26, 2024
1 parent f700ff0 commit 5e46231
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 7 deletions.
67 changes: 60 additions & 7 deletions apps/plugin/src/app/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,42 @@ export class DataviewSerializerPlugin extends Plugin {
true
);

scheduleForcedUpdate = debounce(
this.processForceUpdateFiles.bind(this),
MINIMUM_MS_BETWEEN_EVENTS * 20,
true
);

/**
* Process updates for folders which are marked as forced updates.
* These files are updated on any modification, useful for scenarios
* where there's an index file that holds queries that could be impacted
* by file updates elsewhere. This can be run with an empty argument list,
* or with `true` passed as a value to the single optional parameter to
* update all files. The command palette command uses this behavior.
*/
async processForceUpdateFiles(allFiles?: boolean): Promise<void> {
this.app.vault
.getMarkdownFiles()
.filter((file) => {
if (allFiles) {
// Ignore the configuration and update all queries when this parameter is true
return true;
}
let isUpdateable = false;
this.settings.foldersToForceUpdate.forEach((folder) => {
if (file.path.startsWith(folder)) {
isUpdateable = true;
}
});

return isUpdateable;
})
.forEach(async (file) => {
await this.processFile(file);
});
}

/**
* Process all the identified recently updated files
*/
Expand Down Expand Up @@ -94,11 +130,7 @@ export class DataviewSerializerPlugin extends Plugin {
name: 'Scan and serialize all Dataview queries',
callback: async () => {
log('Scanning and serializing all Dataview queries', 'debug');
const allVaultFiles = this.app.vault.getMarkdownFiles();

for (const vaultFile of allVaultFiles) {
await this.processFile(vaultFile);
}
this.processForceUpdateFiles(true);
},
});
}
Expand Down Expand Up @@ -142,6 +174,19 @@ export class DataviewSerializerPlugin extends Plugin {
log('The loaded settings miss the [ignoredFolders] property', 'debug');
needToSaveSettings = true;
}
if (
loadedSettings.foldersToForceUpdate !== undefined &&
loadedSettings.foldersToForceUpdate !== null &&
Array.isArray(loadedSettings.foldersToForceUpdate)
) {
draft.foldersToForceUpdate = loadedSettings.foldersToForceUpdate;
} else {
log(
'The loaded settings miss the [foldersToForceUpdate] property',
'debug'
);
needToSaveSettings = true;
}
});

log(`Settings loaded`, 'debug', loadedSettings);
Expand Down Expand Up @@ -172,20 +217,23 @@ export class DataviewSerializerPlugin extends Plugin {
this.app.vault.on('create', (file) => {
this.recentlyUpdatedFiles.add(file);
this.scheduleUpdate();
this.scheduleForcedUpdate();
})
);

this.registerEvent(
this.app.vault.on('rename', (file) => {
this.recentlyUpdatedFiles.add(file);
this.scheduleUpdate();
this.scheduleForcedUpdate();
})
);

this.registerEvent(
this.app.vault.on('modify', (file) => {
this.recentlyUpdatedFiles.add(file);
this.scheduleUpdate();
this.scheduleForcedUpdate();
})
);
});
Expand All @@ -207,14 +255,18 @@ export class DataviewSerializerPlugin extends Plugin {
try {
//log(`Processing file: ${file.path}`, 'debug');

const text = await this.app.vault.cachedRead(file);
const foundQueries: string[] = findQueries(text);
// check cached text for queries
const cachedText = await this.app.vault.cachedRead(file);
const foundQueries: string[] = findQueries(cachedText);

if (foundQueries.length === 0) {
// No queries to serialize found in the file
return;
}

// get text from filesystem, per documentation, since we'll likely be changing it
const text = await this.app.vault.read(file);

// Process the modified file
let updatedText = `${text}`; // To ensure we have access to replaceAll...

Expand Down Expand Up @@ -270,6 +322,7 @@ export class DataviewSerializerPlugin extends Plugin {

if (updatedText !== text) {
//log('The file content has changed. Saving the modifications', 'info');

await this.app.vault.modify(file, updatedText);
}
} catch (e: unknown) {
Expand Down
18 changes: 18 additions & 0 deletions apps/plugin/src/app/settingTab/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class SettingsTab extends PluginSettingTab {

this.renderFoldersToScan();
this.renderFoldersToIgnore();
this.renderFoldersToForceUpdate();
this.renderFollowButton(containerEl);
this.renderSupportHeader(containerEl);
}
Expand Down Expand Up @@ -84,6 +85,23 @@ export class SettingsTab extends PluginSettingTab {
});
}

renderFoldersToForceUpdate(): void {
this.doSearchAndRemoveList({
currentList: this.plugin.settings.foldersToForceUpdate,
setValue: async (newValue) => {
this.plugin.settings = produce(
this.plugin.settings,
(draft: Draft<PluginSettings>) => {
draft.foldersToForceUpdate = newValue;
}
);
},
name: 'Folders to force update on any modifications',
description:
'Folders to update when any files are modified. Useful to update index files in your vault.',
});
}

doSearchAndRemoveList({
currentList,
setValue,
Expand Down
2 changes: 2 additions & 0 deletions apps/plugin/src/app/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export interface PluginSettings {
foldersToScan: string[];
ignoredFolders: string[];
foldersToForceUpdate: string[];
}

export const DEFAULT_SETTINGS: PluginSettings = {
foldersToScan: [],
ignoredFolders: [],
foldersToForceUpdate: [],
};

0 comments on commit 5e46231

Please sign in to comment.