11<script setup lang="ts">
22import { getRelativeLocaleUrl } from " astro:i18n"
3+ import type { CollectionEntry } from " astro:content"
34import { getLangFromUrl , useTranslations } from " @/i18n/utils"
45import { computed } from " vue"
6+ import { getPostUrlParts , postReadableDate } from " @/ts/functions"
57
68const 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
3114const lang = getLangFromUrl (props .url )
3215const 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
3823const 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
4829function 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 >
0 commit comments