Skip to content

Commit 0d9795f

Browse files
committed
feat: 🚀 configProvider 添加函数注释
1 parent 520c87d commit 0d9795f

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

vitepress-theme-tk/src/configProvider.ts

+34-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import { emptyPost } from "./post/helper";
77

88
export const postsSymbol: InjectionKey<Post> = Symbol("posts");
99

10-
function createConfigProvider(Layout: Component) {
10+
/**
11+
* 创建 Layout 组件
12+
*/
13+
const createConfigProvider = (Layout: Component) => {
1114
return defineComponent({
1215
name: "ConfigProvider",
1316
setup(_, { slots }) {
@@ -23,17 +26,23 @@ function createConfigProvider(Layout: Component) {
2326
return () => h(Layout, null, slots);
2427
},
2528
});
26-
}
29+
};
2730

2831
export const configProvider = (Layout: Component) => {
2932
return createConfigProvider(Layout);
3033
};
3134

35+
/**
36+
* 返回非响应式的 useDate() 对象
37+
*/
3238
export const useUnrefData = () => {
3339
const { theme, frontmatter, site, page } = useData();
3440
return { theme: unref(theme), frontmatter: unref(frontmatter), site: unref(site), page: unref(page) };
3541
};
3642

43+
/**
44+
* 返回全部 Posts 数据
45+
*/
3746
export const useAllPosts = (): Post => {
3847
const { theme } = useData();
3948
const posts = unref(theme).posts;
@@ -43,41 +52,59 @@ export const useAllPosts = (): Post => {
4352
return posts;
4453
};
4554

55+
/**
56+
* 返回 Posts 数据,当处于多语言功能时,返回对应语言的 Posts 数据,否则返回全部 Posts 数据
57+
*/
4658
export const usePosts = (): Post => {
47-
const { theme, localeIndex } = useData();
48-
const posts = unref(theme).posts;
49-
50-
if (!posts) return emptyPost;
59+
const { localeIndex } = useData();
60+
const posts = useAllPosts();
5161

5262
// 兼容多语言功能,先从多语言下获取 posts 数据,获取不到说明没有使用多语言功能,则获取所有 posts 数据。因为多语言可以随时切换,因此使用 computed
5363
return computed(() => posts.locales?.[unref(localeIndex)] || posts);
5464
};
5565

66+
/**
67+
* 是否为首页
68+
*/
5669
export const isHomePage = () => {
5770
const { frontmatter } = useData();
5871
return !isCategoriesPage() && !isTagsPage() && unref(frontmatter).layout === "home";
5972
};
6073

74+
/**
75+
* 是否为分类页
76+
*/
6177
export const isCategoriesPage = () => {
6278
const { frontmatter } = useData();
6379
return unref(frontmatter).categoriesPage;
6480
};
65-
81+
/**
82+
* 是否为标签页
83+
*/
6684
export const isTagsPage = () => {
6785
const { frontmatter } = useData();
6886
return unref(frontmatter).tagsPage;
6987
};
7088

89+
/**
90+
* 是否为归档页
91+
*/
7192
export const isArchivesPage = () => {
7293
const { frontmatter } = useData();
7394
return unref(frontmatter).archivesPage;
7495
};
7596

97+
/**
98+
* 是否为目录页
99+
*/
76100
export const isCataloguePage = () => {
77101
const { frontmatter } = useData();
78102
return unref(frontmatter).catalogue;
79103
};
80104

105+
/**
106+
* 获取默认背景色
107+
*/
81108
export const getBgColor = () => {
82109
const { theme } = useData();
83110
return (

0 commit comments

Comments
 (0)