From 4eae414f386f8a50ff78d76c7ec3e707d94c5fd0 Mon Sep 17 00:00:00 2001 From: developomp Date: Wed, 5 Jan 2022 22:51:31 +0900 Subject: [PATCH] better code splitting and new feature - added portfolio parsing function - moved front matter parsing to more appropriate file --- source/generate/index.ts | 1 + source/generate/parseMarkdown.ts | 43 +++++++++++++++++++++++++++++- source/generate/recursiveParse.ts | 44 ++++++------------------------- source/types/typing.ts | 1 + 4 files changed, 52 insertions(+), 37 deletions(-) diff --git a/source/generate/index.ts b/source/generate/index.ts index 751cd93..89eeb53 100644 --- a/source/generate/index.ts +++ b/source/generate/index.ts @@ -61,6 +61,7 @@ if (!fs.lstatSync(markdownPath + "/series").isDirectory()) recursiveParse(ParseMode.POSTS, markdownPath + "/posts") recursiveParse(ParseMode.UNSEARCHABLE, markdownPath + "/unsearchable") recursiveParse(ParseMode.SERIES, markdownPath + "/series") +recursiveParse(ParseMode.PORTFOLIO, markdownPath + "/portfolio") /** * Post-process diff --git a/source/generate/parseMarkdown.ts b/source/generate/parseMarkdown.ts index 67455b3..8c93188 100644 --- a/source/generate/parseMarkdown.ts +++ b/source/generate/parseMarkdown.ts @@ -9,16 +9,20 @@ import markdownItFootnote from "markdown-it-footnote" // markdown footnote import highlightLines from "markdown-it-highlight-lines" // highlighting specific lines in code blocks +import matter from "gray-matter" 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 +import { JSDOM } from "jsdom" // HTML DOM parsing + import { nthIndex } from "./util" +import { MarkdownData, ParseMode } from "../types/typing" const md = markdownIt({ // https://github.com/highlightjs/highlight.js/blob/main/SUPPORTED_LANGUAGES.md - highlight: function (str, lang) { + highlight: (str, lang) => { if (lang && hljs.getLanguage(lang)) { try { return hljs.highlight(str, { language: lang }).value @@ -56,3 +60,40 @@ export default function parseMarkdown(markdownRaw: string): string { export function generateToc(markdownRaw: string): string { return md.render(toc(markdownRaw).content) } + +/** + * parse the front matter if it exists + * + * @param {string} markdownRaw - raw unparsed text data of the markdown file + * @param {string} path - filename of the markdown file + * @param {ParseMode} mode + */ +export function parseFrontMatter( + markdownRaw: string, + path: string, + mode: ParseMode +): MarkdownData { + // todo: accurately calculate start and end of front matter + const frontMatter = matter( + markdownRaw.slice(0, nthIndex(markdownRaw, "---", 2) + 3) + ).data + + if (mode != ParseMode.PORTFOLIO) { + if (!frontMatter.title) + throw Error(`Title is not defined in file: ${path}`) + + if (mode != ParseMode.UNSEARCHABLE && !frontMatter.date) + throw Error(`Date is not defined in file: ${path}`) + } + + const dom = new JSDOM(parseMarkdown(markdownRaw)) + + // add .hljs class to all block codes + dom.window.document.querySelectorAll("pre > code").forEach((item) => { + item.classList.add("hljs") + }) + + frontMatter.content = dom.window.document.documentElement.innerHTML + + return frontMatter as MarkdownData +} diff --git a/source/generate/recursiveParse.ts b/source/generate/recursiveParse.ts index d5e8518..5ccc40d 100644 --- a/source/generate/recursiveParse.ts +++ b/source/generate/recursiveParse.ts @@ -1,10 +1,8 @@ import fs from "fs" import readTimeEstimate from "read-time-estimate" // post read time estimation -import matter from "gray-matter" // parse markdown metadata -import { JSDOM } from "jsdom" // HTML DOM parsing -import { nthIndex, path2FileOrFolderName, path2URL, writeToJSON } from "./util" -import parseMarkdown, { generateToc } from "./parseMarkdown" +import { path2FileOrFolderName, path2URL, writeToJSON } from "./util" +import { generateToc, parseFrontMatter } from "./parseMarkdown" import { contentDirectoryPath } from "./config" import { addDocument } from "./searchIndex" @@ -92,6 +90,10 @@ function parseFile(mode: ParseMode, path: string, fileName: string): void { case ParseMode.UNSEARCHABLE: parseUnsearchable(dataToPass) break + + case ParseMode.PORTFOLIO: + parsePortfolio(dataToPass) + break } } @@ -333,36 +335,6 @@ function parseUnsearchable(data: DataToPass): void { ) } -/** - * parse the front matter if it exists - * - * @param {string} markdownRaw - * @param {string} path - * @param {ParseMode} mode - */ -function parseFrontMatter( - markdownRaw: string, - path: string, - mode: ParseMode -): MarkdownData { - // todo: accurately calculate start and end of front matter - const result = matter( - markdownRaw.slice(0, nthIndex(markdownRaw, "---", 2) + 3) - ).data - - if (!result.title) throw Error(`Title is not defined in file: ${path}`) - - if (mode != ParseMode.UNSEARCHABLE && !result.date) - throw Error(`Date is not defined in file: ${path}`) - - const dom = new JSDOM(parseMarkdown(markdownRaw)) - - // add .hljs class to all block codes - dom.window.document.querySelectorAll("pre > code").forEach((item) => { - item.classList.add("hljs") - }) - - result.content = dom.window.document.documentElement.innerHTML - - return result as MarkdownData +function parsePortfolio(data: DataToPass): void { + console.log("portfolio file:", data.path) } diff --git a/source/types/typing.ts b/source/types/typing.ts index 9d1d9c0..efcafad 100644 --- a/source/types/typing.ts +++ b/source/types/typing.ts @@ -42,6 +42,7 @@ export enum ParseMode { POSTS, SERIES, UNSEARCHABLE, + PORTFOLIO, } export interface MarkdownData {