diff --git a/source/generate/parseMarkdown.ts b/source/generate/parseMarkdown.ts index 0593ddd..4078f67 100644 --- a/source/generate/parseMarkdown.ts +++ b/source/generate/parseMarkdown.ts @@ -7,6 +7,7 @@ import markdownItSub from "markdown-it-sub" // markdown subscript import markdownItSup from "markdown-it-sup" // markdown superscript import highlightLines from "markdown-it-highlight-lines" // highlighting specific lines in code blocks +import toc from "markdown-toc" // table of contents generation import hljs from "highlight.js" // code block syntax highlighting import katex from "katex" // rendering mathematical expression import "katex/contrib/mhchem" // chemical formula @@ -47,3 +48,7 @@ export default function parseMarkdown(markdownRaw: string): string { md.render(markdownRaw.slice(nthIndex(markdownRaw, "---", 2) + 3)) || "" ) } + +export function generateToc(markdownRaw: string): string { + return md.render(toc(markdownRaw).content) +} diff --git a/source/generate/recursiveParse.ts b/source/generate/recursiveParse.ts index 1e0a966..37c356d 100644 --- a/source/generate/recursiveParse.ts +++ b/source/generate/recursiveParse.ts @@ -1,11 +1,10 @@ import fs from "fs" import readTimeEstimate from "read-time-estimate" // post read time estimation import matter from "gray-matter" // parse markdown metadata -import toc from "markdown-toc" // table of contents generation import { JSDOM } from "jsdom" // HTML DOM parsing import { nthIndex, path2FileOrFolderName, path2URL, writeToJSON } from "./util" -import parseMarkdown from "./parseMarkdown" +import parseMarkdown, { generateToc } from "./parseMarkdown" import { contentDirectoryPath } from "./config" import { addDocument } from "./searchIndex" @@ -186,7 +185,7 @@ function parsePost(data: DataToPass): void { `${contentDirectoryPath}${urlPath}.json`, JSON.stringify({ content: markdownData.content, - toc: toc(markdownRaw).json, + toc: generateToc(markdownRaw), }) ) } @@ -294,7 +293,7 @@ function parseSeries(data: DataToPass): void { `${contentDirectoryPath}${urlPath}.json`, JSON.stringify({ content: markdownData.content, - toc: toc(markdownRaw).json, + toc: generateToc(markdownRaw), }) ) } diff --git a/source/src/pages/Page/Toc.tsx b/source/src/pages/Page/Toc.tsx index 5c2bca5..a2401b5 100644 --- a/source/src/pages/Page/Toc.tsx +++ b/source/src/pages/Page/Toc.tsx @@ -8,9 +8,8 @@ import styled from "styled-components" import theming from "../../styles/theming" -import { FetchedPage } from "../../../types/typing" - const StyledTocToggleButton = styled.button` + cursor: pointer; border: none; text-align: left; background-color: rgba(0, 0, 0, 0); @@ -29,7 +28,7 @@ const StyledCollapseContainer = styled.div` } ` -const Toc = (props: { fetchedPage: FetchedPage }) => { +const Toc = (props: { data?: string }) => { const [isTocOpened, setIsTocOpened] = useState( storage.getItem("isTocOpened") == "true" ) @@ -38,6 +37,8 @@ const Toc = (props: { fetchedPage: FetchedPage }) => { storage.setItem("isTocOpened", isTocOpened.toString()) }, [isTocOpened]) + if (!props.data) return <> + return ( <> { -
{props.fetchedPage.toc}
+

diff --git a/source/src/pages/Page/index.tsx b/source/src/pages/Page/index.tsx index 18eb9a2..2f86029 100644 --- a/source/src/pages/Page/index.tsx +++ b/source/src/pages/Page/index.tsx @@ -2,9 +2,8 @@ import { useState } from "react" import { Helmet } from "react-helmet-async" import { useLocation } from "react-router-dom" import styled from "styled-components" -import { HashLink } from "react-router-hash-link" -import { TocElement, FetchedPage, Map } from "../../../types/typing" +import { FetchedPage, Map } from "../../../types/typing" import MainContent from "../../components/MainContent" import Loading from "../../components/Loading" @@ -29,21 +28,6 @@ const StyledTitle = styled.h1` margin-bottom: 1rem; ` -function parseToc(tocData: TocElement[]) { - return ( -
    - {tocData.map((elem) => ( - // use elem.lvl -
  1. - - {elem.content} - -
  2. - ))} -
- ) -} - interface SeriesData { seriesHome: string prev?: string @@ -118,23 +102,14 @@ const Page = () => { } fetchContent(_isUnsearchable, url).then((fetched_content) => { - fetchedPage.content = fetched_content.content - ? fetched_content.content - : "No content" + fetchedPage.content = fetched_content.content || "No content" fetchedPage.toc = fetched_content.toc - ? parseToc(fetched_content.toc) - : undefined fetchedPage.title = _isUnsearchable ? map.unsearchable[url].title - : fetchedPage?.title - ? fetchedPage.title - : "No title" + : fetchedPage?.title || "No title" - if (!_isUnsearchable) { - fetchedPage.date = fetchedPage?.date - ? fetchedPage.date - : "Unknown date" - } + if (!_isUnsearchable) + fetchedPage.date = fetchedPage?.date || "Unknown date" setIsSeries(_isSeries) setFetchPage(fetchedPage) @@ -193,9 +168,7 @@ const Page = () => {
{/* add table of contents if it exists */} - {!!fetchedPage.toc?.props.children.length && ( - - )} + {/* page content */}