diff --git a/components/mdx/Anchor.tsx b/components/mdx/Anchor.tsx index c361c66..5dde533 100644 --- a/components/mdx/Anchor.tsx +++ b/components/mdx/Anchor.tsx @@ -2,13 +2,16 @@ import { AnchorHTMLAttributes } from 'react'; import cn from 'classnames'; import { FiExternalLink } from 'react-icons/fi'; -interface Props extends AnchorHTMLAttributes {} +interface Props extends AnchorHTMLAttributes { + external?: boolean; +} -const Anchor = ({ children, ...rest }: Props) => { +const Anchor = ({ children, external = true, ...rest }: Props) => { return ( <> { )} > {children} + {external && } ); diff --git a/components/post/PostTOC.tsx b/components/post/PostTOC.tsx new file mode 100644 index 0000000..7f08b73 --- /dev/null +++ b/components/post/PostTOC.tsx @@ -0,0 +1,29 @@ +import { FC } from 'react'; +import { getHeadings } from 'lib/utils'; +import Anchor from 'components/mdx/Anchor'; + +interface Props { + headings: ReturnType; +} + +const PostTOC: FC = ({ headings }) => { + return ( + <> +

What's inside?

+ +
    + {headings?.map((h) => ( + <> +
  • + + {h.text} + +
  • + + ))} +
+ + ); +}; + +export default PostTOC; diff --git a/layouts/MDXLayout.tsx b/layouts/MDXLayout.tsx index e324633..6eee5b2 100644 --- a/layouts/MDXLayout.tsx +++ b/layouts/MDXLayout.tsx @@ -1,7 +1,9 @@ import { FC } from 'react'; import dynamic from 'next/dynamic'; import { MyMatters } from 'types'; -// import { renderToString } from 'react-dom/server'; +import { renderToString } from 'react-dom/server'; +import { getHeadings } from 'lib/utils'; +import PostTOC from 'components/post/PostTOC'; const Footer = dynamic(() => import('components/Footer')); const HeadBar = dynamic(() => import('components/NavBar')); @@ -9,29 +11,8 @@ const HeadBar = dynamic(() => import('components/NavBar')); interface Props extends MyMatters {} const MainLayout: FC = ({ title, date, children }) => { - // const contentString = renderToString(children as any); - - // const getHeadings = (source: string) => { - // const regex = /

(.*?)<\/h2>/g; - - // if (source.match(regex)) { - // return source.match(regex)?.map((heading) => { - // const headingText = heading.replace('

', '').replace('

', ''); - - // const link = '#' + headingText.replace(/ /g, '_').toLowerCase(); - - // return { - // text: headingText, - // link, - // }; - // }); - // } - - // return []; - // }; - - // const headings = getHeadings(contentString); - // console.log(headings); + const contentString = renderToString(children as any); + const headings = getHeadings(contentString); return ( <> @@ -43,6 +24,8 @@ const MainLayout: FC = ({ title, date, children }) => { + + {children} diff --git a/lib/utils/index.ts b/lib/utils/index.ts index f6e8430..b7fd467 100644 --- a/lib/utils/index.ts +++ b/lib/utils/index.ts @@ -10,3 +10,30 @@ export const sortByDate = ( return 0; } }; + +/** + * Read post head to mark TOC + * @param source + * @returns + */ +export const getHeadings = (source: string) => { + const regex = /

(.*?)<\/h2>/g; + const linkRegx = /id="(.*?)"/; + + if (source.match(regex)) { + return source.match(regex)?.map((heading) => { + const headingText = heading + .replace(/

/, '') + .replace(/<\/h2>/, ''); + + const link = '#' + heading.match(linkRegx)?.[1]; + + return { + text: headingText, + link, + }; + }); + } + + return []; +}; diff --git a/next.config.mjs b/next.config.mjs index 348dbd4..a924f8f 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -1,7 +1,7 @@ import remarkFrontmatter from 'remark-frontmatter'; import mdx from '@next/mdx'; import rehypePrism from '@mapbox/rehype-prism'; -import remarkToc from 'remark-toc'; +// import remarkToc from 'remark-toc'; import composePlugins from 'next-compose-plugins'; import remarkGfm from 'remark-gfm'; import rehypeSlug from 'rehype-slug'; @@ -12,7 +12,7 @@ const composedConfig = composePlugins([ options: { remarkPlugins: [ remarkFrontmatter, - () => remarkToc({ maxDepth: 2 }), + // [remarkToc, { maxDepth: 2 }], remarkGfm, ], rehypePlugins: [rehypePrism, rehypeSlug], diff --git a/pages/p/second-post.mdx b/pages/p/second-post.mdx index 9fad517..e91c187 100644 --- a/pages/p/second-post.mdx +++ b/pages/p/second-post.mdx @@ -14,8 +14,6 @@ export const meta = { export default ({ children }) => {children}; -## Table of contents - ## Hello This is my second post. diff --git a/styles/rua.css b/styles/rua.css index 5cffb71..79faa45 100644 --- a/styles/rua.css +++ b/styles/rua.css @@ -2,15 +2,6 @@ @apply text-lg leading-10; } -/* For the table of content */ -#article h2#table-of-contents + ul { - @apply border-l-4 border-gray-300; - @apply pl-4; -} -#article h2#table-of-contents:hover::before { - content: unset; -} - #article h1 { font-weight: bold; text-align: center;