From 9eb1c1d1eee4286b6e7a124d3f5a32b3af453be0 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Sun, 28 Mar 2021 19:05:38 +0200 Subject: dynamic post routes --- pages/post/[id].tsx | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 pages/post/[id].tsx (limited to 'pages/post') diff --git a/pages/post/[id].tsx b/pages/post/[id].tsx new file mode 100644 index 0000000..8a1dce1 --- /dev/null +++ b/pages/post/[id].tsx @@ -0,0 +1,77 @@ +import ReactMarkdown from 'react-markdown'; +import { readdirSync, readFileSync } from 'fs'; +import { join } from 'path'; + +// import Seperator from '../../components/articleSeperator'; +import Navbar from '../../components/navbar'; +// import Button from '../../components/button'; +// import Image from '../../components/image'; +import Chapters, { chapter } from '../../components/chapters'; + +interface ArticleMeta { + title?: string; + subtitle?: string; + date?: Date; + chapters?: Array; +} + +export default function Post(props: { + content: string, + meta: ArticleMeta +}) { + return
+
+
+

{props.meta.title}

+

{props.meta.subtitle}

+
+
+
+ + +
+
+
+ +
+
+
+} + +function preprocessor(fileContent: string) { + var fileAsArr = fileContent.split("\n"); + var meta: ArticleMeta = {}; + + var result = fileAsArr.join("\n").trim() + return { meta, result } +} + +export function getStaticProps(props: {params: { id: string }}) { + var filename = join("posts/", props.params.id + ".md") + var filecontent = readFileSync(filename).toString().trim() + + var parsed = preprocessor(filecontent); + + return { + props: { + content: parsed.result, + meta: parsed.meta, + }, + } +} + +export function getStaticPaths() { + var files = readdirSync("posts").filter(f => f.endsWith(".md")); + + return { + paths: files.map((f) => { + return { + params: { + id: f.substr(0, f.length - 3) + } + } + }), + fallback: false, + } +} + -- cgit v1.2.3