better code splitting and new feature
- added portfolio parsing function - moved front matter parsing to more appropriate file
This commit is contained in:
parent
485b4b8112
commit
4eae414f38
4 changed files with 52 additions and 37 deletions
|
@ -61,6 +61,7 @@ if (!fs.lstatSync(markdownPath + "/series").isDirectory())
|
||||||
recursiveParse(ParseMode.POSTS, markdownPath + "/posts")
|
recursiveParse(ParseMode.POSTS, markdownPath + "/posts")
|
||||||
recursiveParse(ParseMode.UNSEARCHABLE, markdownPath + "/unsearchable")
|
recursiveParse(ParseMode.UNSEARCHABLE, markdownPath + "/unsearchable")
|
||||||
recursiveParse(ParseMode.SERIES, markdownPath + "/series")
|
recursiveParse(ParseMode.SERIES, markdownPath + "/series")
|
||||||
|
recursiveParse(ParseMode.PORTFOLIO, markdownPath + "/portfolio")
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Post-process
|
* Post-process
|
||||||
|
|
|
@ -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 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 toc from "markdown-toc" // table of contents generation
|
||||||
import hljs from "highlight.js" // code block syntax highlighting
|
import hljs from "highlight.js" // code block syntax highlighting
|
||||||
import katex from "katex" // rendering mathematical expression
|
import katex from "katex" // rendering mathematical expression
|
||||||
import "katex/contrib/mhchem" // chemical formula
|
import "katex/contrib/mhchem" // chemical formula
|
||||||
|
|
||||||
|
import { JSDOM } from "jsdom" // HTML DOM parsing
|
||||||
|
|
||||||
import { nthIndex } from "./util"
|
import { nthIndex } from "./util"
|
||||||
|
import { MarkdownData, ParseMode } from "../types/typing"
|
||||||
|
|
||||||
const md = markdownIt({
|
const md = markdownIt({
|
||||||
// https://github.com/highlightjs/highlight.js/blob/main/SUPPORTED_LANGUAGES.md
|
// https://github.com/highlightjs/highlight.js/blob/main/SUPPORTED_LANGUAGES.md
|
||||||
highlight: function (str, lang) {
|
highlight: (str, lang) => {
|
||||||
if (lang && hljs.getLanguage(lang)) {
|
if (lang && hljs.getLanguage(lang)) {
|
||||||
try {
|
try {
|
||||||
return hljs.highlight(str, { language: lang }).value
|
return hljs.highlight(str, { language: lang }).value
|
||||||
|
@ -56,3 +60,40 @@ export default function parseMarkdown(markdownRaw: string): string {
|
||||||
export function generateToc(markdownRaw: string): string {
|
export function generateToc(markdownRaw: string): string {
|
||||||
return md.render(toc(markdownRaw).content)
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
import fs from "fs"
|
import fs from "fs"
|
||||||
import readTimeEstimate from "read-time-estimate" // post read time estimation
|
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 { path2FileOrFolderName, path2URL, writeToJSON } from "./util"
|
||||||
import parseMarkdown, { generateToc } from "./parseMarkdown"
|
import { generateToc, parseFrontMatter } from "./parseMarkdown"
|
||||||
|
|
||||||
import { contentDirectoryPath } from "./config"
|
import { contentDirectoryPath } from "./config"
|
||||||
import { addDocument } from "./searchIndex"
|
import { addDocument } from "./searchIndex"
|
||||||
|
@ -92,6 +90,10 @@ function parseFile(mode: ParseMode, path: string, fileName: string): void {
|
||||||
case ParseMode.UNSEARCHABLE:
|
case ParseMode.UNSEARCHABLE:
|
||||||
parseUnsearchable(dataToPass)
|
parseUnsearchable(dataToPass)
|
||||||
break
|
break
|
||||||
|
|
||||||
|
case ParseMode.PORTFOLIO:
|
||||||
|
parsePortfolio(dataToPass)
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -333,36 +335,6 @@ function parseUnsearchable(data: DataToPass): void {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
function parsePortfolio(data: DataToPass): void {
|
||||||
* parse the front matter if it exists
|
console.log("portfolio file:", data.path)
|
||||||
*
|
|
||||||
* @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
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,7 @@ export enum ParseMode {
|
||||||
POSTS,
|
POSTS,
|
||||||
SERIES,
|
SERIES,
|
||||||
UNSEARCHABLE,
|
UNSEARCHABLE,
|
||||||
|
PORTFOLIO,
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface MarkdownData {
|
export interface MarkdownData {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue