/
[slug].astro
139 lines (111 loc) · 3.23 KB
/
[slug].astro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
---
import Meta from "@components/Meta.astro";
import Navbar from "@components/Navbar.astro";
import type { OpenGraph } from "@components/opengraph";
import Pagination from "@components/Pagination.astro";
import TableOfContents from "@components/TableOfContents.vue";
import Tags from "@components/Tags.vue";
import TimeSince from "@components/TimeSince.vue";
import DefaultLayout from "@layouts/Default.astro";
import { CollectionEntry, getCollection } from "astro:content";
const { post } = Astro.props;
export async function getStaticPaths({}) {
const posts = await getCollection("articles");
const data = posts.map((post) => {
return {
params: {
slug: post.slug,
},
props: { post },
};
});
return data;
}
interface Props {
post: CollectionEntry<"articles">;
}
const { title, description, published, tags } = post.data;
const { Content, headings } = await post.render();
const openGraph: OpenGraph = {
title: title,
description: description,
type: 'article',
image: {
url: '',
alt: ''
},
url: Astro.request.url
};
const postsAfter = await getCollection("articles", ({ data }) => {
return data.published.getTime() < post.data.published.getTime();
});
const [after] = postsAfter.sort(
(a, b) => b.data.published.getTime() - a.data.published.getTime()
);
const postsBefore = await getCollection("articles", ({ data }) => {
return data.published.getTime() > post.data.published.getTime();
});
const [before] = postsBefore.sort(
(a, b) => a.data.published.getTime() - b.data.published.getTime()
);
---
<DefaultLayout title={title} description={description}>
<Meta slot="head" openGraph={openGraph} />
<div class="my-8">
<Navbar />
<article class="mt-8">
<div class="my-8">
<div class="flex flex-col space-y-2">
<h1 class="font-bold text-5xl lg:text-6xl">{title}</h1>
<h2 class="text-xl italic">{description}</h2>
<Tags tags={tags} />
<p class="text-gray-500">
<TimeSince date={published} client:only />
</p>
</div>
</div>
<div>
<div class="lg:grid grid-flow-col-dense grid-cols-4 gap-8">
<div class="order-last">
<TableOfContents headings={headings} />
</div>
<div class="col-span-3">
<div class="prose prose-neutral dark:prose-invert max-w-none article">
<Content />
</div>
</div>
</div>
<Pagination
next={before ? `/post/${before.slug}` : undefined}
previous={after ? `/post/${after.slug}` : undefined}
/>
</div>
</article>
</div>
</DefaultLayout>
<style is:global>
.article {
& :is(:where(h1, h2, h3, h4, h5, h6)) {
&:hover {
.link {
@apply inline;
}
}
& .link {
@apply text-gray-600 dark:text-gray-400 hidden ml-2 hover:no-underline print:hidden;
}
}
}
</style>
<script>
const headings = document.querySelectorAll(
".article :is(:where(h1, h2, h3, h4, h5, h6))"
);
headings.forEach((h) => {
const link = document.createElement("a");
link.innerHTML = "#";
link.className = "link";
link.href = `#${h.id}`;
h.appendChild(link);
});
</script>