Skip to content

Commit ae57c0b

Browse files
Added search toolbar functionalities
1 parent 009f7da commit ae57c0b

11 files changed

Lines changed: 184 additions & 76 deletions

File tree

lib/posts.ts

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,23 @@ import matter from "gray-matter";
44

55
const postsDirectory = path.join(process.cwd(), "posts");
66

7-
export function getAllPostsNames() {
8-
return fs
9-
.readdirSync(postsDirectory)
10-
.map(
11-
postName =>
12-
`${process.env.NEXT_PUBLIC_URL}blog/${postName.replace(/\.md$/, "")}/`
13-
);
14-
}
15-
16-
function getAllPostsData() {
7+
export function getAllPostsData() {
178
const postNames = fs.readdirSync(postsDirectory);
189
const allPostsData = postNames.map(postName => {
1910
const fullPath = path.join(postsDirectory, postName);
2011
const id = postName.replace(/\.md$/, "");
2112

13+
const postURL = `${process.env.NEXT_PUBLIC_URL}blog/${id}/`;
2214
const postContent = fs.readFileSync(fullPath, "utf8");
2315

24-
return { id, ...matter(postContent) };
16+
return {
17+
postURL,
18+
id,
19+
...matter(postContent, {
20+
excerpt: true,
21+
excerpt_separator: "<!-- end -->",
22+
}),
23+
};
2524
});
2625

2726
return allPostsData;
@@ -31,23 +30,21 @@ export function getPostData(id: string) {
3130
const postPath = path.join(postsDirectory, id + ".md");
3231

3332
try {
34-
const postData = fs.readFileSync(postPath, "utf8");
33+
const postData = fs
34+
.readFileSync(postPath, "utf8")
35+
.replace("<!-- end -->", "");
3536
return matter(postData);
3637
} catch (error) {
3738
return false;
3839
}
3940
}
4041

4142
export function getPostsPreview() {
42-
return getAllPostsData()
43-
.map(postData => {
44-
return { id: postData.id, data: postData.data };
45-
})
46-
.sort((a, b) => {
47-
if (a.data.date < b.data.date) {
48-
return 1;
49-
} else {
50-
return -1;
51-
}
52-
});
43+
return getAllPostsData().map(postData => {
44+
return {
45+
id: postData.id,
46+
data: postData.data,
47+
excerpt: postData.excerpt,
48+
};
49+
});
5350
}

pages/blog/blog-sitemap.xml/index.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
import { GetServerSidePropsContext } from "next";
22
import { getServerSideSitemapLegacy } from "next-sitemap";
3-
import { getAllPostsNames } from "../../../lib/posts";
3+
import { getAllPostsData } from "../../../lib/posts";
44

55
export default function GenerateSitemap() {}
66

77
export async function getServerSideProps(ctx: GetServerSidePropsContext) {
8-
const postsURL = getAllPostsNames();
8+
const postsData = getAllPostsData();
9+
const postsURLAndDate = postsData.map(postData => {
10+
return { postURL: postData.postURL, date: postData.data.date };
11+
});
912

1013
fetch(
1114
"https://www.google.com/ping?sitemap=https://tools-css.vercel.app/blog/blog-sitemap.xml"
1215
);
1316

1417
return getServerSideSitemapLegacy(
1518
ctx,
16-
postsURL.map(postURL => {
19+
postsURLAndDate.map(({ date, postURL }) => {
1720
return {
1821
loc: postURL,
19-
lastmod: new Date().toISOString(),
22+
lastmod: new Date(date).toISOString(),
2023
changefreq: "monthly",
2124
priority: 0.8,
2225
};

pages/blog/index.tsx

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ import PostPreview from "../../src/components/blog/PostPreview";
33
import ColorText from "../../src/components/ColorText";
44
import SearchToolbar from "../../src/components/SearchToolbar";
55
import { getPostsPreview } from "../../lib/posts";
6-
import path from "path";
6+
import Image from "next/image";
7+
import { useState } from "react";
78

89
const url = process.env.NEXT_PUBLIC_URL;
910

1011
export default function Blog({ postsData }: { postsData: postsDataProps }) {
12+
const [filteredPosts, setFilteredPosts] = useState(postsData);
13+
1114
return (
1215
<main className="bg-zinc-800">
1316
<NextSeo
@@ -25,33 +28,51 @@ export default function Blog({ postsData }: { postsData: postsDataProps }) {
2528
/>
2629
<div
2730
id="home"
28-
className="h-[70vh] md:h-[50vh] flex flex-col items-center justify-center text-center p-5 bg-background bg-opacity-5 bg-zinc-700"
31+
className="relative h-[70vh] md:h-[50vh] flex flex-col items-center justify-center text-center p-5 bg-zinc-950"
2932
>
3033
<ColorText
3134
Variant="span"
32-
className="text-base font-semibold tracking-widest uppercase"
35+
className="text-base font-semibold tracking-widest uppercase z-10"
3336
>
3437
Blog
3538
</ColorText>
36-
<h1 className="text-4xl font-extrabold uppercase text-zinc-50">
39+
<h1 className="text-4xl font-extrabold uppercase text-zinc-50 z-10">
3740
Find the best tips for better designs
3841
</h1>
42+
43+
<div className="opacity-50">
44+
<Image
45+
src="/background.jpg"
46+
fill
47+
alt="background img"
48+
className="object-cover pointer-events-none"
49+
/>
50+
</div>
3951
</div>
4052
<div className="flex justify-center py-10 p-5 bg-zinc-900">
4153
<div className="max-w-4xl flex flex-col items-center gap-20 justify-center">
42-
<SearchToolbar />
54+
<SearchToolbar
55+
postsData={postsData}
56+
setFilteredPosts={setFilteredPosts}
57+
/>
4358
<div className="flex w-full">
44-
<TimeLine />
45-
<div className="flex flex-col gap-3">
46-
{postsData.map(({ data, id }) => (
47-
<PostPreview
48-
title={data.title}
49-
date={data.date}
50-
excerpt={data.excerpt}
51-
slug={id}
52-
key={id}
53-
/>
54-
))}
59+
{filteredPosts.length > 0 && <TimeLine />}
60+
<div className="flex flex-col gap-3 w-full">
61+
{filteredPosts.length > 0 ? (
62+
filteredPosts.map(({ data, id, excerpt }) => (
63+
<PostPreview
64+
title={data.title}
65+
date={data.date}
66+
excerpt={excerpt}
67+
slug={id}
68+
key={id}
69+
/>
70+
))
71+
) : (
72+
<span className="text-center text-zinc-50 text-lg font-medium">
73+
No results was found :(
74+
</span>
75+
)}
5576
</div>
5677
</div>
5778
</div>
@@ -61,16 +82,17 @@ export default function Blog({ postsData }: { postsData: postsDataProps }) {
6182
}
6283

6384
function TimeLine() {
64-
return <div className="h-full w-2 rounded-lg bg-zinc-700 md:mr-10 mr-3" />;
85+
return <div className="h-full w-1 rounded-lg bg-zinc-700 md:mr-10 mr-3" />;
6586
}
6687

67-
type postsDataProps = {
88+
export type postsDataProps = {
6889
id: string;
6990
data: {
7091
date: string;
7192
title: string;
72-
excerpt: string;
93+
tags: string[];
7394
};
95+
excerpt: string;
7496
}[];
7597

7698
export async function getStaticProps() {

pages/index.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,39 @@ import { links } from "../src/components/Header";
22
import ColorText from "../src/components/ColorText";
33
import Link from "next/link";
44
import { CaretDown } from "phosphor-react";
5+
import Image from "next/image";
56

67
function App() {
78
return (
89
<main className="bg-zinc-800">
910
<div
1011
id="home"
11-
className="h-[100vh] flex flex-col items-center justify-center p-5 bg-background bg-opacity-5 bg-zinc-700"
12+
className="relative h-[100vh] flex flex-col items-center justify-center p-5 bg-zinc-950"
1213
>
13-
<h1 className="font-bold text-[2.35rem] text-center leading-tight text-zinc-50 mt-auto">
14+
<h1 className="font-bold text-[2.35rem] text-center leading-tight text-zinc-50 mt-auto z-10">
1415
Elevate your website design with powerful{" "}
1516
<ColorText
1617
Variant="span"
17-
className="bg-clip-text bg-orange-fade text-transparent"
18+
className="bg-clip-text bg-orange-fade text-transparent z-10"
1819
>
1920
CSS tools
2021
</ColorText>
2122
.
2223
</h1>
23-
<a className="mt-auto animate-bounce text-orange-600" href="#tools">
24+
<a
25+
className="mt-auto animate-bounce text-orange-600 z-10"
26+
href="#tools"
27+
>
2428
<CaretDown size={32} />
2529
</a>
30+
<div className="opacity-30">
31+
<Image
32+
src="/background.jpg"
33+
fill
34+
alt="background img"
35+
className="object-cover pointer-events-none"
36+
/>
37+
</div>
2638
</div>
2739
<div id="tools" className="p-10 flex flex-col items-center">
2840
<ColorText

posts/how-to-center-a-div.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
---
22
title: "3 Ways To Center A Div"
33
date: "2023-04-13"
4-
excerpt: "Centering a div element in CSS can sometimes be a challenging task, but fear not! There are several methods you can use to achieve this. In this article, we will explore three popular ways to center a div using English terms."
4+
tags:
5+
- centering
6+
- layout
57
---
68

7-
Centering a div element in CSS can sometimes be a challenging task, but fear not! There are several methods you can use to achieve this. In this article, we will explore three popular ways to center a div using English terms.
9+
Centering a div element in CSS can sometimes be a challenging task, but fear not! There are several methods you can use to achieve this. In this article, we will explore using English terms.
10+
11+
<!-- end -->
812

913
## Method 1: Margin Auto
1014

posts/teste-1.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
---
22
date: "2023-04-14"
3-
title: "Test 1243564574745 odjfvofnv fnvi dfiob ogvnb fgvnb"
4-
excerpt: "CSS (Cascading Style Sheets) is a styling language used for web development to define the layout, appearance, and design of web pages. It works in conjunction with HTML (Hypertext Markup Language) to create visually appealing and user-friendly websites."
3+
title: "Introduction to CSS"
4+
tags:
5+
- typography
6+
- colors
7+
- layout
58
---
69

7-
# Introduction to CSS
8-
910
CSS (Cascading Style Sheets) is a styling language used for web development to define the layout, appearance, and design of web pages. It works in conjunction with HTML (Hypertext Markup Language) to create visually appealing and user-friendly websites.
1011

12+
<!-- end -->
13+
1114
## Benefits of CSS
1215

1316
- Separation of Concerns: CSS allows developers to separate the content (HTML) from the presentation (CSS) and behavior (JavaScript), making it easier to manage and maintain web pages.

src/components/AxisBox.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import Draggable, { DraggableData } from "react-draggable";
2-
import ColorText from "./ColorText";
1+
import Draggable from "react-draggable";
32
import InputText from "./InputText";
43

54
interface AxisBoxProps {

src/components/Header.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export default function Header() {
4848
(usePathname() === "/" || usePathname() === "/blog/"
4949
? "absolute top-0 "
5050
: "relative ") +
51-
"w-full flex flex-col justify-center items-center md:justify-start gap-5 p-6 md:flex-row"
51+
"w-full flex flex-col justify-center items-center z-20 md:justify-start gap-5 p-6 md:flex-row"
5252
}
5353
>
5454
<Link href="/">

0 commit comments

Comments
 (0)