Skip to content

Commit

Permalink
add category function
Browse files Browse the repository at this point in the history
  • Loading branch information
airene committed Apr 14, 2024
1 parent 57daf8e commit bbd8fb3
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions .vitepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default defineConfig({
},
nav: [
{ text: 'Home', link: '/' },
{ text: 'Category', link: '/pages/category' },
{ text: 'Archives', link: '/pages/archives' },
{ text: 'Tags', link: '/pages/tags' },
{ text: 'About', link: '/pages/about' }
Expand Down
31 changes: 31 additions & 0 deletions .vitepress/theme/components/Category.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<template>
<div v-for="(posts, key) in data">
<div class="category">
{{ key }}
</div>
<a :href="withBase(article.regularPath)" v-for="(article, index) in posts" :key="index" class="posts">
<div class="post-container">
<div class="post-dot"></div>
{{ article.frontMatter.title }}
</div>
<div class="date">{{ article.frontMatter.date.slice(5) }}</div>
</a>
</div>
</template>
<script lang="ts" setup>
import { useData, withBase } from 'vitepress'
import { computed } from 'vue'
import { initCategory } from '../functions'
const { theme } = useData()
const data = computed(() => initCategory(theme.value.posts))
</script>

<style scoped>
.category {
padding: 14px 0 8px 0;
font-size: 1.25rem;
font-weight: 500;
font-family: var(--date-font-family);
}
</style>
18 changes: 18 additions & 0 deletions .vitepress/theme/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ type Post = {
frontMatter: {
date: string
title: string
category: string
tags: string[]
description: string
}
Expand All @@ -27,6 +28,23 @@ export function initTags(post: Post[]) {
return data
}

export function initCategory(post: Post[]) {
const data: any = {}
for (let index = 0; index < post.length; index++) {
const element = post[index]
const category = element.frontMatter.category
if (category) {
if (data[category]) {
data[category].push(element)
} else {
data[category] = []
data[category].push(element)
}
}
}
return data
}

export function useYearSort(post: Post[]) {
const data = []
let year = '0'
Expand Down
2 changes: 2 additions & 0 deletions .vitepress/theme/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import DefaultTheme from 'vitepress/theme'

import NewLayout from './components/NewLayout.vue'
import Archives from './components/Archives.vue'
import Category from './components/Category.vue'
import Tags from './components/Tags.vue'
import Page from './components/Page.vue'
import Comment from './components/Comment.vue'
Expand All @@ -14,6 +15,7 @@ export default {
enhanceApp({ app }) {
// register global compoment
app.component('Tags', Tags)
app.component('Category', Category)
app.component('Archives', Archives)
app.component('Page', Page)
app.component('Comment', Comment)
Expand Down
7 changes: 7 additions & 0 deletions pages/category.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
page: true
title: Category
description: Category
aside: false
---
<Category/>
1 change: 1 addition & 0 deletions posts/vitepress-first.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
date: 2021-06-30
title: 一直想找一个系统架构和设计都足够干净的系统
category: 主题
tags:
- vitepress
- markdown
Expand Down

0 comments on commit bbd8fb3

Please sign in to comment.