Skip to content

Commit

Permalink
fix: Duplicated file name in FolderTree returns incorrect slug (#64)
Browse files Browse the repository at this point in the history
I fixed bug that routerPath in TreeView returns invalid value when same
filename exists.
```diff
- Before
- posts/fire/post1 -> /fire/post1
- posts/water/post1-> /fire/post1

+ After
+ posts/fire/post1 -> /fire/post1
+ posts/water/post1-> /water/post1
```
  • Loading branch information
turtton authored Mar 3, 2024
1 parent 7530c22 commit 62180c2
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/lib/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ export function convertTreeData(thisObject: DirectoryTree): TreeData {
const children: TreeData[] = [];

const objectName = thisObject.name;
const routerPath = getRouterPath(objectName);
const routerPath = getRouterPath(thisObject.path);
const newObject: TreeData = {
name: objectName.replace(".md", ""),
children,
id: objectName,
id: routerPath ?? objectName,
routePath: routerPath,
};

Expand Down
2 changes: 1 addition & 1 deletion src/lib/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function getSearchIndex(): SearchData[] {
if (content === null || content === undefined) {
break;
}
const path = getRouterPath(`${title}.md`);
const path = getRouterPath(markdownFile);
if (path === null) {
break;
}
Expand Down
9 changes: 4 additions & 5 deletions src/lib/slug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,14 @@ export function getDirectoryData(): TreeData {
return convertTreeData(filteredDirectory);
}

export function getRouterPath(fileName: string): string | null {
export function getRouterPath(path: string): string | null {
const routerPath = getAllSlugs().find((slug) => {
const slugFileName = Transformer.parseFileNameFromPath(toFilePath(slug));
return (
Transformer.normalizeFileName(slugFileName ?? "") ===
Transformer.normalizeFileName(fileName)
Transformer.normalizeFileName(slug ?? "") ===
Transformer.normalizeFileName(toSlug(path))
);
});
const nameAndExtension = fileName.split(".");
const nameAndExtension = path.split(".");
return nameAndExtension.length > 1 && routerPath !== undefined
? routerPath
: null;
Expand Down

0 comments on commit 62180c2

Please sign in to comment.