Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: split Blog template into components #368

Merged
merged 1 commit into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/defaultTheme/components/organisms/BlogpostCard.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<template>
<article class="mb-16 flex flex-col items-end">
<div class="flex flex-col mb-4 w-full">
<!-- date & author -->
<div class="px-4 sm:px-0 flex items-center sm:items-end justify-between mb-4">
<time :datetime="post.date" class="font-medium mr-2 text-sm text-gray-400 dark:text-gray-500">
{{ formatDateByLocale($i18n.locale, post.date) }}
</time>

<div class="flex">
<a
v-for="(author, index) in post.authors"
:key="index"
:href="author.link"
target="_blank"
rel="noopener noindex nofollow"
class="flex items-center justify-end -ml-2 rounded-full border border-gray-300 dark:border-gray-700"
>
<NuxtImg class="inline-block h-8 w-8 rounded-full" height="32" width="32" :src="author.avatarUrl" alt />
</a>
</div>
</div>

<NuxtLink class="w-full hover:opacity-75 transition-opacity duration-100" :to="$contentLocalePath(post.to)">
<div class="aspect-w-16 aspect-h-9 bg-gray-100 dark:bg-gray-800">
<NuxtImg :src="post.imgUrl" width="864" height="378" alt="" />
</div>
</NuxtLink>
</div>

<div class="flex flex-col w-full">
<div class="px-4 sm:px-0">
<NuxtLink
class="hover:text-gray-500 hover:dark:text-gray-400 transition-color duration-100"
:to="$contentLocalePath(post.to)"
>
<h1 class="text-2xl font-semibold mb-2">{{ post.title }}</h1>
</NuxtLink>

<p class="text-gray-600 dark:text-gray-400 mb-4">{{ post.description }}</p>
<NuxtLink
class="font-medium hover:text-gray-500 hover:dark:text-gray-400 transition-color duration-100"
:to="$contentLocalePath(post.to)"
>
<span>Read More →</span>
</NuxtLink>
</div>
</div>
</article>
</template>

<script>
import { defineComponent } from '@nuxtjs/composition-api'

export default defineComponent({
props: {
post: {
type: Object,
required: true
}
},
setup() {
const formatDateByLocale = (locale, d) => {
const currentLocale = locale || 'en'
const options = { year: 'numeric', month: 'long', day: 'numeric' }
return new Date(d).toLocaleDateString(currentLocale, options)
}
return { formatDateByLocale }
}
})
</script>
30 changes: 30 additions & 0 deletions src/defaultTheme/components/organisms/BlogpostList.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<template>
<div>
<BlogpostCard v-for="post in posts" :key="post.slug" :post="post" />
</div>
</template>

<script>
import { defineComponent, ref, useContext, useFetch } from '@nuxtjs/composition-api'

export default defineComponent({
setup() {
const { $docus } = useContext()
const posts = ref()

useFetch(async () => {
const documents = await $docus
.search('/blog', { deep: true })
.where({ slug: { $ne: '' } })
.sortBy('date', 'desc')
.fetch()

posts.value = documents
})

return {
posts
}
}
})
</script>
78 changes: 3 additions & 75 deletions src/defaultTheme/components/templates/Blog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,64 +16,17 @@
dark:text-gray-100
"
>
Blog
{{ page.title }}
</h1>
</div>

<article v-for="post in posts" :key="post.slug" class="mb-16 flex flex-col items-end">
<div class="flex flex-col mb-4 w-full">
<!-- date & author -->
<div class="px-4 sm:px-0 flex items-center sm:items-end justify-between mb-4">
<time :datetime="post.date" class="font-medium mr-2 text-sm text-gray-400 dark:text-gray-500">
{{ formatDateByLocale($i18n.locale, post.date) }}
</time>

<div class="flex">
<a
v-for="(author, index) in post.authors"
:key="index"
:href="author.link"
target="_blank"
rel="noopener noindex nofollow"
class="flex items-center justify-end -ml-2 rounded-full border border-gray-300 dark:border-gray-700"
>
<NuxtImg class="inline-block h-8 w-8 rounded-full" height="32" width="32" :src="author.avatarUrl" alt />
</a>
</div>
</div>

<NuxtLink class="w-full hover:opacity-75 transition-opacity duration-100" :to="$contentLocalePath(post.to)">
<div class="aspect-w-16 aspect-h-9 bg-gray-100 dark:bg-gray-800">
<NuxtImg :src="post.imgUrl" width="864" height="378" alt="" />
</div>
</NuxtLink>
</div>

<div class="flex flex-col w-full">
<div class="px-4 sm:px-0">
<NuxtLink
class="hover:text-gray-500 hover:dark:text-gray-400 transition-color duration-100"
:to="$contentLocalePath(post.to)"
>
<h1 class="text-2xl font-semibold mb-2">{{ post.title }}</h1>
</NuxtLink>

<p class="text-gray-600 dark:text-gray-400 mb-4">{{ post.description }}</p>
<NuxtLink
class="font-medium hover:text-gray-500 hover:dark:text-gray-400 transition-color duration-100"
:to="$contentLocalePath(post.to)"
>
<span>Read More →</span>
</NuxtLink>
</div>
</div>
</article>
<BlogpostList />
</div>
</div>
</template>

<script>
import { defineComponent, ref, useContext, useFetch } from '@nuxtjs/composition-api'
import { defineComponent } from '@nuxtjs/composition-api'

export default defineComponent({
props: {
Expand All @@ -82,31 +35,6 @@ export default defineComponent({
required: true
}
},
setup() {
const { $docus } = useContext()
const posts = ref()

useFetch(async () => {
const documents = await $docus
.search('/blog', { deep: true })
.where({ slug: { $ne: '' } })
.sortBy('date', 'desc')
.fetch()

posts.value = documents
})

const formatDateByLocale = (locale, d) => {
const currentLocale = locale || 'en'
const options = { year: 'numeric', month: 'long', day: 'numeric' }
return new Date(d).toLocaleDateString(currentLocale, options)
}

return {
posts,
formatDateByLocale
}
},
templateOptions: {
aside: false,
fluid: true
Expand Down