@@ -250,92 +250,39 @@ export async function merge_front_matter(
250250 return ;
251251 }
252252 try {
253- await Promise . all (
254- Object . keys ( properties ) . map ( async ( prop ) => {
255- const value = properties [ prop ] ;
256- if ( app . metadataCache . getFileCache ( file ) ?. frontmatter == null ) {
257- // console.log("Frontmatter is empty");
258- await app . fileManager . processFrontMatter (
259- file ,
260- ( frontmatter ) => {
261- // console.log(
262- // `adding new property: ${prop} to ${file.basename} with value: ${value}`
263- // );
264- frontmatter [ prop ] = value ;
265- }
266- ) ;
253+ await app . fileManager . processFrontMatter ( file , ( frontmatter ) => {
254+ for ( const prop in properties ) {
255+ const currentValue = frontmatter [ prop ] ;
256+ const newValue = properties [ prop ] ;
257+
258+ if ( currentValue === undefined ) {
259+ // If the property doesn't exist, add it
260+ frontmatter [ prop ] = newValue ;
267261 } else if (
268- app . metadataCache
269- . getFileCache ( file )
270- ?. frontmatter ?. hasOwnProperty ( prop )
262+ Array . isArray ( currentValue ) ||
263+ Array . isArray ( newValue )
271264 ) {
272- // console.log(`${file.basename} contains property: ${prop}`);
273- const originalValue =
274- app . metadataCache . getFileCache ( file ) ?. frontmatter ?. [
275- prop
276- ] ;
277- if (
278- value != null &&
279- ( Array . isArray ( originalValue ) || Array . isArray ( value ) )
280- ) {
281- // console.log(`${prop} is an array`);
282- await app . fileManager . processFrontMatter (
283- file ,
284- ( frontmatter ) => {
285- if ( ! Array . isArray ( originalValue ) ) {
286- // console.log(
287- // `converting ${prop} to an array in ${file.basename}`
288- // );
289- frontmatter [ prop ] = [ originalValue ] ;
290- }
291- if ( Array . isArray ( value ) ) {
292- for ( let i = 0 ; i < value . length ; i ++ ) {
293- // console.log(
294- // `adding ${value[i]} to ${prop} in ${file.basename}`
295- // );
296- if (
297- ! frontmatter [ prop ] . includes (
298- value [ i ]
299- )
300- ) {
301- frontmatter [ prop ] . push ( value [ i ] ) ;
302- }
303- }
304- } else {
305- // console.log(
306- // `adding ${value} to ${prop} in ${file.basename}`
307- // );
308- if ( ! frontmatter [ prop ] . includes ( value ) ) {
309- frontmatter [ prop ] . push ( value ) ;
310- }
311- }
312- }
313- ) ;
314- } else if ( originalValue !== value && value != null ) {
315- // console.log(
316- // `updating property: ${prop} in ${file.basename} from ${originalValue} to ${value}`
317- // );
318- await app . fileManager . processFrontMatter (
319- file ,
320- ( frontmatter ) => {
321- frontmatter [ prop ] = value ;
322- }
323- ) ;
324- }
325- } else {
326- // console.log(`${file.basename} doesn't contain ${prop}`);
327- // console.log(
328- // `adding property: ${prop} to ${file.basename} with value: ${value}`
329- // );
330- await app . fileManager . processFrontMatter (
331- file ,
332- ( frontmatter ) => {
333- frontmatter [ prop ] = value ;
334- }
265+ // If either is an array, merge them
266+ frontmatter [ prop ] = Array . from (
267+ new Set ( [
268+ ...( currentValue
269+ ? Array . isArray ( currentValue )
270+ ? currentValue
271+ : [ currentValue ]
272+ : [ ] ) ,
273+ ...( newValue
274+ ? Array . isArray ( newValue )
275+ ? newValue
276+ : [ newValue ]
277+ : [ ] ) ,
278+ ] )
335279 ) ;
280+ } else if ( newValue !== currentValue && newValue != null ) {
281+ // If they are different, update the value
282+ frontmatter [ prop ] = newValue ;
336283 }
337- } )
338- ) ;
284+ }
285+ } ) ;
339286 } catch ( error ) {
340287 console . error ( "Error in processing frontmatter: " , error ) ;
341288 }
0 commit comments