Skip to content

Commit 21a961f

Browse files
committed
feat: Provide user-configuration for folders to update on any modification
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.
1 parent dbf8f79 commit 21a961f

File tree

3 files changed

+70
-14
lines changed

3 files changed

+70
-14
lines changed

apps/plugin/src/app/plugin.ts

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,40 @@ export class DataviewSerializerPlugin extends Plugin {
5252
true
5353
);
5454

55-
scheduleFullUpdate = debounce(
56-
this.processAllFiles.bind(this),
55+
scheduleForcedUpdate = debounce(
56+
this.processForceUpdateFiles.bind(this),
5757
MINIMUM_MS_BETWEEN_EVENTS * 20,
5858
true
5959
);
6060

61-
async processAllFiles(): Promise<void> {
62-
this.app.vault.getMarkdownFiles().forEach((file) => {
63-
this.processFile(file);
64-
});
61+
/**
62+
* Process updates for folders which are marked as forced updates.
63+
* These files are updated on any modification, useful for scenarios
64+
* where there's an index file that holds queries that could be impacted
65+
* by file updates elsewhere. This can be run with an empty argument list,
66+
* or with `true` passed as a value to the single optional parameter to
67+
* update all files. The command palette command uses this behavior.
68+
*/
69+
async processForceUpdateFiles(allFiles?: boolean): Promise<void> {
70+
this.app.vault
71+
.getMarkdownFiles()
72+
.filter((file) => {
73+
if (allFiles) {
74+
// Ignore the configuration and update all queries when this parameter is true
75+
return true;
76+
}
77+
let isUpdateable = false;
78+
this.settings.foldersToForceUpdate.forEach((folder) => {
79+
if (file.path.startsWith(folder)) {
80+
isUpdateable = true;
81+
}
82+
});
83+
84+
return isUpdateable;
85+
})
86+
.forEach(async (file) => {
87+
await this.processFile(file);
88+
});
6589
}
6690
/**
6791
* Process all the identified recently updated files
@@ -105,11 +129,7 @@ export class DataviewSerializerPlugin extends Plugin {
105129
name: 'Scan and serialize all Dataview queries',
106130
callback: async () => {
107131
log('Scanning and serializing all Dataview queries', 'debug');
108-
const allVaultFiles = this.app.vault.getMarkdownFiles();
109-
110-
for (const vaultFile of allVaultFiles) {
111-
await this.processFile(vaultFile);
112-
}
132+
this.processForceUpdateFiles(true);
113133
},
114134
});
115135
}
@@ -153,6 +173,19 @@ export class DataviewSerializerPlugin extends Plugin {
153173
log('The loaded settings miss the [ignoredFolders] property', 'debug');
154174
needToSaveSettings = true;
155175
}
176+
if (
177+
loadedSettings.foldersToForceUpdate !== undefined &&
178+
loadedSettings.foldersToForceUpdate !== null &&
179+
Array.isArray(loadedSettings.foldersToForceUpdate)
180+
) {
181+
draft.foldersToForceUpdate = loadedSettings.foldersToForceUpdate;
182+
} else {
183+
log(
184+
'The loaded settings miss the [foldersToForceUpdate] property',
185+
'debug'
186+
);
187+
needToSaveSettings = true;
188+
}
156189
});
157190

158191
log(`Settings loaded`, 'debug', loadedSettings);
@@ -183,23 +216,23 @@ export class DataviewSerializerPlugin extends Plugin {
183216
this.app.vault.on('create', (file) => {
184217
this.recentlyUpdatedFiles.add(file);
185218
this.scheduleUpdate();
186-
this.scheduleFullUpdate();
219+
this.scheduleForcedUpdate();
187220
})
188221
);
189222

190223
this.registerEvent(
191224
this.app.vault.on('rename', (file) => {
192225
this.recentlyUpdatedFiles.add(file);
193226
this.scheduleUpdate();
194-
this.scheduleFullUpdate();
227+
this.scheduleForcedUpdate();
195228
})
196229
);
197230

198231
this.registerEvent(
199232
this.app.vault.on('modify', (file) => {
200233
this.recentlyUpdatedFiles.add(file);
201234
this.scheduleUpdate();
202-
this.scheduleFullUpdate();
235+
this.scheduleForcedUpdate();
203236
})
204237
);
205238
});
@@ -221,6 +254,7 @@ export class DataviewSerializerPlugin extends Plugin {
221254
try {
222255
//log(`Processing file: ${file.path}`, 'debug');
223256

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

@@ -229,6 +263,7 @@ export class DataviewSerializerPlugin extends Plugin {
229263
return;
230264
}
231265

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

234269
// Process the modified file
@@ -286,6 +321,7 @@ export class DataviewSerializerPlugin extends Plugin {
286321

287322
if (updatedText !== text) {
288323
//log('The file content has changed. Saving the modifications', 'info');
324+
289325
await this.app.vault.modify(file, updatedText);
290326
}
291327
} catch (e: unknown) {

apps/plugin/src/app/settingTab/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export class SettingsTab extends PluginSettingTab {
2121

2222
this.renderFoldersToScan();
2323
this.renderFoldersToIgnore();
24+
this.renderFoldersToForceUpdate();
2425
this.renderFollowButton(containerEl);
2526
this.renderSupportHeader(containerEl);
2627
}
@@ -84,6 +85,23 @@ export class SettingsTab extends PluginSettingTab {
8485
});
8586
}
8687

88+
renderFoldersToForceUpdate(): void {
89+
this.doSearchAndRemoveList({
90+
currentList: this.plugin.settings.foldersToForceUpdate,
91+
setValue: async (newValue) => {
92+
this.plugin.settings = produce(
93+
this.plugin.settings,
94+
(draft: Draft<PluginSettings>) => {
95+
draft.foldersToForceUpdate = newValue;
96+
}
97+
);
98+
},
99+
name: 'Folders to force update on any modifications',
100+
description:
101+
'Folders to update when any files are modified. Useful to update index files in your vault.',
102+
});
103+
}
104+
87105
doSearchAndRemoveList({
88106
currentList,
89107
setValue,

apps/plugin/src/app/types/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
export interface PluginSettings {
22
foldersToScan: string[];
33
ignoredFolders: string[];
4+
foldersToForceUpdate: string[];
45
}
56

67
export const DEFAULT_SETTINGS: PluginSettings = {
78
foldersToScan: [],
89
ignoredFolders: [],
10+
foldersToForceUpdate: [],
911
};

0 commit comments

Comments
 (0)