-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmd2html.ts
More file actions
165 lines (147 loc) · 4.56 KB
/
md2html.ts
File metadata and controls
165 lines (147 loc) · 4.56 KB
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
import fs from 'node:fs'
import path from 'node:path'
import { unified } from 'unified'
import { fromHtmlIsomorphic } from 'hast-util-from-html-isomorphic'
import {
transformerNotationDiff,
transformerNotationFocus,
transformerMetaHighlight,
} from '@shikijs/transformers'
import remarkGfm from 'remark-gfm'
import remarkMath from 'remark-math'
import remarkParse from 'remark-parse'
import remarkEmoji from 'remark-emoji'
import remarkRehype from 'remark-rehype'
import remarkHeadingId from 'remark-heading-id'
import { remarkAlert } from 'remark-github-blockquote-alert'
import rehypeRaw from 'rehype-raw'
import rehypeKatex from 'rehype-katex'
import rehypeShiki from '@shikijs/rehype'
import rehypeStringify from 'rehype-stringify'
import rehypeAutolinkHeadings from 'rehype-autolink-headings'
import { config } from './config.ts'
const glmCSS = fs
.readFileSync(path.join(import.meta.dirname, './css/glm.css'), 'utf8')
.replace(/[\r\n]/g, '')
const katexCSS = fs
.readFileSync(path.join(import.meta.dirname, './css/katex.css'), 'utf8')
.replace(/[\r\n]/g, '')
const globalCSS = fs
.readFileSync(path.join(import.meta.dirname, './css/global.css'), 'utf8')
.replace(/[\r\n]/g, '')
const shikiCSS = fs
.readFileSync(path.join(import.meta.dirname, './css/shiki.css'), 'utf8')
.replace(/[\r\n]/g, '')
const linkSvg = fs.readFileSync(path.join(import.meta.dirname, './assets/link.svg'), 'utf8')
interface Md2htmlOptions {
title?: string
color?: 'dark' | 'light' | 'auto'
darkTheme?: string
lightTheme?: string
defaultColor?: string
lineNumbers?: boolean
}
export async function md2html(md: string, options: Md2htmlOptions = {}): Promise<string> {
const {
title = '',
color = 'auto',
lineNumbers = false,
darkTheme = config.darkTheme,
lightTheme = config.lightTheme,
defaultColor = config.defaultColor,
} = options
const enableKatex = md.includes('$$') || md.includes('\\(') || md.includes('\\[')
const finalTitle =
title || (md.match(/#+\s*(.+)\n?/)?.[1] || 'md2html').replace(/\s*\{#.+\}\s*/g, '').trim()
const processor = unified()
.use(remarkParse)
.use(remarkEmoji, {
accessible: true,
padSpaceAfter: true,
})
.use(remarkHeadingId, {
defaults: true,
uniqueDefaults: true,
})
.use(remarkAlert, { legacyTitle: true })
.use(remarkMath)
.use(remarkGfm)
.use(remarkRehype, {
allowDangerousHtml: true,
})
.use(rehypeShiki, {
defaultColor,
themes: {
dark: darkTheme,
light: lightTheme,
},
transformers: [
transformerNotationDiff(), // like: +const a = 1
transformerNotationFocus(), // like: // [!code focus]
transformerMetaHighlight(), // like: ```js {1,3-5}
],
})
.use(rehypeAutolinkHeadings, {
properties: {
class: 'anchor',
},
content: fromHtmlIsomorphic(linkSvg).children as never,
})
.use(rehypeKatex)
.use(rehypeRaw, { tagfilter: true })
.use(rehypeStringify)
const mainHTML = await processor.process(md)
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${finalTitle}</title>
<style>
body { margin: 2rem auto; }
main { max-width: 800px; margin: 0 auto; padding: 1rem; }
${globalCSS}
${glmCSS}
${enableKatex ? katexCSS : ''}
${shikiCSS}
${
lineNumbers
? `
pre.shiki code {
counter-reset: step;
counter-increment: step 0;
}
pre.shiki code .line::before {
content: counter(step);
counter-increment: step;
min-width: 1rem;
margin-right: 0.8rem;
display: inline-block;
text-align: right;
color: rgba(115, 138, 148, 0.4) !important;
}
`
: ''
}
</style>
</head>
<body class="markdown-body">
<main>${mainHTML.toString()}</main>
<script>
${
color === 'auto'
? `
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', e => {
document.documentElement.classList.remove('dark', 'light');
document.documentElement.classList.add(e.matches ? 'dark' : 'light');
});
const colorSchema = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';`
: color === 'dark'
? `const colorSchema = 'dark';`
: `const colorSchema = 'light';`
}
document.documentElement.classList.add(colorSchema);
</script>
</body>
</html>`
}