Files
sori.studio/pages/posts/index.vue

66 lines
1.6 KiB
Vue

<script setup>
const { data: posts } = await useFetch('/api/posts', {
default: () => []
})
const { data: tags } = await useFetch('/api/tags?tagType=all', {
default: () => []
})
/**
* 태그 슬러그로 공개 태그 정보를 찾는다.
* @param {string | undefined} slug - 태그 슬러그
* @returns {{ name: string, slug: string }} 태그 정보
*/
const getTagMeta = (slug) => {
if (!slug) {
return {
name: '',
slug: ''
}
}
const matchedTag = tags.value.find((tag) => tag.slug === slug)
return {
name: matchedTag?.name || String(slug).toUpperCase(),
slug
}
}
const postCards = computed(() => posts.value.map((post) => {
const primaryTag = getTagMeta(post.tags?.[0])
return {
title: post.title,
excerpt: createPostSummary(post.excerpt, post.content, {
maxLength: 320,
appendEllipsis: false
}),
featuredImage: post.featuredImage,
featuredImageThumbnail: post.featuredImageThumbnail || '',
tag: primaryTag.name,
tagSlug: primaryTag.slug,
publishedAt: formatPostDate(post.publishedAt),
to: `/post/${post.slug}`
}
}))
</script>
<template>
<MainColumn>
<section class="posts-index site-section">
<div class="posts-index__header site-section-header">
<p class="posts-index__eyebrow text-xs font-semibold uppercase text-muted">
Posts
</p>
<h1 class="posts-index__title mt-3 text-4xl font-semibold leading-tight">
게시물
</h1>
</div>
</section>
<PostCard v-for="post in postCards" :key="post.to" :post="post" />
</MainColumn>
</template>