better code splitting and new feature

- added portfolio parsing function
- moved front matter parsing to more appropriate file
This commit is contained in:
Kim, Jimin 2022-01-05 22:51:31 +09:00
parent 485b4b8112
commit 4eae414f38
4 changed files with 52 additions and 37 deletions

View file

@ -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

View file

@ -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
}

View file

@ -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)
}

View file

@ -42,6 +42,7 @@ export enum ParseMode {
POSTS,
SERIES,
UNSEARCHABLE,
PORTFOLIO,
}
export interface MarkdownData {