-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
184 additions
and
39 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,48 +1,103 @@ | ||
"use client"; | ||
|
||
import { ComponentPropsWithoutRef, useEffect, useState } from "react"; | ||
import * as tocbot from "tocbot"; | ||
|
||
import useNoColonId from "@/hooks/useNoColonId"; | ||
|
||
import Link from "./Link"; | ||
import clsx from "clsx"; | ||
import { default as NextLink } from "next/link"; | ||
import { ComponentPropsWithoutRef, memo, useEffect, useState } from "react"; | ||
|
||
interface TocProps extends ComponentPropsWithoutRef<"div"> { | ||
contentSelector: string; | ||
} | ||
|
||
const Toc = ({ contentSelector, ...props }: TocProps) => { | ||
const id = useNoColonId(); | ||
useEffect(() => { | ||
// Tocbotの初期化 | ||
tocbot.init({ | ||
tocSelector: `#${id}`, // 目次の表示部分 | ||
contentSelector: contentSelector, // 目次を生成する対象 | ||
headingSelector: "h2, h3", // 目次に表示する見出しのタグ | ||
}); | ||
|
||
// コンポーネントがアンマウントされたときにTocbotを破棄 | ||
return () => tocbot.destroy(); | ||
}, []); | ||
type HeadingList = { | ||
id: string; | ||
text: string; | ||
level: number; | ||
}[]; | ||
|
||
return <div id={id} {...props}></div>; | ||
type HeadingTree = { | ||
id: string; | ||
text: string; | ||
level: number; | ||
children: HeadingTree[]; | ||
}; | ||
|
||
export const TocWithoutTocBot = ({ contentSelector, ...props }: TocProps) => { | ||
const [htmlIds, setHtmlIds] = useState<Element[]>([]); | ||
useEffect(() => { | ||
setHtmlIds(Array.from(document.querySelector(contentSelector)?.querySelectorAll("h2, h3") || [])); | ||
}, []); | ||
const genHeadingTree = (list: HeadingList) => { | ||
const tree: HeadingTree[] = []; | ||
for (const item of list) { | ||
// 木が空の場合 | ||
if (tree.length === 0) { | ||
tree.push({ ...item, children: [] }); | ||
continue; | ||
} | ||
|
||
const current = tree[tree.length - 1]; | ||
// 同じレベルの場合 | ||
if (current.level === item.level) { | ||
tree.push({ ...item, children: [] }); | ||
continue; | ||
} | ||
|
||
// 深い階層の場合 | ||
if (current.level < item.level) { | ||
current.children.push({ ...item, children: [] }); | ||
continue; | ||
} | ||
} | ||
|
||
return tree; | ||
}; | ||
|
||
const elementsToHeadingList = (elements: Element[]): HeadingList => | ||
elements.map((e) => ({ | ||
id: e.id, | ||
text: e.innerHTML, | ||
level: parseInt(e.tagName.slice(1)), | ||
})); | ||
|
||
const elementsToHeadingTree = (elements: Element[]): HeadingTree[] => genHeadingTree(elementsToHeadingList(elements)); | ||
|
||
export const RenderHeadingTree = ({ tree }: { tree: HeadingTree[] }) => { | ||
const levelClassNames: { [key: number]: string } = { | ||
1: "", | ||
2: "", | ||
3: "ml-4", | ||
}; | ||
return ( | ||
<div {...props}> | ||
{htmlIds.map((e) => ( | ||
<li key={e.id}> | ||
<Link href={`#${e.id}`}>{e.innerHTML}</Link> | ||
<ul> | ||
{tree.map((e) => ( | ||
<li key={e.id} className={levelClassNames[e.level]}> | ||
<NextLink href={`#${e.id}`} scroll={true}> | ||
{e.text} | ||
</NextLink> | ||
<RenderHeadingTree tree={e.children} /> | ||
</li> | ||
))} | ||
</ul> | ||
); | ||
}; | ||
|
||
export const useHeadingTree = (contentSelector: string) => { | ||
const [tree, setTree] = useState<HeadingTree[]>([]); | ||
|
||
useEffect(() => { | ||
const content = document.querySelector(contentSelector); | ||
if (!content) return; | ||
|
||
const headingTree = elementsToHeadingTree(Array.from(content.querySelectorAll("h2, h3, h4, h5, h6"))); | ||
setTree(headingTree); | ||
}, []); | ||
|
||
return tree; | ||
}; | ||
|
||
export const Toc = ({ contentSelector, ...props }: TocProps) => { | ||
const tree = useHeadingTree(contentSelector); | ||
|
||
return ( | ||
<div {...props} className={clsx(props.className, { hidden: tree.length < 1 })}> | ||
<RenderHeadingTree tree={tree} /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Toc; | ||
export default memo(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { ComponentPropsWithoutRef, useEffect } from "react"; | ||
import * as tocbot from "tocbot"; | ||
|
||
import useNoColonId from "@/hooks/useNoColonId"; | ||
|
||
interface TocProps extends ComponentPropsWithoutRef<"div"> { | ||
contentSelector: string; | ||
} | ||
export const Tocbot = ({ contentSelector, ...props }: TocProps) => { | ||
const id = useNoColonId(); | ||
useEffect(() => { | ||
// Tocbotの初期化 | ||
tocbot.init({ | ||
tocSelector: `#${id}`, // 目次の表示部分 | ||
contentSelector: contentSelector, // 目次を生成する対象 | ||
headingSelector: "h2, h3", // 目次に表示する見出しのタグ | ||
}); | ||
|
||
// コンポーネントがアンマウントされたときにTocbotを破棄 | ||
return () => tocbot.destroy(); | ||
}, []); | ||
|
||
return <div id={id} {...props}></div>; | ||
}; | ||
|
||
export default Tocbot; |
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,21 @@ | ||
"use client"; | ||
|
||
import { clsx } from "clsx"; | ||
|
||
import { BlogHeading as Heading } from "@/components/elements/Heading"; | ||
import { RenderHeadingTree, useHeadingTree } from "@/components/elements/Toc"; | ||
|
||
const Toc = ({ contentSelector }: { contentSelector: string }) => { | ||
const tree = useHeadingTree(contentSelector); | ||
|
||
return ( | ||
<div className={clsx(" border-accent py-8", { hidden: tree.length < 1 })}> | ||
<Heading level={2} className="text-accent"> | ||
お品書き | ||
</Heading> | ||
<RenderHeadingTree tree={tree} /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default 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