Skip to content

Commit cb72a3d

Browse files
committed
hotfix: バグ修正。
1 parent cc8e0b6 commit cb72a3d

File tree

3 files changed

+51
-39
lines changed

3 files changed

+51
-39
lines changed

src/app/[article_year]/[month]/[aid]/components/client.tsx

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,34 @@ import { Article } from "../../../../article/article-client";
44
import Image from "next/image";
55
import { useEffect, useRef } from "react";
66
import { generateArticleButton } from "../../../../article/article-client";
7-
import { useTranslations, useLocale } from "next-intl";
7+
import { useTranslations } from "next-intl";
8+
import { notFound } from "next/navigation";
9+
import { AvailableLocales } from "@/i18n/request";
810

911
interface ClientComponentProps {
1012
articles: Article[];
1113
slug: string;
12-
toc: { id: string; text: string; level: string }[];
13-
processedContent: string;
14+
tocs: {lang:AvailableLocales,toc:{ id: string; text: string; level: string }[]}[];
15+
processedContents: {content:string,lang:AvailableLocales}[];
1416
}
1517

1618
export default function ClientComponent({
1719
articles,
1820
slug,
19-
toc,
20-
processedContent,
21+
tocs,
22+
processedContents,
2123
}: ClientComponentProps) {
2224
const tocRef = useRef<HTMLElement>(null);
2325
const t = useTranslations();
2426
const locale = t("info.lang");
25-
27+
const processedContent = processedContents.find((c) => c.lang === locale)?.content;
28+
const toc = tocs.find((t) => t.lang === locale)?.toc || [];
2629
const article = articles.find((a) => a.slug === slug && a.lang === locale);
30+
console.log(article);
2731
const otherArticles = articles.filter(
2832
(a) => a.slug !== slug && a.lang === locale
2933
);
3034

31-
if (!article) {
32-
return null;
33-
}
34-
3535
useEffect(() => {
3636
const handleScroll = () => {
3737
if (!tocRef.current) return;
@@ -59,11 +59,14 @@ export default function ClientComponent({
5959
}
6060
});
6161
};
62-
6362
window.addEventListener("scroll", handleScroll);
6463
return () => window.removeEventListener("scroll", handleScroll);
6564
}, []);
6665

66+
if (!article || !processedContent) {
67+
return notFound();
68+
}
69+
6770
return (
6871
<div className="article-container">
6972
<aside className="toc relative md:sticky" ref={tocRef}>
Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
import {
2-
getArticleIndexes,
3-
toHTML,
4-
generateArticleButton,
5-
Article,
6-
} from "../../../../article/article";
1+
import { getArticleIndexes, toHTML } from "../../../../article/article";
72
import "../../../../article/article.css";
8-
import { notFound } from "next/navigation";
9-
import Image from "next/image";
103
import crypto from "crypto";
114
import ClientComponent from "./client";
5+
import { AvailableLocales } from "@/i18n/request";
126

137
export async function generateStaticParams() {
148
const indexes = getArticleIndexes();
@@ -28,32 +22,48 @@ export default async function ArticleServer({
2822
const articles = await toHTML(indexes);
2923
const slug = `${resolvedParams.article_year}/${resolvedParams.month}/${resolvedParams.aid}`;
3024

31-
const headings =
32-
articles
33-
.find((a) => a.slug === slug)
34-
?.content.match(/<h[1-6]>.*?<\/h[1-6]>/g) || [];
35-
const toc = headings.map((heading) => {
36-
const text = heading.replace(/<.*?>/g, "").trim();
37-
const id = crypto.createHash("sha512").update(text).digest("hex");
38-
const level = "index-" + heading.slice(1, 3);
39-
return { id, text, level };
25+
const headings_list = articles
26+
.filter((a) => a.slug === slug)
27+
.map((article) => {
28+
return {
29+
lang: article.lang as AvailableLocales,
30+
toc: article.content.match(/<h[1-6]>.*?<\/h[1-6]>/g) || [],
31+
};
32+
});
33+
const tocs = headings_list.map((headings) => {
34+
return {
35+
toc: headings.toc.map((heading) => {
36+
const text = heading.replace(/<.*?>/g, "").trim();
37+
const id = crypto.createHash("sha512").update(text).digest("hex");
38+
const level = "index-" + heading.slice(1, 3);
39+
return { id, text, level };
40+
}),
41+
lang: headings.lang,
42+
};
4043
});
4144

42-
const processedContent =
43-
articles
44-
.find((a) => a.slug === slug)
45-
?.content.replace(/<h([1-6])>(.*?)<\/h[1-6]>/g, (match, level, text) => {
46-
const newLevel = Math.min(parseInt(level) + 1, 6);
47-
const id = crypto.createHash("sha512").update(text).digest("hex");
48-
return `<h${newLevel} id="${id}">${text}</h${newLevel}>`;
49-
}) || "";
45+
const processedContents = articles
46+
.filter((a) => a.slug === slug)
47+
.map((article) => {
48+
return {
49+
content: article.content.replace(
50+
/<h([1-6])>(.*?)<\/h[1-6]>/g,
51+
(match, level, text) => {
52+
const newLevel = Math.min(parseInt(level) + 1, 6);
53+
const id = crypto.createHash("sha512").update(text).digest("hex");
54+
return `<h${newLevel} id="${id}">${text}</h${newLevel}>`;
55+
}
56+
),
57+
lang: article.lang as AvailableLocales,
58+
};
59+
});
5060

5161
return (
5262
<ClientComponent
5363
articles={articles}
5464
slug={slug}
55-
toc={toc}
56-
processedContent={processedContent}
65+
tocs={tocs}
66+
processedContents={processedContents}
5767
/>
5868
);
5969
}

src/app/layout.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import Header from "./layout/header";
33
import Footer from "./layout/footer";
44
import BackToTopButton from "./layout/back2top";
55
import I18nProvider from "./i18nProvider";
6-
import { getMessages, AvailableLocales } from "@/i18n/request";
76
import { Geist, Geist_Mono } from "next/font/google";
87
import type { Metadata } from "next";
98

0 commit comments

Comments
 (0)