Skip to content

Commit 9415717

Browse files
committed
feat(tags): finish tags page
1 parent 6697f5f commit 9415717

File tree

5 files changed

+99
-12
lines changed

5 files changed

+99
-12
lines changed

.vitepress/config.theme.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ export default {
3131
text: "SORTS",
3232
link: "/sorts",
3333
},
34-
// {
35-
// text: "TAGS",
36-
// link: "/pages/tags",
37-
// },
34+
{
35+
text: "TAGS",
36+
link: "/tags",
37+
},
3838
// {
3939
// text: "VISIONS",
4040
// link: "/pages/visions",

.vitepress/theme/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Home from "./pages/Home.vue";
55
import Layout from "./pages/Layout.vue";
66
import Posts from "./pages/Posts.vue";
77
import Sorts from "./pages/Sorts.vue";
8+
import Tags from "./pages/Tags.vue";
89

910
export default {
1011
extends: Theme,
@@ -13,5 +14,6 @@ export default {
1314
app.component("Home", Home);
1415
app.component("Posts", Posts);
1516
app.component("Sorts", Sorts);
17+
app.component("Tags", Tags);
1618
},
1719
};

.vitepress/theme/pages/Tags.vue

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<template>
2+
<Container :class="$style.container">
3+
<div :class="$style.main">
4+
<div :class="$style.tagList">
5+
<Badge
6+
v-for="tag of tags"
7+
:key="tag"
8+
:text="tag"
9+
:number="postsByTag[tag].length"
10+
:link="true"
11+
:selected="tag === selectedTag"
12+
@click="selectTag(tag)"
13+
/>
14+
</div>
15+
<PostList
16+
date-format="ll"
17+
:post-list="postsByTag[selectedTag]"
18+
/>
19+
</div>
20+
</Container>
21+
</template>
22+
23+
<script lang="ts" setup>
24+
import { ref, onMounted } from "vue";
25+
import { data as allPosts } from "../posts.data";
26+
import { toDashedHash } from "../utils";
27+
import Badge from "../components/Badge.vue";
28+
import Container from "../components/Container.vue";
29+
import PostList from "../components/PostList.vue";
30+
31+
const postsByTag = {};
32+
allPosts.forEach(post => {
33+
post.frontmatter.tags.forEach(tag => {
34+
postsByTag[tag] = postsByTag[tag] || [];
35+
postsByTag[tag].push(post);
36+
});
37+
});
38+
39+
const ALL = "All";
40+
postsByTag[ALL] = allPosts;
41+
42+
const tags = Object.keys(postsByTag).sort((a, b) => {
43+
if (postsByTag[a].length == postsByTag[b].length) {
44+
return a.localeCompare(b);
45+
}
46+
return postsByTag[b].length - postsByTag[a].length;
47+
});
48+
49+
const hashToTag = {};
50+
Object.keys(postsByTag).forEach(tag => {
51+
hashToTag[toDashedHash(tag)] = tag;
52+
});
53+
54+
const selectedTag = ref();
55+
56+
function selectTag(tag: string) {
57+
window.location.hash = `#${toDashedHash(tag)}`;
58+
selectedTag.value = tag;
59+
}
60+
61+
onMounted(() => {
62+
const defaultHash = window.location.hash?.slice(1);
63+
selectedTag.value = hashToTag[defaultHash] || ALL;
64+
65+
selectTag(selectedTag.value);
66+
});
67+
</script>
68+
69+
<style module scoped>
70+
.container {
71+
display: block;
72+
}
73+
74+
.main {
75+
margin: 0 auto;
76+
max-width: 42rem;
77+
padding: 0 0 2rem;
78+
}
79+
80+
.tagList {
81+
padding-bottom: 1rem;
82+
border-bottom: 1px solid var(--vp-c-divider);
83+
margin-bottom: 2rem;
84+
}
85+
</style>

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ This blog is powered by [VitePress](https://vitepress.dev/) with a customized th
2222
- [x] Prev/Next (without the built-in global sidebar)
2323
- [x] Comments ([Giscus](https://github.com/giscus/giscus))
2424
- [x] Sort (Category)
25-
- [ ] Tags
25+
- [x] Tags
2626
- [x] Home
2727
- [x] Profile Card
2828
- Avatar
@@ -33,7 +33,7 @@ This blog is powered by [VitePress](https://vitepress.dev/) with a customized th
3333
- [x] Paged Post List
3434
- [x] Posts (Archives)
3535
- [x] Sorts (Categories)
36-
- [ ] Tags
36+
- [x] Tags
3737
- [ ] Atom Feed
3838
- [x] Others
3939
- [x] Local Search (supports Chinese)
@@ -47,7 +47,7 @@ Feel free to customize it based on your own ideas. Please check out the [Contrib
4747

4848
## License
4949

50-
Copyright (c) 2019-present. Shaobiao Lin.
50+
Copyright ©️ 2019-present. Shaobiao Lin.
5151

52-
- Contents in directories `posts/`, `visions/`, and `public/` are licensed under [CC BY-SA 4.0](http://creativecommons.org/licenses/by-sa/4.0/).
52+
- Contents in directories `posts/`, `public/`, and `visions/` are licensed under [CC BY-SA 4.0](http://creativecommons.org/licenses/by-sa/4.0/).
5353
- Others are licensed under [MIT](./LICENSE).

pages/tags.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
2-
# layout: page
2+
title: Tags
3+
description: Posts Tags
4+
layout: page
35
---
46

5-
# Tags
6-
7-
👷 🚧
7+
<Tags />

0 commit comments

Comments
 (0)