@@ -52,6 +52,42 @@ export class DataviewSerializerPlugin extends Plugin {
5252 true
5353 ) ;
5454
55+ scheduleForcedUpdate = debounce (
56+ this . processForceUpdateFiles . bind ( this ) ,
57+ MINIMUM_MS_BETWEEN_EVENTS * 20 ,
58+ true
59+ ) ;
60+
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+ } ) ;
89+ }
90+
5591 /**
5692 * Process all the identified recently updated files
5793 */
@@ -94,11 +130,7 @@ export class DataviewSerializerPlugin extends Plugin {
94130 name : 'Scan and serialize all Dataview queries' ,
95131 callback : async ( ) => {
96132 log ( 'Scanning and serializing all Dataview queries' , 'debug' ) ;
97- const allVaultFiles = this . app . vault . getMarkdownFiles ( ) ;
98-
99- for ( const vaultFile of allVaultFiles ) {
100- await this . processFile ( vaultFile ) ;
101- }
133+ this . processForceUpdateFiles ( true ) ;
102134 } ,
103135 } ) ;
104136 }
@@ -142,6 +174,19 @@ export class DataviewSerializerPlugin extends Plugin {
142174 log ( 'The loaded settings miss the [ignoredFolders] property' , 'debug' ) ;
143175 needToSaveSettings = true ;
144176 }
177+ if (
178+ loadedSettings . foldersToForceUpdate !== undefined &&
179+ loadedSettings . foldersToForceUpdate !== null &&
180+ Array . isArray ( loadedSettings . foldersToForceUpdate )
181+ ) {
182+ draft . foldersToForceUpdate = loadedSettings . foldersToForceUpdate ;
183+ } else {
184+ log (
185+ 'The loaded settings miss the [foldersToForceUpdate] property' ,
186+ 'debug'
187+ ) ;
188+ needToSaveSettings = true ;
189+ }
145190 } ) ;
146191
147192 log ( `Settings loaded` , 'debug' , loadedSettings ) ;
@@ -172,20 +217,23 @@ export class DataviewSerializerPlugin extends Plugin {
172217 this . app . vault . on ( 'create' , ( file ) => {
173218 this . recentlyUpdatedFiles . add ( file ) ;
174219 this . scheduleUpdate ( ) ;
220+ this . scheduleForcedUpdate ( ) ;
175221 } )
176222 ) ;
177223
178224 this . registerEvent (
179225 this . app . vault . on ( 'rename' , ( file ) => {
180226 this . recentlyUpdatedFiles . add ( file ) ;
181227 this . scheduleUpdate ( ) ;
228+ this . scheduleForcedUpdate ( ) ;
182229 } )
183230 ) ;
184231
185232 this . registerEvent (
186233 this . app . vault . on ( 'modify' , ( file ) => {
187234 this . recentlyUpdatedFiles . add ( file ) ;
188235 this . scheduleUpdate ( ) ;
236+ this . scheduleForcedUpdate ( ) ;
189237 } )
190238 ) ;
191239 } ) ;
@@ -207,14 +255,18 @@ export class DataviewSerializerPlugin extends Plugin {
207255 try {
208256 //log(`Processing file: ${file.path}`, 'debug');
209257
210- const text = await this . app . vault . cachedRead ( file ) ;
211- const foundQueries : string [ ] = findQueries ( text ) ;
258+ // check cached text for queries
259+ const cachedText = await this . app . vault . cachedRead ( file ) ;
260+ const foundQueries : string [ ] = findQueries ( cachedText ) ;
212261
213262 if ( foundQueries . length === 0 ) {
214263 // No queries to serialize found in the file
215264 return ;
216265 }
217266
267+ // get text from filesystem, per documentation, since we'll likely be changing it
268+ const text = await this . app . vault . read ( file ) ;
269+
218270 // Process the modified file
219271 let updatedText = `${ text } ` ; // To ensure we have access to replaceAll...
220272
@@ -270,6 +322,7 @@ export class DataviewSerializerPlugin extends Plugin {
270322
271323 if ( updatedText !== text ) {
272324 //log('The file content has changed. Saving the modifications', 'info');
325+
273326 await this . app . vault . modify ( file , updatedText ) ;
274327 }
275328 } catch ( e : unknown ) {
0 commit comments