-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdown.ts
183 lines (164 loc) · 6.2 KB
/
markdown.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import katex from "katex"
import { marked } from "marked"
import hljs from "highlight.js"
import * as fm from 'front-matter'
import { File } from "./file.js"
import * as fs from "node:fs"
const katexCss = String.raw`<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css"
integrity="sha384-ZPe7yZ91iWxYumsBEOn7ieg8q/o+qh/hQpSaPow8T6BwALcXSCS6C6fSRPIAnTQs" crossorigin="anonymous">`
const highlightCss = String.raw`<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/styles/default.min.css">`
const mkStyleSheet = [katexCss, highlightCss].join("\n")
type MKHeadItem = {
anchor: string
level: number
text: string
children: MKHeadItem[]
}
type MKRenderResult = {
result: string
headList: MKHeadItem[]
}
export class MarkDownData {
constructor(
public title: string,
public abstract: string,
public createDate: Date,
public editDate: Date,
public html: string,
public stylesheet: string,
public relPath: string,
public frontMatter: any,
public headList: MKHeadItem[]
) {}
}
export class MarkDownUtil {
static headListSupport(renderer: marked.Renderer, toc: MKHeadItem[]) {
renderer.heading = function (this: any, text, level, raw, slugger) {
// https://github.com/markedjs/marked/issues/545
// https://github.com/markedjs/marked/blob/f65264d6b4a831dd8e25a41012e3c7dc4866ea7a/src/Renderer.ts#L51
// NOTE: keep this.options.headerIds true
const anchor = this.options.headerPrefix + slugger.slug(raw)
toc.push({
anchor: anchor,
level: level,
text: text,
children: [],
})
return `<h${level} id="${anchor}">${text}</h${level}>\n`
}
}
private static _toHeadItemTree(headList: MKHeadItem[], start: number): number {
const rootHead = headList[start]
let cur = start + 1
while (cur < headList.length) {
const curHead = headList[cur]
if (curHead.level <= rootHead.level) {
break
}
rootHead.children.push(curHead)
cur = this._toHeadItemTree(headList, cur)
}
return cur
}
private static toHeadItemTree(headList: MKHeadItem[]): MKHeadItem[] {
const result: MKHeadItem[] = []
let cur = 0
while (cur < headList.length) {
result.push(headList[cur])
cur = this._toHeadItemTree(headList, cur)
}
return result
}
// see https://github.com/markedjs/marked/issues/1538
public static renderMarkdown(mkdown: string): MKRenderResult {
marked.setOptions({
highlight: function (code: string, lang: string) {
const language = hljs.getLanguage(lang) ? lang : "plaintext"
return hljs.highlight(code, { language }).value
},
langPrefix: "hljs language-",
})
let newRenderer = new marked.Renderer()
let cnt = 0
let math_expressions: {
[key: string]: { type: "block" | "inline"; expression: string }
} = {}
const next_id = () => `__special_katex_id__${cnt++}`
const replace_with_math_ids = (text: string) => {
text = text.replace(/\$\$([\S\s]+?)\$\$/g, (_match, expression) => {
let cur_id = next_id()
math_expressions[cur_id] = { type: "block", expression }
return cur_id
})
text = text.replace(/\$([\S\s]+?)\$/g, (_match, expression) => {
let cur_id = next_id()
math_expressions[cur_id] = { type: "inline", expression }
return cur_id
})
return text
}
mkdown = replace_with_math_ids(mkdown)
let headList: MKHeadItem[] = []
this.headListSupport(newRenderer, headList)
let render_result = marked(mkdown, { renderer: newRenderer })
render_result = render_result.replace(
/(__special_katex_id__\d+)/g,
(_match, capture) => {
const { type, expression } = math_expressions[capture]
const katexCode = katex.renderToString(expression, {
displayMode: type === "block",
})
if (type === "block") {
return `<div class="_hi_katex_block_math">${katexCode}</div>`
} else {
return `<span class="_hi_katex_inline_math">${katexCode}</span>`
}
}
)
return {
result: render_result,
headList: this.toHeadItemTree(headList),
}
}
public static async configure(file: File): Promise<MarkDownData> {
if (file.content === undefined) {
file.content = (await fs.promises.readFile(file.getSrcAbsPath())).toString("utf-8")
}
const relPath = file.getSrcRelPath()
const fmRes = fm.default(file.content as string)
const frontMatter: any = fmRes.attributes
const renderRes = MarkDownUtil.renderMarkdown(fmRes.body)
let title = ""
{
let minHeader = 9999
for (var header of renderRes.headList) {
if (header.level < minHeader) {
minHeader = header.level
title = header.text
}
}
}
let abstract = ""
if ("describe" in frontMatter) {
abstract = frontMatter.describe
} else if ("abstract" in frontMatter) {
abstract = frontMatter.abstract
}
let createDate: Date
let editDate: Date
{
const stat = await fs.promises.stat(file.getSrcAbsPath())
createDate = stat.birthtime
editDate = stat.mtime
if ("date" in frontMatter) {
createDate = new Date(frontMatter.date)
}
}
const html = `<div class="markdown">${renderRes.result}</div>`
return new MarkDownData(
title, abstract, createDate, editDate, html, mkStyleSheet,
relPath, frontMatter, renderRes.headList
)
}
}