diff --git a/src/components/common/PageContent.tsx b/src/components/common/PageContent.tsx index c04ab2d1..f0f11723 100644 --- a/src/components/common/PageContent.tsx +++ b/src/components/common/PageContent.tsx @@ -1,6 +1,7 @@ import clsx from "clsx" import { useCodeCopy } from "~/hooks/useCodeCopy" import { renderPageContent } from "~/markdown" +import { PostToc } from "~/components/site/PostToc" export const PageContent: React.FC<{ content?: string @@ -9,20 +10,17 @@ export const PageContent: React.FC<{ }> = ({ className, content, toc }) => { useCodeCopy() - // TODO - // const [element, setElement] = useState() - // const renderMarkdown = useCallback((doc: string) => { - // setElement(renderPageContent(doc).element) - // }, []) - // useEffect(() => { - // if (content) { - // renderMarkdown(content) - // } - // }, [content, renderMarkdown]) + let parsedContent: ReturnType | null = null + if (content) { + parsedContent = renderPageContent(content) + } return ( -
- {content && renderPageContent(content, false, toc).element} -
+ <> +
+ {parsedContent?.element} +
+ {toc && parsedContent?.toc && } + ) } diff --git a/src/components/site/PostLink.tsx b/src/components/site/PostLink.tsx index b310e04e..0275bd90 100644 --- a/src/components/site/PostLink.tsx +++ b/src/components/site/PostLink.tsx @@ -1,7 +1,6 @@ import { Link } from "react-scroll" export const PostLink: React.FC = ({ children, ...props }) => { - console.log(props.href) if (props.href?.startsWith("#")) { return ( + {items?.children?.map((item, index) => ( +
  • + {item.children.map((child: any) => ( + <> + {child.type === "paragraph" && child.children?.[0]?.url && ( + + {`${prefix}${index + 1}. ${ + child.children[0].children?.[0]?.value + }`} + + )} + {child.type === "list" && + renderItems(child, activeId, `${index + 1}.`)} + + ))} +
  • + ))} + + ) +} export const PostToc: React.FC<{ - children: React.ReactNode -}> = ({ children }) => { + data: TocResult +}> = ({ data }) => { const containerRef = useRef(null) const [maxWidth, setMaxWidth] = useState(0) @@ -19,14 +52,14 @@ export const PostToc: React.FC<{ return (
    40 ? maxWidth : 0, }} ref={containerRef} >
    - {children} + {renderItems(data?.map, "activeId")}
    ) diff --git a/src/components/site/SiteLayout.tsx b/src/components/site/SiteLayout.tsx index d245edb3..b61edede 100644 --- a/src/components/site/SiteLayout.tsx +++ b/src/components/site/SiteLayout.tsx @@ -47,7 +47,7 @@ export const SiteLayout: React.FC = ({ children, title }) => {
    {children}
    diff --git a/src/markdown/index.ts b/src/markdown/index.ts index be017abc..8ce29232 100644 --- a/src/markdown/index.ts +++ b/src/markdown/index.ts @@ -24,9 +24,7 @@ import { remarkYoutube } from "./remark-youtube" import sanitizeScheme from "./sanitize-schema" import rehypeSlug from "rehype-slug" import rehypeAutolinkHeadings from "rehype-autolink-headings" -import { toc } from "mdast-util-toc" -import { PostToc } from "~/components/site/PostToc" -import { PostLink } from "~/components/site/PostLink" +import { toc, Result as TocResult } from "mdast-util-toc" import { Element } from "react-scroll" export type MarkdownEnv = { @@ -34,6 +32,7 @@ export type MarkdownEnv = { frontMatter: Record __internal: Record cover: string + toc: TocResult | null } export type Rendered = { @@ -42,6 +41,7 @@ export type Rendered = { excerpt: string frontMatter: Record cover: string + toc: TocResult | null } refractor.alias("html", ["svelte", "vue"]) @@ -58,6 +58,7 @@ export const renderPageContent = ( __internal: {}, frontMatter: {}, cover: "", + toc: null, } const result = unified() @@ -75,17 +76,7 @@ export const renderPageContent = ( } }) .use(() => (tree) => { - if (enableToc) { - const result = toc(tree, { tight: true, ordered: true }) - if (result.map) { - tree.children = [ - ...tree.children, - { type: "html", value: "" }, - result.map, - { type: "html", value: "" }, - ] - } - } + env.toc = toc(tree, { tight: true, ordered: true }) }) .use(remarkGfm, {}) .use(remarkExcerpt, { env }) @@ -137,8 +128,6 @@ export const renderPageContent = ( createElement: createElement, components: { img: Image, - a: PostLink, - toc: PostToc, anchor: Element, } as any, }) @@ -152,5 +141,6 @@ export const renderPageContent = ( excerpt: env.excerpt, frontMatter: env.frontMatter, cover: env.cover, + toc: env.toc, } }