From f6cb2a8f74f662a647f8ca4e99f7fb489ca33c65 Mon Sep 17 00:00:00 2001 From: DefectingCat Date: Fri, 19 Aug 2022 10:50:02 +0800 Subject: [PATCH] Generate post toc on build time --- components/post/PostToc.tsx | 62 ++++++++++++++++++++++++++++--------- layouts/MDXLayout.tsx | 35 --------------------- lib/posts.ts | 2 +- lib/utils/index.ts | 42 +++++++++++++++++++++++++ pages/p/[slug].tsx | 16 ++++++++++ scripts/build-search.mjs | 6 ++-- styles/rua.css | 26 ++-------------- 7 files changed, 114 insertions(+), 75 deletions(-) delete mode 100644 layouts/MDXLayout.tsx diff --git a/components/post/PostToc.tsx b/components/post/PostToc.tsx index 4c88b0a..37a516e 100644 --- a/components/post/PostToc.tsx +++ b/components/post/PostToc.tsx @@ -1,4 +1,4 @@ -import { getHeadings } from 'lib/utils'; +import { getHeadings, SingleToc } from 'lib/utils'; import Anchor from 'components/mdx/Anchor'; import styles from './PostToc.module.css'; import classNames from 'classnames'; @@ -6,10 +6,39 @@ import { useCallback, useState } from 'react'; import { FiChevronDown } from 'react-icons/fi'; interface Props { - headings: ReturnType; + toc: SingleToc[]; + tocLength: number; } -const PostTOC = ({ headings }: Props) => { +const TocItem = ({ item }: { item: SingleToc }) => { + return ( +
  • + + {item.head} + +
  • + ); +}; +const TocList = ({ + toc, + children, +}: { + toc: SingleToc[]; + children?: React.ReactElement; +}) => { + return ( + + ); +}; + +const PostToc = ({ toc, tocLength }: Props) => { const [show, setShow] = useState(false); const handleClick = useCallback(() => setShow((show) => !show), []); @@ -22,7 +51,7 @@ const PostTOC = ({ headings }: Props) => { 'my-4' )} style={{ - maxHeight: show ? (headings?.length ?? 0) * 50 + 70 : 70, + maxHeight: show ? (tocLength ?? 0) * 50 + 70 : 70, }} >

    { />

    - +
    +
      + {toc?.map((h) => ( + <> + + {h.children.map((child) => ( +
        + +
      + ))} + + ))} +
    +
    ); }; -export default PostTOC; +export default PostToc; diff --git a/layouts/MDXLayout.tsx b/layouts/MDXLayout.tsx deleted file mode 100644 index af7cd0f..0000000 --- a/layouts/MDXLayout.tsx +++ /dev/null @@ -1,35 +0,0 @@ -import dynamic from 'next/dynamic'; -import { MyMatters } from 'types'; - -const Footer = dynamic(() => import('components/Footer')); -const HeadBar = dynamic(() => import('components/NavBar')); -const PostComment = dynamic(() => import('components/post/PostComment')); -const SlideToc = dynamic(() => import('components/post/SlideToc')); - -interface Props extends MyMatters { - showTOC?: boolean; - children: React.ReactElement; -} - -const MDXLayout = ({ title, date, showTOC = true, children }: Props) => { - return ( - <> - - -
    -

    {title}

    - - {showTOC && } - -
    - {children} - -
    -
    - -