Skip to content

Commit

Permalink
add post meta of date and word count in seperate lazy component
Browse files Browse the repository at this point in the history
  • Loading branch information
probablyjassin committed Nov 25, 2024
1 parent ad62871 commit 66e4aa5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
25 changes: 25 additions & 0 deletions components/PostMeta.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<template>
<Suspense>
<template #default>
<p class="text-[var(--background-400)]">
{{ page?.date }} · {{ wordCount }} words
</p>
</template>
<template #fallback>
<div class="animate-pulse">
<div class="h-4 bg-[var(--text-200)] rounded w-32"></div>
</div>
</template>
</Suspense>
</template>

<script setup lang="ts">
const { page } = useContent()
const wordCount = computed(() => {
if (!page.value?.body) return 0
const content = JSON.stringify(page.value.body)
return content.split(/\s+/)
.filter(word => /[a-zA-Z0-9]/.test(word))
.length
})
</script>
6 changes: 4 additions & 2 deletions pages/blog/[...slug].vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
<Suspense>
<template #default>
<main>
<p class="mb-[-30px] text-[var(--background-400)]">{{ page?.date }}</p>
<div class="mb-[-30px]">
<LazyPostMeta />
</div>
<ContentLoader />
<hr />
<RelatedPosts v-if="showRelated" />
Expand Down Expand Up @@ -37,7 +39,7 @@ const route = useRoute()
const { data: page } = await useAsyncData(`content-${route.path}`, () => {
return queryContent()
.where({ _path: route.path })
.only(['description', 'tags', 'date'])
.only(['description', 'tags'])
.findOne()
})
Expand Down

0 comments on commit 66e4aa5

Please sign in to comment.