forked from statichunt/geeky-nextjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
211 lines (200 loc) · 7.61 KB
/
index.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import config from "@config/config.json";
import Base from "@layouts/Baseof";
import ImageFallback from "@layouts/components/ImageFallback";
import Pagination from "@layouts/components/Pagination";
import Post from "@layouts/partials/Post";
import Sidebar from "@layouts/partials/Sidebar";
import { getListPage, getSinglePage } from "@lib/contentParser";
import { getTaxonomy } from "@lib/taxonomyParser";
import dateFormat from "@lib/utils/dateFormat";
import { sortByDate } from "@lib/utils/sortFunctions";
import { markdownify } from "@lib/utils/textConverter";
import Link from "next/link";
import { FaRegCalendar } from "react-icons/fa";
const { blog_folder, pagination } = config.settings;
const Home = ({
banner,
posts,
featured_posts,
recent_posts,
categories,
promotion,
}) => {
// define state
const sortPostByDate = sortByDate(posts);
const featuredPosts = sortPostByDate.filter(
(post) => post.frontmatter.featured
);
const showPosts = pagination;
return (
<Base>
{/* Banner */}
<section className="section banner relative pb-0">
<ImageFallback
className="absolute bottom-0 left-0 z-[-1] w-full"
src={"/images/banner-bg-shape.svg"}
width={1905}
height={295}
alt="banner-shape"
priority
/>
<div className="container">
<div className="row flex-wrap-reverse items-center justify-center lg:flex-row">
<div className={banner.image_enable ? "mt-12 text-center lg:mt-0 lg:text-left lg:col-6" : "mt-12 text-center lg:mt-0 lg:text-left lg:col-12"}>
<div className="banner-title">
{markdownify(banner.title, "h1")}
{markdownify(banner.title_small, "span")}
</div>
{markdownify(banner.content, "p", "mt-4")}
{banner.button.enable && (
<Link
className="btn btn-primary mt-6"
href={banner.button.link}
rel={banner.button.rel}
>
{banner.button.label}
</Link>
)}
</div>
{banner.image_enable && (
<div className="col-9 lg:col-6">
<ImageFallback
className="mx-auto object-contain"
src={banner.image}
width={548}
height={443}
priority={true}
alt="Banner Image"
/>
</div>
)}
</div>
</div>
</section>
{/* Home main */}
<section className="section">
<div className="container">
<div className="row items-start">
<div className="mb-12 lg:mb-0 lg:col-8">
{/* Featured posts */}
{featured_posts.enable && (
<div className="section">
{markdownify(featured_posts.title, "h2", "section-title")}
<div className="rounded border border-border p-6 dark:border-darkmode-border">
<div className="row">
<div className="md:col-6">
<Post post={featuredPosts[0]} />
</div>
<div className="scrollbar-w-[10px] mt-8 max-h-[480px] scrollbar-thin scrollbar-track-gray-100 scrollbar-thumb-border dark:scrollbar-track-gray-800 dark:scrollbar-thumb-darkmode-theme-dark md:mt-0 md:col-6">
{featuredPosts
.slice(1, featuredPosts.length)
.map((post, i, arr) => (
<div
className={`mb-6 flex items-center pb-6 ${
i !== arr.length - 1 &&
"border-b border-border dark:border-darkmode-border"
}`}
key={`key-${i}`}
>
{post.frontmatter.image && (
<ImageFallback
className="mr-3 h-[85px] rounded object-cover"
src={post.frontmatter.image}
alt={post.frontmatter.title}
width={105}
height={85}
/>
)}
<div>
<h3 className="h5 mb-2">
<Link
href={`/${blog_folder}/${post.slug}`}
className="block hover:text-primary"
>
{post.frontmatter.title}
</Link>
</h3>
<p className="inline-flex items-center font-bold">
<FaRegCalendar className="mr-1.5" />
{dateFormat(post.frontmatter.date)}
</p>
</div>
</div>
))}
</div>
</div>
</div>
</div>
)}
{/* Promotion */}
{promotion.enable && (
<Link href={promotion.link} className="section block pt-0">
<ImageFallback
className="h-full w-full"
height="115"
width="800"
src={promotion.image}
alt="promotion"
/>
</Link>
)}
{/* Recent Posts */}
{recent_posts.enable && (
<div className="section pt-0">
{markdownify(recent_posts.title, "h2", "section-title")}
<div className="rounded border border-border px-6 pt-6 dark:border-darkmode-border">
<div className="row">
{sortPostByDate.slice(0, showPosts).map((post) => (
<div className="mb-8 md:col-6" key={post.slug}>
<Post post={post} />
</div>
))}
</div>
</div>
</div>
)}
<Pagination
totalPages={Math.ceil(posts.length / showPosts)}
currentPage={1}
/>
</div>
{/* sidebar */}
<Sidebar
className={"lg:mt-[9.5rem]"}
posts={posts}
categories={categories}
/>
</div>
</div>
</section>
</Base>
);
};
export default Home;
// for homepage data
export const getStaticProps = async () => {
const homepage = await getListPage("content/_index.md");
const { frontmatter } = homepage;
const { banner, featured_posts, recent_posts, promotion } = frontmatter;
const posts = getSinglePage(`content/${blog_folder}`);
const categories = getTaxonomy(`content/${blog_folder}`, "categories");
const categoriesWithPostsCount = categories.map((category) => {
const filteredPosts = posts.filter((post) =>
post.frontmatter.categories.includes(category)
);
return {
name: category,
posts: filteredPosts.length,
};
});
return {
props: {
banner: banner,
posts: posts,
featured_posts,
recent_posts,
promotion,
categories: categoriesWithPostsCount,
},
};
};