diff --git a/src/app/(routes)/blog/components/home/new/new.tsx b/src/app/(routes)/blog/components/home/new/new.tsx
index accc0a2..1525fba 100644
--- a/src/app/(routes)/blog/components/home/new/new.tsx
+++ b/src/app/(routes)/blog/components/home/new/new.tsx
@@ -3,6 +3,7 @@ import { getDefaultLocale } from "@/app/lib/functions/utils";
import { PostType } from "@/app/types/types";
import Image from "next/image";
import Link from "next/link";
+import { estimateReadingTime } from "../../../posts/[postSlug]/readingTime";
interface NewProps {
data: PostType[];
@@ -10,12 +11,15 @@ interface NewProps {
export default async function New({ data }: NewProps) {
const dict = await getDictionary(await getDefaultLocale());
+ const featured = data[0];
+ const readingTime = estimateReadingTime(featured.body, await getDefaultLocale());
+ const featuredTags = Array.isArray(featured.tags) ? featured.tags.slice(0, 2) : [];
return (
-
+
@@ -31,7 +35,7 @@ export default async function New({ data }: NewProps) {
href={"/"}
className="text-[15px] font-[400] capitalize text-white underline md:text-[16px]"
>
- {data[0].author.name}
+ {featured.author.name}
@@ -39,24 +43,50 @@ export default async function New({ data }: NewProps) {
{dict.blog.homePage.in}
- {data[0].category.name}
+ {featured.category.name}
- {data[0].title}
+ {featured.title}
- {data[0].excerpt}
+ {featured.excerpt}
+
+
+ {featuredTags.length > 0 ? (
+
+ {featuredTags.map((tag) => (
+
+ #{tag.name}
+
+ ))}
+
+ ) : null}
+
+ {readingTime} {dict.blog.homePage.readTime}
+
+
+
+
+ {dict.blog.homePage.readArticle}
+
diff --git a/src/app/(routes)/blog/components/home/newsletter/newsletterCta.tsx b/src/app/(routes)/blog/components/home/newsletter/newsletterCta.tsx
new file mode 100644
index 0000000..e06a314
--- /dev/null
+++ b/src/app/(routes)/blog/components/home/newsletter/newsletterCta.tsx
@@ -0,0 +1,30 @@
+import { getDictionary } from "@/app/lib/functions/dictionaries";
+import { getDefaultLocale } from "@/app/lib/functions/utils";
+
+const NEWSLETTER_EMAIL = `mailto:ceo@parsaaghayi.ir?subject=${encodeURIComponent(
+ "Blog newsletter subscription",
+)}`;
+
+export default async function NewsletterCta() {
+ const dict = await getDictionary(await getDefaultLocale());
+
+ return (
+
+ );
+}
\ No newline at end of file
diff --git a/src/app/(routes)/blog/components/share/shareButtons.tsx b/src/app/(routes)/blog/components/share/shareButtons.tsx
new file mode 100644
index 0000000..19b70f0
--- /dev/null
+++ b/src/app/(routes)/blog/components/share/shareButtons.tsx
@@ -0,0 +1,82 @@
+"use client";
+
+import { useState } from "react";
+
+type ShareButtonsProps = {
+ title: string;
+ url: string;
+ dict: {
+ share?: string;
+ copyLink?: string;
+ linkCopied?: string;
+ };
+ vertical?: boolean;
+};
+
+const SHARE_TARGETS = [
+ {
+ key: "twitter",
+ label: "X",
+ buildUrl: (title: string, url: string) =>
+ `https://twitter.com/intent/tweet?url=${encodeURIComponent(url)}&text=${encodeURIComponent(title)}`,
+ },
+ {
+ key: "linkedin",
+ label: "in",
+ buildUrl: (title: string, url: string) =>
+ `https://www.linkedin.com/sharing/share-offsite/?url=${encodeURIComponent(url)}`,
+ },
+ {
+ key: "telegram",
+ label: "TG",
+ buildUrl: (title: string, url: string) =>
+ `https://t.me/share/url?url=${encodeURIComponent(url)}&text=${encodeURIComponent(title)}`,
+ },
+];
+
+export default function ShareButtons({
+ title,
+ url,
+ dict,
+ vertical = false,
+}: ShareButtonsProps) {
+ const [copied, setCopied] = useState(false);
+
+ const copyLink = async () => {
+ try {
+ await navigator.clipboard.writeText(url);
+ setCopied(true);
+ setTimeout(() => setCopied(false), 2000);
+ } catch {
+ setCopied(false);
+ }
+ };
+
+ const stackClass = vertical ? "flex-col items-center gap-4" : "flex-row items-center gap-4";
+
+ return (
+
+ {SHARE_TARGETS.map((target) => (
+
+ {target.label}
+
+ ))}
+
+
+ );
+}
\ No newline at end of file
diff --git a/src/app/(routes)/blog/page.tsx b/src/app/(routes)/blog/page.tsx
index d9e320a..e14f6d1 100644
--- a/src/app/(routes)/blog/page.tsx
+++ b/src/app/(routes)/blog/page.tsx
@@ -2,6 +2,7 @@ import FilteredPosts from "./components/home/filtered-posts/filteredPosts";
import New from "./components/home/new/new";
import Random from "./components/home/random/random";
import Categories from "./components/home/Categories/Categories";
+import NewsletterCta from "./components/home/newsletter/newsletterCta";
import { getDefaultLocale } from "@/app/lib/functions/utils";
export default async function Blog() {
@@ -22,6 +23,7 @@ export default async function Blog() {
+
);
diff --git a/src/app/(routes)/blog/posts/[postSlug]/body.tsx b/src/app/(routes)/blog/posts/[postSlug]/body.tsx
index 4f75dcc..de0e091 100644
--- a/src/app/(routes)/blog/posts/[postSlug]/body.tsx
+++ b/src/app/(routes)/blog/posts/[postSlug]/body.tsx
@@ -6,6 +6,11 @@ export type BodyBlock =
| { kind: "paragraph"; text: string }
| { kind: "list"; items: string[] };
+export type HeadingItem = {
+ id: string;
+ text: string;
+};
+
const H1_LINE = /^#\s+(.*)$/;
const H2_LINE = /^##\s+(.*)$/;
const LIST_ITEM_LINE = /^-\s+(.*)$/;
@@ -104,6 +109,21 @@ export function parseBody(body: unknown): BodyBlock[] {
);
}
+/**
+ * Section list for the table of contents. Heading anchors are `heading-N`
+ * matching the `id` values renderBody attaches to each h2, so TOC links and
+ * article headings stay in sync no matter how the body changes.
+ */
+export function getHeadings(body: unknown): HeadingItem[] {
+ return parseBody(body)
+ .filter((block) => block.kind === "h2")
+ .map((block, index) =>
+ block.kind === "h2"
+ ? { id: `heading-${index}`, text: block.text }
+ : { id: "", text: "" },
+ );
+}
+
/**
* Renders the post body as React elements. Falls back to the post excerpt and,
* when that is empty too, to a short Persian notice. Text is always rendered as
@@ -113,6 +133,22 @@ export function renderBody(body: unknown, fallback?: unknown): ReactNode {
const blocks = parseBody(body);
if (blocks.length > 0) {
+ const h2Ordinal = (blocks: BodyBlock[]) => {
+ let count = 0;
+ const ordinalAt: { [index: number]: number } = {};
+
+ blocks.forEach((block, index) => {
+ if (block.kind === "h2") {
+ ordinalAt[index] = count;
+ count += 1;
+ }
+ });
+
+ return ordinalAt;
+ };
+
+ const ordinals = h2Ordinal(blocks);
+
return blocks.map((block, index) => {
const key = `${block.kind}-${index}`;
@@ -125,7 +161,11 @@ export function renderBody(body: unknown, fallback?: unknown): ReactNode {
);
case "h2":
return (
-
+
{block.text}
);
diff --git a/src/app/(routes)/blog/posts/[postSlug]/page.tsx b/src/app/(routes)/blog/posts/[postSlug]/page.tsx
index 7955c4a..a9269ea 100644
--- a/src/app/(routes)/blog/posts/[postSlug]/page.tsx
+++ b/src/app/(routes)/blog/posts/[postSlug]/page.tsx
@@ -12,7 +12,9 @@ import { Metadata } from "next";
import Image from "next/image";
import Link from "next/link";
import { redirect } from "next/navigation";
-import { renderBody } from "./body";
+import { getHeadings, renderBody } from "./body";
+import { estimateReadingTime } from "./readingTime";
+import ShareButtons from "../../components/share/shareButtons";
import { getCoverImageSrc } from "./coverImage";
import { AUTHOR_PROFILE_URL } from "@/app/lib/social-links";
import { SITE_NAME, SITE_NAME_EN, SITE_URL } from "@/app/lib/site-config";
@@ -133,6 +135,10 @@ export default async function SinglePost(props: PostSlugPagepropsType) {
const relatedPosts = Array.isArray(postData.related_posts)
? postData.related_posts
: [];
+ const previousPost = postData.previous_post ?? null;
+ const nextPost = postData.next_post ?? null;
+ const readingTime = estimateReadingTime(post.body, locale);
+ const headings = getHeadings(post.body);
const pageUrl = `${SITE_URL}/blog/posts/${post.slug ?? params.postSlug}`;
const articleJsonLd: Record = {
@@ -240,6 +246,11 @@ export default async function SinglePost(props: PostSlugPagepropsType) {
{publishedDate}
) : null}
+ {readingTime > 0 ? (
+
+ {readingTime} {dict.blog.postSinglePage.readTime}
+
+ ) : null}
@@ -287,12 +298,31 @@ export default async function SinglePost(props: PostSlugPagepropsType) {
-
-
+ {headings.length > 0 ? (
+
+ ) : null}
+
@@ -448,6 +478,39 @@ export default async function SinglePost(props: PostSlugPagepropsType) {
))}
+
+ {previousPost || nextPost ? (
+
+ {previousPost ? (
+
+
+ {dict.blog.postSinglePage.newerPost}
+
+
+ {previousPost.title}
+
+
+ ) : (
+
+ )}
+ {nextPost ? (
+
+
+ {dict.blog.postSinglePage.olderPost}
+
+
+ {nextPost.title}
+
+
+ ) : null}
+
+ ) : null}
);
diff --git a/src/app/(routes)/blog/posts/[postSlug]/readingTime.ts b/src/app/(routes)/blog/posts/[postSlug]/readingTime.ts
new file mode 100644
index 0000000..e1adb59
--- /dev/null
+++ b/src/app/(routes)/blog/posts/[postSlug]/readingTime.ts
@@ -0,0 +1,29 @@
+import { parseBody } from "./body";
+
+const WORDS_PER_MINUTE_EN = 210;
+const WORDS_PER_MINUTE_FA = 150;
+
+/**
+ * Estimates how long a post takes to read, in whole minutes. Persian text is
+ * denser per character than Latin text, so the two languages use different
+ * reading rates. A post always reports at least one minute.
+ */
+export function estimateReadingTime(body: unknown, locale: string): number {
+ const blocks = parseBody(body);
+
+ const wordCount = blocks.reduce((total, block) => {
+ if (block.kind === "list") {
+ return total + block.items.join(" ").split(/\s+/).filter(Boolean).length;
+ }
+
+ if (block.kind === "paragraph" || block.kind === "h1" || block.kind === "h2") {
+ return total + block.text.split(/\s+/).filter(Boolean).length;
+ }
+
+ return total;
+ }, 0);
+
+ const rate = locale === "fa" ? WORDS_PER_MINUTE_FA : WORDS_PER_MINUTE_EN;
+
+ return Math.max(1, Math.round(wordCount / rate));
+}
\ No newline at end of file
diff --git a/src/app/lib/dictionaries/en.json b/src/app/lib/dictionaries/en.json
index 8f6f0c2..3cfa486 100644
--- a/src/app/lib/dictionaries/en.json
+++ b/src/app/lib/dictionaries/en.json
@@ -107,7 +107,12 @@
"popular": "popular",
"random": "random",
"by": "BY",
- "in": "IN"
+ "in": "IN",
+ "readArticle": "Read article",
+ "newsletterTitle": "Get fresh articles in your inbox",
+ "newsletterText": "One useful engineering post per week. No spam, unsubscribe anytime.",
+ "subscribe": "Subscribe",
+ "readTime": "min read"
},
"filteredPostsPage": {
"newTitle": "new",
@@ -125,7 +130,15 @@
},
"postSinglePage": {
"followMe": "follow me",
- "YouMightLike": "you might also like..."
+ "YouMightLike": "you might also like...",
+ "readTime": "min read",
+ "onThisPage": "On this page",
+ "share": "Share",
+ "copyLink": "Copy link",
+ "linkCopied": "Link copied",
+ "newerPost": "Newer post",
+ "olderPost": "Older post",
+ "home": "Home"
},
"header": {
"Home": "Home",
@@ -144,4 +157,4 @@
"last": "last"
}
}
-}
+}
\ No newline at end of file
diff --git a/src/app/lib/dictionaries/fa.json b/src/app/lib/dictionaries/fa.json
index 328a33c..c5ed2ea 100644
--- a/src/app/lib/dictionaries/fa.json
+++ b/src/app/lib/dictionaries/fa.json
@@ -107,7 +107,12 @@
"popular": "محبوبترینها",
"random": "تصادفی",
"by": "توسط",
- "in": "در"
+ "in": "در",
+ "readArticle": "مطالعه مقاله",
+ "newsletterTitle": "مقالههای تازه را در ایمیل خود دریافت کنید",
+ "newsletterText": "هر هفته یک مقاله مفید درباره مهندسی نرمافزار. بدون اسپم، لغو اشتراک در هر زمان.",
+ "subscribe": "عضویت",
+ "readTime": "دقیقه مطالعه"
},
"filteredPostsPage": {
"newTitle": "جدیدترینها",
@@ -125,7 +130,15 @@
},
"postSinglePage": {
"followMe": "من را دنبال کنید",
- "YouMightLike": "شاید اینها را هم بپسندید..."
+ "YouMightLike": "شاید اینها را هم بپسندید...",
+ "readTime": "دقیقه مطالعه",
+ "onThisPage": "فهرست مطالب",
+ "share": "اشتراکگذاری",
+ "copyLink": "کپی لینک",
+ "linkCopied": "لینک کپی شد",
+ "newerPost": "پست جدیدتر",
+ "olderPost": "پست قدیمیتر",
+ "home": "خانه"
},
"header": {
"Home": "خانه",
@@ -144,4 +157,4 @@
"last": "آخرین"
}
}
-}
+}
\ No newline at end of file
diff --git a/src/app/types/types.ts b/src/app/types/types.ts
index a41846a..2531d0e 100644
--- a/src/app/types/types.ts
+++ b/src/app/types/types.ts
@@ -107,6 +107,8 @@ export type PostSlugPagepropsType = {
export type PostSlugPagePostType = {
post: PostType;
related_posts: PostType[];
+ previous_post: PostType | null;
+ next_post: PostType | null;
};
export type MetaType = {
@@ -208,6 +210,11 @@ export type Dictionary = {
random: string;
by: string;
in: string;
+ readArticle: string;
+ readTime: string;
+ newsletterTitle: string;
+ newsletterText: string;
+ subscribe: string;
};
filteredPostsPage: {
newTitle: string;
@@ -226,6 +233,13 @@ export type Dictionary = {
postSinglePage: {
followMe: string;
YouMightLike: string;
+ readTime: string;
+ onThisPage: string;
+ share: string;
+ copyLink: string;
+ linkCopied: string;
+ newerPost: string;
+ olderPost: string;
};
header: {
Home: string;