Skip to content

Commit 0f4d250

Browse files
fix : and add a lot for frontmatter properties
1 parent da333ea commit 0f4d250

File tree

4 files changed

+109
-26
lines changed

4 files changed

+109
-26
lines changed

src/renamer/checkRenamedFileItemType.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export function checkRenamedFileItemType(plugin: Plugin, fileItem: RenamedItem<T
1111
[isItem.isFileHasChildren(fileItem as RenamedItem<TFile>) === "alone", "the file has children"],
1212
[isItem.isFileCluster(plugin, fileItem as RenamedItem<TFile>) === "notTheCluster", "the file is cluster"],
1313
])
14+
1415
if (check1 === true) {
1516
return 'file:alone:notTheCluster';
1617
}
@@ -48,6 +49,7 @@ export function checkRenamedFileItemType(plugin: Plugin, fileItem: RenamedItem<T
4849
[isItem.isFolderCluster(fileItem as RenamedItem<TFolder>) === "notTheCluster", "the folder is cluster"],
4950
[isItem.whatClusteringState(fileItem as RenamedItem<TFolder>) === "unLinked", "the folder linked"],
5051
])
52+
5153
if (check5 === true) {
5254
return 'folder:notTheCluster:unLinked';
5355
}
@@ -78,4 +80,13 @@ export function checkRenamedFileItemType(plugin: Plugin, fileItem: RenamedItem<T
7880
if (check8 === true) {
7981
return 'folder:theCluster:linked';
8082
}
83+
84+
const undefinedFileStatus = {
85+
type: fileItem.file instanceof TFile ? "file" : "folder",
86+
isClusterIfFile: isItem.isFileCluster(plugin, fileItem as RenamedItem<TFile>),
87+
hasChildrenIfFile: isItem.isFileHasChildren(fileItem as RenamedItem<TFile>),
88+
clusteringStateIfFolder: isItem.whatClusteringState(fileItem as RenamedItem<TFolder>),
89+
isClusterIfFolder: isItem.isFolderCluster(fileItem as RenamedItem<TFolder>),
90+
}
91+
return undefinedFileStatus
8192
}

src/renamer/doModify.ts

Lines changed: 86 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { TFile, TFolder, Plugin, Notice, TAbstractFile } from 'obsidian';
22
import { RenamedItem } from "src/types/obsidian";
33
import isItem from './isItem';
44

5+
56
async 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
}
2261
async 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

44124
async 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,

src/renamer/isItem.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,21 @@ function isFileCluster(plugin: Plugin, fileItem: RenamedItem<TFile>): "theCluste
1717
if (fileItem.file instanceof TFolder) {
1818
return
1919
}
20-
const frontmatter = plugin.app.metadataCache.getFileCache(fileItem.file)?.frontmatter;
21-
const theClusterTag = (frontmatter?.tags as string[])?.find(tag => tag === "Cluster");
22-
const clusterTag = (frontmatter?.tags as string[])?.find(tag => tag.contains("-cluster"));
23-
const generationFromFrontmatter = frontmatter?.generation
20+
2421
const generationFromPath = fileItem.oldPath.split('/').length - 2;
25-
const parent = frontmatter?.parent
2622
const oldName = fileItem.oldPath.split("/")[1].slice(0, -3);
27-
const newName = fileItem.newPath.split("/")[1].slice(0, -3);
2823
const theClusterResult = U.IF([
2924
[fileItem.file instanceof TFile, "its is folder"],
3025
[oldName.endsWith("-cluster"), "old name didn't ends with -cluster"],
31-
[generationFromFrontmatter === 0, "cluster generation must be 0 "],
3226
[generationFromPath === 0, "cluster generation must be 0 "],
33-
[generationFromPath === generationFromFrontmatter, "generationFromFrontmatter must equal generationFromPath "],
34-
[theClusterTag === "Cluster", "there is no Cluster tag"],
35-
[!clusterTag, `${clusterTag} forbidden`],
36-
[!parent, `the parent property ${parent} forbidden`],
3727
])
3828
if (theClusterResult === true) {
3929
return "theCluster"
4030
}
4131
const notTheClusterResult = U.IF([
4232
[fileItem.file instanceof TFile, "its is folder"],
4333
[!(oldName.endsWith("-cluster")), "old name ends with -cluster"],
44-
[!(generationFromFrontmatter === 0), "generation must be bigger than 0 "],
4534
[!(generationFromPath === 0), "generation must be bigger than 0 "],
46-
[generationFromPath === generationFromFrontmatter, "generationFromFrontmatter must equal generationFromPath "],
47-
[!(theClusterTag === "Cluster"), "Cluster tag forbidden"],
48-
[typeof clusterTag === 'string' && clusterTag.contains("-cluster"), `${clusterTag} is missing`],
49-
[parent, `the parent property ${parent} forbidden`],
5035
])
5136
if (notTheClusterResult === true) {
5237
return "notTheCluster"
@@ -60,7 +45,6 @@ function isFolderCluster(fileItem: RenamedItem<TFolder>): "theCluster" | "notThe
6045

6146
const generationFromPath = fileItem.oldPath.split('/').length - 2;
6247
const oldName = fileItem.oldPath.split("/")[1];
63-
const newName = fileItem.newPath.split("/")[1];
6448
const theClusterResult = U.IF([
6549
[fileItem.file instanceof TFolder, "its is file"],
6650
[oldName.endsWith("-cluster"), "old name didn't ends with -cluster"],

src/renamer/renamer.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export async function renamer(plugin: Plugin, fileItem: RenamedItem<TAbstractFil
1414
console.log(fileItem.file.name + ": undefined -->", renamedFileItemType);
1515
return
1616
}
17-
console.log(fileItem.file.name + "-->", renamedFileItemType);
17+
// console.log(fileItem.file.name + "-->", renamedFileItemType);
1818

1919
//do rename
2020
await rename(plugin, fileItem, renamedFileItemType)
@@ -29,13 +29,17 @@ async function rename(plugin: Plugin, fileItem: RenamedItem<TAbstractFile>, type
2929
if (!forbid) {
3030
await doModify.file.updateClusterTagFrontmatter(plugin, fileItem as RenamedItem<TFile>)
3131
await doModify.file.updateParentFrontmatter(plugin, fileItem as RenamedItem<TFile>)
32+
await doModify.file.updateGenerationFrontmatter(plugin, fileItem as RenamedItem<TFile>)
3233
}
3334
}
3435
// file:alone:theCluster
3536
if (type === "file:alone:theCluster") {
3637
const forbid = await doModify.file.forbidClusterRenaming(plugin, fileItem as RenamedItem<TFile>)
3738
if (!forbid) {
38-
// Add any additional logic if needed
39+
await doModify.file.updateClusterTagFrontmatter(plugin, fileItem as RenamedItem<TFile>)
40+
await doModify.file.updateGenerationFrontmatter(plugin, fileItem as RenamedItem<TFile>)
41+
// remove parent if there is one need time to work as expected
42+
setTimeout(async () => { await doModify.file.updateParentFrontmatter(plugin, fileItem as RenamedItem<TFile>) }, 100)
3943
}
4044
}
4145
// file:hasChildren:notTheCluster
@@ -44,6 +48,7 @@ async function rename(plugin: Plugin, fileItem: RenamedItem<TAbstractFile>, type
4448
if (!forbid) {
4549
await doModify.file.updateClusterTagFrontmatter(plugin, fileItem as RenamedItem<TFile>)
4650
await doModify.file.updateParentFrontmatter(plugin, fileItem as RenamedItem<TFile>)
51+
await doModify.file.updateGenerationFrontmatter(plugin, fileItem as RenamedItem<TFile>)
4752
await doModify.file.renameChildrenFolder(plugin, fileItem as RenamedItem<TFile>)
4853
}
4954
}
@@ -52,6 +57,10 @@ async function rename(plugin: Plugin, fileItem: RenamedItem<TAbstractFile>, type
5257
const forbid = await doModify.file.forbidClusterRenaming(plugin, fileItem as RenamedItem<TFile>)
5358
if (!forbid) {
5459
await doModify.file.renameChildrenFolder(plugin, fileItem as RenamedItem<TFile>)
60+
await doModify.file.updateClusterTagFrontmatter(plugin, fileItem as RenamedItem<TFile>)
61+
await doModify.file.updateGenerationFrontmatter(plugin, fileItem as RenamedItem<TFile>)
62+
// remove parent if there is one need time to work as expected
63+
setTimeout(async () => { await doModify.file.updateParentFrontmatter(plugin, fileItem as RenamedItem<TFile>) }, 100)
5564
}
5665
}
5766
//= folders

0 commit comments

Comments
 (0)