This commit is contained in:
Kim, Jimin 2022-01-13 17:08:13 +09:00
parent da60f14c1e
commit 27c33e1164
3 changed files with 46 additions and 31 deletions

View file

@ -50,17 +50,6 @@ const md = markdownIt({
.use(highlightLines)
.use(markdownItFootnote)
export default function parseMarkdown(markdownRaw: string): string {
return (
// todo: accurately calculate start and end of front matter
md.render(markdownRaw.slice(nthIndex(markdownRaw, "---", 2) + 3)) || ""
)
}
export function generateToc(markdownRaw: string): string {
return md.render(toc(markdownRaw).content)
}
/**
* parse the front matter if it exists
*
@ -68,7 +57,7 @@ export function generateToc(markdownRaw: string): string {
* @param {string} path - filename of the markdown file
* @param {ParseMode} mode
*/
export function parseFrontMatter(
export default function parseMarkdown(
markdownRaw: string,
path: string,
mode: ParseMode
@ -85,14 +74,38 @@ export function parseFrontMatter(
throw Error(`Date is not defined in file: ${path}`)
}
const dom = new JSDOM(parseMarkdown(markdownRaw))
//
// work with rendered DOM
//
const dom = new JSDOM(
md.render(markdownRaw.slice(nthIndex(markdownRaw, "---", 2) + 3)) || ""
)
// add .hljs class to all block codes
dom.window.document.querySelectorAll("pre > code").forEach((item) => {
item.classList.add("hljs")
})
// add parent div to tables (horizontally scroll table on small displays)
dom.window.document.querySelectorAll("table").forEach((item) => {
// `element` is the element you want to wrap
const parent = item.parentNode
if (!parent) return // stop if table doesn't have a parent node
const wrapper = dom.window.document.createElement("div")
wrapper.style.overflowX = "auto"
parent.replaceChild(wrapper, item)
wrapper.appendChild(item)
})
frontMatter.content = dom.window.document.documentElement.innerHTML
return frontMatter as MarkdownData
}
export function generateToc(markdownRaw: string): string {
return md.render(toc(markdownRaw).content)
}

View file

@ -2,7 +2,7 @@ import fs from "fs"
import readTimeEstimate from "read-time-estimate" // post read time estimation
import { path2FileOrFolderName, path2URL } from "../util"
import { parseFrontMatter } from "../parseMarkdown"
import parseMarkdown from "../parseMarkdown"
import { ParseMode } from "../../types/types"
import parsePost from "./parsePost"
@ -69,7 +69,7 @@ function parseFile(mode: ParseMode, path: string): void {
*/
const markdownRaw = fs.readFileSync(path, "utf8")
const markdownData = parseFrontMatter(markdownRaw, path, mode)
const markdownData = parseMarkdown(markdownRaw, path, mode)
const { humanizedDuration, totalWords } = readTimeEstimate(
markdownData.content,
275,

View file

@ -117,26 +117,28 @@ const kbdCSS = css`
const tableCSS = css`
table {
border-collapse: collapse;
border-spacing: 0;
width: 100%;
}
table td,
table th {
td,
th {
padding: 8px;
border: ${(props) =>
theming.theme(props.theme.currentTheme, {
light: "1px solid #ddd",
dark: "1px solid #777777",
})};
padding: 8px;
}
table tr:nth-child(even) {
/* table alternating color */
tr:nth-child(even) {
background-color: ${(props) =>
theming.theme(props.theme.currentTheme, {
light: "#f2f2f2",
dark: "#21272E",
})};
}
}
`
const blockquoteCSS = css`