From 627166edff0d7c32a99f330c83fb98891a68fc81 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Tue, 30 Mar 2021 11:51:45 +0200 Subject: section links working :tada: --- pages/post/[id].tsx | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'pages/post') diff --git a/pages/post/[id].tsx b/pages/post/[id].tsx index 890fb4f..38c894c 100644 --- a/pages/post/[id].tsx +++ b/pages/post/[id].tsx @@ -1,3 +1,4 @@ +import { ReactNode } from 'react'; import ReactMarkdown from 'react-markdown'; import { readdirSync, readFileSync } from 'fs'; import { join } from 'path'; @@ -17,6 +18,21 @@ interface ArticleMeta { chapters?: Array; } +var headingLevel = (input: string) => input?.match(/^[#]+/)[0]?.length || 0; + +var sectionID = (input: string) => input + .replace(/[()\[\]{}!@#$%^&*<>?,./\;':"\\|=+]/g, "") + .replace(/\s/g, "-") + .toLowerCase(); + +function Heading(props: { + children?: ReactNode; + level?: number; +}) { + var HeadingTag = "h" + props.level as keyof JSX.IntrinsicElements; + return +} + export default function Post(props: { content: string, meta: ArticleMeta @@ -39,6 +55,7 @@ export default function Post(props: { renderers={{ image: Image, thematicBreak: Seperator, + heading: Heading, }}/> @@ -67,7 +84,6 @@ function parseMeta(file: Array): ArticleMeta { return meta; } -var headingLevel = (input: string) => input?.match(/^[#]+/)[0]?.length || 0; function parseToCRecursive(headings: Array): Array { interface WIPchapter extends chapter { unparsedChildren?: Array; @@ -79,8 +95,10 @@ function parseToCRecursive(headings: Array): Array { for (var i in headings) { var localLevel = headingLevel(headings[i]); if (localLevel == lowestLevel) { + var chapterName = headings[i].match(/^[#]+\s+(.+)/)[1]; children.push({ - name: headings[i].match(/^[#]+\s+(.+)/)[1], + name: chapterName, + sectionLink: sectionID(chapterName), unparsedChildren: [], }); currentChildIndex += 1; @@ -101,7 +119,6 @@ function parseToCRecursive(headings: Array): Array { function parseToC(file: Array): Array { var chapterStrings = file.filter(line => line.startsWith("#")); - console.log(parseToCRecursive(chapterStrings)) return parseToCRecursive(chapterStrings); } -- cgit v1.2.3