1- import {
2- getArticleIndexes ,
3- toHTML ,
4- generateArticleButton ,
5- Article ,
6- } from "../../../../article/article" ;
1+ import { getArticleIndexes , toHTML } from "../../../../article/article" ;
72import "../../../../article/article.css" ;
8- import { notFound } from "next/navigation" ;
9- import Image from "next/image" ;
103import crypto from "crypto" ;
114import ClientComponent from "./client" ;
5+ import { AvailableLocales } from "@/i18n/request" ;
126
137export 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}
0 commit comments