Skip to content

Commit 2fd29b3

Browse files
authored
Fix/post excerpt (#72)
* fix: post excerpt * chore: simplify component * chore: rename function
1 parent be1eae4 commit 2fd29b3

File tree

5 files changed

+19
-59
lines changed

5 files changed

+19
-59
lines changed

src/components/PostExcerpt.vue

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,33 @@
11
<script setup lang="ts">
22
import { getRelativeLocaleUrl } from "astro:i18n"
3+
import type { CollectionEntry } from "astro:content"
34
import { getLangFromUrl, useTranslations } from "@/i18n/utils"
45
import { computed } from "vue"
6+
import { getPostUrlParts, postReadableDate } from "@/ts/functions"
57
68
const props = defineProps<{
79
url: URL
8-
author?: {
9-
data: {
10-
firstName: string
11-
}
12-
}
13-
post: {
14-
id: string
15-
data: {
16-
title: string
17-
date: Date
18-
lastModification?: Date
19-
author: {
20-
id: string
21-
}
22-
image?: unknown
23-
}
24-
body?: string
25-
rendered?: {
26-
html: string
27-
}
28-
}
10+
author?: CollectionEntry<"authors">
11+
post: CollectionEntry<"posts">
2912
}>()
3013
3114
const lang = getLangFromUrl(props.url)
3215
const t = useTranslations(lang)
16+
const KEEP_READING_FLAG = `<!-- ${t("Seguir leyendo")} -->`
3317
34-
const excerpt = props.post.rendered?.html.split(
35-
`<!-- ${t("Seguir leyendo")} -->`,
36-
)[0]
18+
const excerpt = props.post.rendered?.html.split(KEEP_READING_FLAG)[0]
19+
20+
const hasExcerpt =
21+
props.post.rendered?.html.includes(KEEP_READING_FLAG) ?? false
3722
3823
const href = computed(() => {
39-
const year = props.post.data.date.getFullYear()
40-
const month = (props.post.data.date.getMonth() + 1)
41-
.toString()
42-
.padStart(2, "0")
43-
const slug = props.post.id.split(/\d{4}-\d{2}-\d{2}-/)[1]
24+
const [year, month, slug] = getPostUrlParts(props.post)
4425
4526
return getRelativeLocaleUrl(lang, `/blog/${year}/${month}/${slug}`)
4627
})
4728
4829
function formatDate(date: Date) {
49-
const dateParts = new Intl.DateTimeFormat(lang, {
50-
month: "long",
51-
day: "2-digit",
52-
year: "numeric",
53-
}).formatToParts(date)
54-
55-
const month = dateParts.find(({ type }) => type === "month")?.value
56-
const day = dateParts.find(({ type }) => type === "day")?.value
57-
const year = dateParts.find(({ type }) => type === "year")?.value
58-
59-
return `${month} ${day}, ${year}`
30+
return postReadableDate(lang, date)
6031
}
6132
</script>
6233

@@ -71,7 +42,7 @@ function formatDate(date: Date) {
7142
<div class="flex gap-2 justify-between">
7243
<div class="flex-1 basis-3/4">
7344
<div class="flex-1 content" v-html="excerpt"></div>
74-
<p>
45+
<p v-if="hasExcerpt">
7546
...
7647
<br />
7748
<a :href>{{ t("Seguir leyendo") }}</a>

src/components/PostTranslationLink.astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { getEntry } from "astro:content"
33
import type { CollectionEntry } from "astro:content"
44
import { getAbsoluteLocaleUrl } from "astro:i18n"
5-
import { getPostUrl } from "@/ts/functions"
5+
import { getPostUrlParts } from "@/ts/functions"
66
import { displayLanguages } from "@/i18n/ui"
77
88
type Translation = NonNullable<
@@ -26,7 +26,7 @@ const translationLabel = displayLanguages[translation.short]
2626
class="capitalize"
2727
href={getAbsoluteLocaleUrl(
2828
translation.short,
29-
"/blog/" + getPostUrl(post).join("/"),
29+
"/blog/" + getPostUrlParts(post).join("/"),
3030
)}
3131
>
3232
{translationLabel}

src/pages/blog/[...slug].astro

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,16 @@ import { getCollection, getEntry, render } from "astro:content"
44
import { Image } from "astro:assets"
55
import HtmlLayout from "@/layouts/html.astro"
66
import { getLangFromUrl, useTranslations } from "@/i18n/utils"
7-
import { postReadableDate } from "@/ts/functions"
7+
import { getPostUrlParts, postReadableDate } from "@/ts/functions"
88
import PostTag from "@/components/PostTag.vue"
99
import PostReactions from "@/components/PostReactions.vue"
1010
import PostTranslationLink from "@/components/PostTranslationLink.astro"
1111
1212
export const getStaticPaths = (async () => {
1313
const posts = await getCollection("posts", (post) => !post.id.includes("/"))
1414
15-
function getPostUrl(post: {
16-
id: string
17-
data: { date: Date }
18-
}): [string, string, string] {
19-
const year = post.data.date.getFullYear().toString()
20-
const month = (post.data.date.getMonth() + 1).toString().padStart(2, "0")
21-
const slug = post.id.replace(/\d{4}-\d{2}-\d{2}-/, "")
22-
23-
return [year, month, slug]
24-
}
25-
2615
return posts.map((post) => ({
27-
params: { slug: getPostUrl(post).join("/") },
16+
params: { slug: getPostUrlParts(post).join("/") },
2817
props: { post },
2918
}))
3019
}) satisfies GetStaticPaths

src/pages/en/blog/[...slug].astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import type { GetStaticPaths } from "astro"
33
import { getCollection } from "astro:content"
44
import BlogPost from "@/pages/blog/[...slug].astro"
5-
import { getPostUrl } from "@/ts/functions"
5+
import { getPostUrlParts } from "@/ts/functions"
66
77
export const getStaticPaths = (async () => {
88
const posts = await getCollection(
@@ -11,7 +11,7 @@ export const getStaticPaths = (async () => {
1111
)
1212
1313
return posts.map((post) => ({
14-
params: { slug: getPostUrl(post).join("/") },
14+
params: { slug: getPostUrlParts(post).join("/") },
1515
props: { post },
1616
}))
1717
}) satisfies GetStaticPaths

src/ts/functions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function postReadableDate(lang: typeof languages[number], date: Date) {
3636
return `${month} ${day}, ${year}`
3737
}
3838

39-
export function getPostUrl(post: {
39+
export function getPostUrlParts(post: {
4040
id: string
4141
data: { date: Date }
4242
}): [string, string, string] {

0 commit comments

Comments
 (0)