-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: useRouteMeta & useTabMeta with getRouteMetaById (#1888)
* refactor: useRouteMeta & useTabMeta * fix: route meta * refactor: fix sidebar * chore: code clean
- Loading branch information
Showing
5 changed files
with
81 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { tabs } from './tabs'; | ||
|
||
const files = { | ||
{{#metaFiles}} | ||
'{{{id}}}': { | ||
frontmatterGetter: () => import('{{{file}}}?type=frontmatter'), | ||
textGetter: () => import('{{{file}}}?type=text'), | ||
{{#tabs}} | ||
tabs: {{{tabs}}}, | ||
{{/tabs}} | ||
}, | ||
{{/metaFiles}} | ||
}; | ||
|
||
export const getRouteMetaById = async (id: string) => { | ||
const file = files[id]; | ||
if (!file) { | ||
return null; | ||
} | ||
|
||
const text = await file.textGetter(); | ||
const frontmatter = await file.frontmatterGetter(); | ||
|
||
const tabsMeta = file.tabs && await Promise.all(file.tabs.map(async (tab) => { | ||
const meta = await getRouteMetaById(tab) ?? { | ||
frontmatter: { title: tabs[tab].title }, | ||
toc: [], | ||
texts: [], | ||
}; | ||
return { | ||
...tabs[tab], | ||
meta, | ||
} | ||
})); | ||
|
||
return { | ||
texts: text?.texts, | ||
toc: text?.toc, | ||
frontmatter: frontmatter?.frontmatter, | ||
tabs: tabsMeta, | ||
}; | ||
} |