|
| 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> |
0 commit comments