@@ -2,6 +2,7 @@ import { TFile, TFolder, Plugin, Notice, TAbstractFile } from 'obsidian';
22import { RenamedItem } from "src/types/obsidian" ;
33import isItem from './isItem' ;
44
5+
56async function updateParentFrontmatter ( plugin : Plugin , fileItem : RenamedItem < TFile > ) {
67 if ( isItem . isFileCluster ( plugin , fileItem ) === "notTheCluster" ) {
78 try {
@@ -18,21 +19,100 @@ async function updateParentFrontmatter(plugin: Plugin, fileItem: RenamedItem<TFi
1819
1920 }
2021 }
22+ //remove the parent property if there is one because cluster file should'nt have one
23+ if ( isItem . isFileCluster ( plugin , fileItem ) === "theCluster" ) {
24+ try {
25+ await plugin . app . fileManager . processFrontMatter ( fileItem . file , ( frontmatter ) => {
26+ if ( ! frontmatter ) {
27+ console . error ( "No frontmatter found in file:" , fileItem . file . path ) ;
28+ return ;
29+ }
30+ // Update the "parent" property
31+ const parent = frontmatter ?. parent
32+ if ( parent || parent === null || parent instanceof String ) { // parent === null means empty
33+ delete frontmatter . parent
34+ return
35+ }
36+ } ) ;
37+ } catch ( error ) {
38+ console . error ( "Error updating parent frontmatter:" , error ) ;
39+
40+ }
41+ }
42+ }
43+ async function updateGenerationFrontmatter ( plugin : Plugin , fileItem : RenamedItem < TFile > ) {
44+ if ( fileItem . file instanceof TFile ) {
45+ try {
46+ await plugin . app . fileManager . processFrontMatter ( fileItem . file , ( frontmatter ) => {
47+ if ( ! frontmatter ) {
48+ console . error ( "No frontmatter found in file:" , fileItem . file . path ) ;
49+ return ;
50+ }
51+ // Update the "generation" property
52+ const generationFromPath = fileItem . oldPath . split ( '/' ) . length - 2 ;
53+ frontmatter . generation = generationFromPath
54+ } ) ;
55+ } catch ( error ) {
56+ console . error ( "Error updating generation frontmatter:" , error ) ;
57+
58+ }
59+ }
2160}
2261async function updateClusterTagFrontmatter ( plugin : Plugin , fileItem : RenamedItem < TFile > ) {
2362 if ( isItem . isFileCluster ( plugin , fileItem ) === "notTheCluster" ) {
63+
2464 try {
2565 await plugin . app . fileManager . processFrontMatter ( fileItem . file , ( frontmatter ) => {
2666 if ( ! frontmatter ) {
2767 console . error ( "No frontmatter found in file:" , fileItem . file . path ) ;
2868 return ;
2969 }
30- // Update the "cluster tag" property
70+ if ( ! frontmatter . tags ) {
71+ frontmatter . tags = [ ]
72+ }
73+
74+ // remove the "Cluster" itself if there is one because notTheCluster file should'nt have one
75+ const ClusterTagIndex = ( frontmatter . tags as string [ ] ) . findIndex ( tag => tag . contains ( "Cluster" ) ) ;
76+ if ( ClusterTagIndex >= 0 ) {
77+ ( frontmatter . tags as string [ ] ) . splice ( ClusterTagIndex , 1 )
78+ }
79+ // Update the "cluster tag" property that reference the cluster name
3180 let tagName = fileItem . newPath . split ( "/" ) [ 1 ]
3281 tagName = tagName . endsWith ( "-cluster" ) ? tagName : `${ tagName } -cluster`
3382 tagName = tagName . replace ( / / g, "-" )
3483 const clusterTagIndex = ( frontmatter . tags as string [ ] ) . findIndex ( tag => tag . contains ( "-cluster" ) ) ;
35- ( frontmatter . tags as string [ ] ) [ clusterTagIndex ] = tagName
84+ if ( clusterTagIndex < 0 ) {
85+ frontmatter . tags . unshift ( tagName )
86+ return
87+ }
88+ if ( clusterTagIndex >= 0 ) {
89+ ( frontmatter . tags as string [ ] ) . splice ( clusterTagIndex , 1 ) ;
90+ ( frontmatter . tags as string [ ] ) . unshift ( tagName )
91+ return
92+ }
93+
94+ } ) ;
95+
96+ } catch ( error ) {
97+ console . error ( "Error updating -cluster tag frontmatter:" , error ) ;
98+ }
99+ }
100+ if ( isItem . isFileCluster ( plugin , fileItem ) === "theCluster" ) {
101+ try {
102+ await plugin . app . fileManager . processFrontMatter ( fileItem . file , ( frontmatter ) => {
103+ if ( ! frontmatter ) {
104+ console . error ( "No frontmatter found in file:" , fileItem . file . path ) ;
105+ return ;
106+ }
107+ if ( ! frontmatter . tags ) {
108+ frontmatter . tags = [ ]
109+ }
110+
111+ const clusterTagIndex = ( frontmatter . tags as string [ ] ) . findIndex ( tag => tag . contains ( "Cluster" ) ) ;
112+ if ( clusterTagIndex < 0 ) {
113+ frontmatter . tags . unshift ( "Cluster" )
114+ return
115+ }
36116 } ) ;
37117
38118 } catch ( error ) {
@@ -43,13 +123,11 @@ async function updateClusterTagFrontmatter(plugin: Plugin, fileItem: RenamedItem
43123
44124async function renameLinkedFile ( plugin : Plugin , fileItem : RenamedItem < TFolder > ) : Promise < void > {
45125 const selfOldName = fileItem . oldPath . split ( "/" ) . pop ( ) ; //Removes the last element from an array and returns it.
46- console . log ( "selfOldName:" , selfOldName ) ;
47126 const selfNewName = `${ fileItem . newPath } .md`
48127 const siblings = fileItem . file . parent ?. children
49- console . log ( "selfNewName:" , selfNewName ) ;
50128 const isTherelinkedFile = siblings ?. find ( ( item ) => item instanceof TFile && item . basename === selfOldName ) as TFile ;
51129 if ( ! isTherelinkedFile ) {
52- console . log ( "Error: No linked file found." ) ;
130+ // console.log("Error: No linked file found.");
53131 return
54132 }
55133 try {
@@ -63,7 +141,7 @@ async function renameChildrenFolder(plugin: Plugin, fileItem: RenamedItem<TFile>
63141 const siblings = fileItem . file . parent ?. children
64142 const isThereChildrenFolder = siblings ?. find ( ( item ) => item instanceof TFolder && item . name === selfOldName ) as TFolder
65143 if ( ! isThereChildrenFolder ) {
66- console . log ( "Error: No children folder found." ) ;
144+ // console.log("Error: No children folder found.");
67145 return
68146 }
69147 try {
@@ -132,7 +210,8 @@ const doModify = {
132210 updateParentFrontmatter,
133211 updateClusterTagFrontmatter,
134212 renameChildrenFolder,
135- forbidClusterRenaming
213+ forbidClusterRenaming,
214+ updateGenerationFrontmatter
136215 } ,
137216 folder : {
138217 forbidClusterRenaming,
0 commit comments