-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TOCコンポーネントを追加し、見出しに自動IDを付与する機能を実装しました。また、スタイルを改善しました。
- Loading branch information
Showing
7 changed files
with
129 additions
and
54 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
next/src/app/[locale]/(common-layout)/user/[handle]/page/[slug]/components/toc.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"use client"; | ||
import { useEffect } from "react"; | ||
import * as tocbot from "tocbot"; | ||
import "tocbot/dist/tocbot.css"; | ||
|
||
export default function PostContent() { | ||
useEffect(() => { | ||
// tocbotの初期化 | ||
tocbot.init({ | ||
tocSelector: ".js-toc", | ||
contentSelector: ".js-content", | ||
headingSelector: "h1, h2, h3", | ||
collapseDepth: 10, | ||
orderedList: false, | ||
headingLabelCallback: (text) => { | ||
// 10文字以上の場合は10文字に切り詰めて「...」を追加 | ||
return text.length > 10 ? `${text.substring(0, 10)}...` : text; | ||
}, | ||
}); | ||
|
||
// コンポーネントのアンマウント時にリソースを解放 | ||
return () => { | ||
tocbot.destroy(); | ||
}; | ||
}, []); | ||
return <nav className="js-toc" />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters