-
Notifications
You must be signed in to change notification settings - Fork 408
Expand file tree
/
Copy pathexport.ts
More file actions
251 lines (230 loc) · 7.17 KB
/
export.ts
File metadata and controls
251 lines (230 loc) · 7.17 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import unified from 'unified'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import remarkAdmonitions from 'remark-admonitions'
import remarkMath from 'remark-math'
import rehypeDocument from 'rehype-document'
import rehypeStringify from 'rehype-stringify'
import rehypeRaw from 'rehype-raw'
import rehypeSanitize from 'rehype-sanitize'
import rehypeKatex from 'rehype-katex'
import { prop } from 'ramda'
import { downloadString } from '../../design/lib/dom'
import rehypeCodeMirror from '../../design/lib/codemirror/rehypeCodeMirror'
import { SerializedDoc } from '../interfaces/db/doc'
import { filenamify } from './utils/string'
import { UserSettings } from './stores/settings'
import { boostHubBaseUrl } from './consts'
import { selectV2Theme } from '../../design/lib/styled/styleFunctions'
import { getGlobalCss } from '../../design/components/atoms/GlobalStyle'
import {
appendElementToBody,
rehypeChart,
rehypeFlowChart,
rehypeMermaid,
remarkCharts,
remarkPlantUML,
} from './charts'
import { schema } from '../components/MarkdownView'
export function filenamifyTitle(title: string): string {
return filenamify(title.toLowerCase().replace(/\s+/g, '-'))
}
const remarkAdmonitionOptions = {
tag: ':::',
icons: 'emoji',
infima: false,
}
export const exportAsHtmlFile = async (
doc: SerializedDoc,
preferences: UserSettings,
previewStyle?: string
): Promise<void> => {
if (doc.head == null) {
return
}
const file = await unified()
.use(remarkParse)
.use(remarkMath)
.use(remarkAdmonitions, remarkAdmonitionOptions)
.use(remarkPlantUML, { server: 'https://www.plantuml.com/plantuml' })
.use(remarkCharts)
.use([remarkRehype, { allowDangerousHTML: false }])
.use(rehypeCodeMirror, {
ignoreMissing: true,
theme: preferences['general.codeBlockTheme'],
})
.use(rehypeRaw)
.use(rehypeSanitize, schema)
.use(rehypeDocument, {
title: doc.title,
style: previewStyle,
css: getCssLinks(preferences),
meta: { keywords: (doc.tags || []).map(prop('text')).join() },
script: `${appendElementToBody.toString()}
appendElementToBody('chart-export-container', 'canvas')
appendElementToBody('flowchart-export-container', 'div')
`,
})
.use(rehypeMermaid)
.use(rehypeFlowChart)
.use(rehypeChart, { tagName: 'chart' })
.use(rehypeChart, { tagName: 'chart(yaml)', isYml: true })
.use(rehypeKatex, { output: 'htmlAndMathml' })
.use(rehypeStringify)
.process(doc.head.content)
downloadString(
file.toString(),
`${filenamifyTitle(doc.title)}.html`,
'text/html'
)
}
export const exportAsMarkdownFile = async (
doc: SerializedDoc,
{ includeFrontMatter }: { includeFrontMatter: boolean }
): Promise<void> => {
if (doc.head == null) {
return
}
let content = doc.head.content.trim() + '\n'
if (includeFrontMatter) {
content =
[
'---',
`title: "${doc.title}"`,
`tags: "${(doc.tags || []).map(prop('text')).join()}"`,
'---',
'',
'',
].join('\n') + content
}
downloadString(content, `${filenamifyTitle(doc.title)}.md`, 'text/markdown')
}
const fetchCorrectMdThemeName = (theme: string, appTheme: string) => {
if (theme === 'solarized-dark') {
return 'solarized'
}
if (theme === 'default') {
if (appTheme === 'light') {
// Fix: Return 'default' theme instead of null to ensure code block styling
return 'default'
}
return 'material-darker'
}
return theme
}
const getCssLinks = (settings: UserSettings) => {
const cssHrefs: string[] = []
cssHrefs.push(boostHubBaseUrl + '/app/codemirror/theme/codemirror.css')
cssHrefs.push(boostHubBaseUrl + '/app/katex/katex.min.css')
cssHrefs.push(boostHubBaseUrl + '/app/remark-admonitions/classic.css')
const editorTheme = fetchCorrectMdThemeName(
settings['general.editorTheme'],
settings['general.theme']
)
if (editorTheme != null) {
const editorThemePath =
boostHubBaseUrl + `/app/codemirror/theme/${editorTheme}.css`
cssHrefs.push(editorThemePath)
}
const markdownCodeBlockTheme = fetchCorrectMdThemeName(
settings['general.codeBlockTheme'],
settings['general.theme']
)
if (
markdownCodeBlockTheme != null &&
markdownCodeBlockTheme !== editorTheme
) {
cssHrefs.push(
boostHubBaseUrl + `/app/codemirror/theme/${markdownCodeBlockTheme}.css`
)
}
return cssHrefs
}
const cssStyleLinkGenerator = (href: string) =>
`<link rel="stylesheet" href="${href}" type="text/css"/>`
const generatePrintToPdfHTML = (
markdownHTML: string | Uint8Array,
settings: UserSettings,
previewStyle?: string
) => {
const cssHrefs: string[] = getCssLinks(settings)
const generalThemeName = settings['general.theme']
const cssLinks = cssHrefs
.map((href) => cssStyleLinkGenerator(href))
.join('\n')
const themedGlobalCss = getGlobalCss(selectV2Theme(generalThemeName))
const previewStyleCssEl = previewStyle ? `` : ''
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<!-- Preview styles -->
<style>${themedGlobalCss}</style>
${previewStyle != null ? `<style>${previewStyle}</style>` : ''}
${previewStyleCssEl}
<!-- External Css files -->
${cssLinks}
<!-- Print Styles -->
<style media="print">
pre code {
white-space: pre-wrap;
}
/* Fix: Ensure code blocks have proper contrast in exported PDF/HTML */
.CodeMirror {
background-color: #f5f5f5 !important;
color: #333 !important;
padding: 1em !important;
border-radius: 4px !important;
}
.CodeMirror .cm-s-default {
background-color: #f5f5f5 !important;
color: #333 !important;
}
/* Ensure code text is readable */
pre .CodeMirror-lines {
color: #333 !important;
}
/* Fix for dark themes in light mode exports */
.light .CodeMirror,
.light .CodeMirror-cursor {
background-color: #f5f5f5 !important;
color: #333 !important;
}
</style>
</head>
<body>
<div class="${generalThemeName}">
${markdownHTML}
</div>
</body>
</html>
`
}
export async function convertMarkdownToPdfExportableHtml(
content: string,
preferences: UserSettings,
previewStyle?: string
): Promise<string> {
const file = await unified()
.use(remarkParse)
.use(remarkAdmonitions, remarkAdmonitionOptions)
.use(remarkMath)
.use([remarkRehype, { allowDangerousHTML: true }])
.use(rehypeRaw)
.use(rehypeSanitize, schema)
.use(rehypeCodeMirror, {
ignoreMissing: true,
theme: preferences['general.codeBlockTheme'],
})
.use(rehypeKatex, { output: 'htmlAndMathml' })
.use(rehypeStringify)
.process(content)
const stringifiedMdContent = file.toString().trim() + '\n'
const htmlString = generatePrintToPdfHTML(
stringifiedMdContent,
preferences,
previewStyle
)
return htmlString
}