Skip to content

Commit a7690bd

Browse files
authored
feat: Tags(#9) (#10)
- blog post tags in frontmatter - filter PostList by tag prop - tags in themeConfig - global TagList and DateTime components - default tag list shows all configured tag links - handle tag casing inconsistency
1 parent 0ade3b0 commit a7690bd

File tree

13 files changed

+139
-31
lines changed

13 files changed

+139
-31
lines changed

example/.vuepress/themeConfig.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ module.exports = {
88
link: 'https://github.com/petedavisdev/vuepress-theme-base/',
99
},
1010
],
11+
tags: [
12+
{ text: 'A tag', link: '/tag/a-tag/' },
13+
{ text: 'Another tag', link: '/tag/another-tag/' },
14+
{ text: 'Taggy McTagface', link: '/tag/taggy-mctagface/' },
15+
],
1116
collections: [
1217
{
1318
directory: 'blog',

example/blog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# Blog
22

3+
Tags:
4+
<TagList />
35
<PostList directory="blog" />

example/blog/a-newer-blog-post.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ title: A newer blog post
33
date: 2020-06-17 19:57:13 UTC
44
image: https://picsum.photos/400/400
55
summary: This is my latest blog post about stuff
6+
tags:
7+
- A tag
8+
- Another tag
9+
- An unlinked tag
610
---
711

812
Lorem ipsum

example/blog/first-blog-post.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ title: First blog post
33
date: 2020-04-15 17:57:13 UTC
44
image: https://picsum.photos/450/450
55
summary: This is my first blog post about stuff
6+
tags:
7+
- A tag
68
---
79

810
Lorem ipsum

example/tag/a-tag.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Blog posts tagged with "A tag"
2+
3+
<PostList directory="blog" tag="A tag" />

example/tag/another-tag.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Blog posts tagged with "another tag"
2+
3+
<PostList directory="blog" tag="another tag" />

example/tag/taggy-mctagface.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Blog posts tagged with "taggy mctagface"
2+
3+
<PostList directory="blog" tag="taggy mctagface" />

theme/components/PostListItem.vue

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
<template>
22
<article class="PostListItem">
33
<h2 class="PostListItem-heading">
4-
<RouterLink class="PostListItem-link" :to="post.path">
5-
{{ post.title }}
6-
</RouterLink>
4+
<RouterLink class="PostListItem-link" :to="post.path">{{
5+
post.title
6+
}}</RouterLink>
77
</h2>
88
<p v-if="post.frontmatter.summary" class="PostListItem-summary">
99
{{ post.frontmatter.summary }}
1010
</p>
11-
<time class="PostListItem-date" :datetime="post.frontmatter.date">
12-
{{ formattedDate }}
13-
</time>
11+
<DateTime :date="post.frontmatter.date" />
12+
<TagList :tags="post.frontmatter.tags || null" />
1413
<img
1514
v-if="post.frontmatter.image"
1615
class="PostListItem-image"
@@ -38,5 +37,15 @@ export default {
3837
})
3938
},
4039
},
40+
methods: {
41+
getTagLink(tag) {
42+
if (this.$site.themeConfig.tags) {
43+
const { link } =
44+
this.$site.themeConfig.tags.find(({ text }) => text === tag) || {}
45+
46+
return link
47+
}
48+
},
49+
},
4150
}
4251
</script>

theme/global-components/DateTime.vue

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<template>
2+
<time class="DateTime" :datetime="datetime">{{ formattedDate }}</time>
3+
</template>
4+
5+
<script>
6+
export default {
7+
props: {
8+
date: String,
9+
},
10+
computed: {
11+
datetime() {
12+
return new Date(this.date)
13+
},
14+
formattedDate() {
15+
return this.datetime.toLocaleString('en-GB', {
16+
month: 'long',
17+
year: 'numeric',
18+
day: 'numeric',
19+
})
20+
},
21+
},
22+
methods: {
23+
getTagLink(tag) {
24+
if (this.$site.themeConfig.tags) {
25+
const { link } =
26+
this.$site.themeConfig.tags.find(({ text }) => text === tag) || {}
27+
28+
return link
29+
}
30+
},
31+
},
32+
}
33+
</script>

theme/global-components/PostList.vue

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<template>
22
<section class="PostList">
33
<PostListItem v-for="post in posts" :key="post.id" :post="post" />
4+
<p v-if="!posts.length">No posts found</p>
45
</section>
56
</template>
67

@@ -13,11 +14,13 @@ export default {
1314
},
1415
props: {
1516
directory: String,
17+
tag: String,
1618
},
1719
computed: {
1820
posts() {
19-
const filteredPosts = this.filterByDirectory(this.$site.pages)
20-
const sortedPosts = this.sortByDate(filteredPosts)
21+
const directoryPosts = this.filterByDirectory(this.$site.pages)
22+
const tagPosts = this.filterByTag(directoryPosts)
23+
const sortedPosts = this.sortByDateDesc(tagPosts)
2124
return sortedPosts
2225
},
2326
},
@@ -37,15 +40,32 @@ export default {
3740
3841
return posts
3942
},
40-
sortByDate(posts) {
41-
return posts.reverse((a, b) => {
43+
filterByTag(posts) {
44+
const tag = this.tag && this.tag.toLowerCase()
45+
46+
if (tag) {
47+
return posts.filter((post) => {
48+
const postTags =
49+
post.frontmatter.tags &&
50+
post.frontmatter.tags.map((tag) => tag.toLowerCase())
51+
52+
if (postTags && postTags.includes(tag)) {
53+
return posts
54+
}
55+
})
56+
}
57+
58+
return posts
59+
},
60+
sortByDateDesc(posts) {
61+
return posts.sort((a, b) => {
4262
const x = a.frontmatter.date.toLowerCase()
4363
const y = b.frontmatter.date.toLowerCase()
4464
if (x < y) {
45-
return -1
65+
return 1
4666
}
4767
if (x > y) {
48-
return 1
68+
return -1
4969
}
5070
return 0
5171
})

0 commit comments

Comments
 (0)