Skip to content

Commit 9d564c4

Browse files
committed
fix toc link
1 parent dc91e99 commit 9d564c4

4 files changed

Lines changed: 28 additions & 18 deletions

File tree

src/app/[lang]/components/Article.tsx

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,42 +37,45 @@ const Article: React.FC<ArticleProps> = ({ children, className, ...props }) => {
3737

3838
setHeadings(newHeadings);
3939
}, [children]); // đảm bảo cập nhật khi nội dung thay đổi
40-
const MyArticle = () => <article ref={articleRef} className={className} {...props}>
41-
{children}
42-
</article>
4340

4441
return (
4542
<div>
4643
<PanelGroup autoSaveId="example" direction="horizontal">
4744
<Panel defaultSize={80}>
48-
<MyArticle/>
45+
<article ref={articleRef}
46+
className={className} {...props}>
47+
{children}
48+
</article>
4949
</Panel>
5050
<PanelResizeHandle
5151
className={`resizeHandler
5252
${style.tocShow}`}
5353
/>
5454
<Panel className={`${style.tocContainer} ${style.tocShow}`}>
55-
<div className={style.toc}>{myToc(headings)}</div>
55+
{headings.length > 0 && <div className={style.toc}>{myToc(headings)}</div>}
5656
</Panel>
5757
</PanelGroup>
5858
<GiscusComments />
5959
<LanguageSwitcher />
60-
6160
</div>
6261
);
6362
};
6463

6564
export default Article;
6665

67-
export function myToc(headings: Heading[]){
68-
return <nav className=''>
69-
<h3>Mục lục bài viết</h3>
70-
<ul>
71-
{headings.map(({ id, text, level }) => (
72-
<li key={id} style={{ marginLeft: (level-3) * 10 }}>
73-
<a href={`#${id}`}>{text}</a>
74-
</li>
75-
))}
76-
</ul>
77-
</nav>
66+
export function myToc(headings: Heading[]) {
67+
return (
68+
<nav>
69+
<h3>Mục lục bài viết</h3>
70+
<ul>
71+
{headings.map(({ id, text, level }) => (
72+
<li key={id} style={{ marginLeft: (level - 1) * 10 }}>
73+
<a href={`#${id}`}>
74+
{text}
75+
</a>
76+
</li>
77+
))}
78+
</ul>
79+
</nav>
80+
);
7881
}

src/css/components/article.module.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
font-style: italic;
3030
margin: 1.5rem 0;
3131
}
32+
.article h1, .article h2, .article h3 {
33+
scroll-margin-top: 100px;
34+
}
3235

3336
.container {
3437
display: flex;

src/scss/components/article.module.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
font-style: italic;
3636
margin: 1.5rem 0;
3737
}
38+
h1,h2,h3{
39+
scroll-margin-top: 100px;
40+
}
41+
3842

3943
}
4044
.container{

src/scss/globals.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ details{
4242
}
4343
.center{
4444
@include display.center;
45-
}
45+
}

0 commit comments

Comments
 (0)