From 3c418d9229bf4023816fae82e5524ae902c0e982 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Mon, 29 Mar 2021 10:18:51 +0200 Subject: [meta] tag parsing --- pages/index.tsx | 2 +- pages/post/[id].tsx | 37 ++++++++++++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 6 deletions(-) (limited to 'pages') diff --git a/pages/index.tsx b/pages/index.tsx index 1f44fec..b4f8993 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -1,4 +1,4 @@ -import Seperator from '../components/articleSeperator'; +import Seperator from '../components/seperator'; import Navbar from '../components/navbar'; import Button from '../components/button'; import Image from '../components/image'; diff --git a/pages/post/[id].tsx b/pages/post/[id].tsx index 8a1dce1..f70b21f 100644 --- a/pages/post/[id].tsx +++ b/pages/post/[id].tsx @@ -2,16 +2,17 @@ import ReactMarkdown from 'react-markdown'; import { readdirSync, readFileSync } from 'fs'; import { join } from 'path'; -// import Seperator from '../../components/articleSeperator'; +import Seperator from '../../components/seperator'; import Navbar from '../../components/navbar'; // import Button from '../../components/button'; -// import Image from '../../components/image'; +import Image from '../../components/image'; import Chapters, { chapter } from '../../components/chapters'; interface ArticleMeta { title?: string; subtitle?: string; - date?: Date; + author?: string; + date?: string; chapters?: Array; } @@ -32,15 +33,41 @@ export default function Post(props: {
- +
} +var parseTag = { + "title": (val: string) => val, + "subtitle": (val: string) => val, + "author": (val: string) => val, + "date": (val: string) => new Date(val).toDateString(), +} + +function parseMeta(file: Array) { + var meta: ArticleMeta = {}; + + file.forEach(line => { + if (!line.startsWith("[meta]: ")) return; + var tags = line.match(/\[meta\]:\s+\<(.+?)\>\s+\((.+?)\)/); + if (!tags || !tags[1] || !tags[2]) return; + if (!parseTag.hasOwnProperty(tags[1])) return; + meta[tags[1]] = parseTag[tags[1]](tags[2]); + }); + + return meta; +} + function preprocessor(fileContent: string) { var fileAsArr = fileContent.split("\n"); - var meta: ArticleMeta = {}; + var meta = parseMeta(fileAsArr); var result = fileAsArr.join("\n").trim() return { meta, result } -- cgit v1.2.3