forked from marktext/marktext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf.js
More file actions
146 lines (128 loc) · 4.58 KB
/
pdf.js
File metadata and controls
146 lines (128 loc) · 4.58 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
import fs from 'fs'
import path from 'path'
import Slugger from 'muya/lib/parser/marked/slugger'
import { isFile } from 'common/filesystem'
import { escapeHTML, unescapeHTML } from 'muya/lib/utils'
import academicTheme from '@/assets/themes/export/academic.theme.css'
import liberTheme from '@/assets/themes/export/liber.theme.css'
import { cloneObj } from '../util'
import { sanitize, EXPORT_DOMPURIFY_CONFIG } from '../util/dompurify'
export const getCssForOptions = options => {
const {
type,
pageMarginTop,
pageMarginRight,
pageMarginBottom,
pageMarginLeft,
fontFamily,
fontSize,
lineHeight,
autoNumberingHeadings,
showFrontMatter,
theme,
headerFooterFontSize
} = options
const isPrintable = type !== 'styledHtml'
let output = ''
if (isPrintable) {
output += `@media print{@page{
margin: ${pageMarginTop}mm ${pageMarginRight}mm ${pageMarginBottom}mm ${pageMarginLeft}mm;}`
}
// Font options
output += '.markdown-body{'
if (fontFamily) {
output += `font-family:"${fontFamily}",${FALLBACK_FONT_FAMILIES};`
output = `.hf-container{font-family:"${fontFamily}",${FALLBACK_FONT_FAMILIES};}${output}`
}
if (fontSize) {
output += `font-size:${fontSize}px;`
}
if (lineHeight) {
output += `line-height:${lineHeight};`
}
output += '}'
// Auto numbering headings via CSS
if (autoNumberingHeadings) {
output += autoNumberingHeadingsCss
}
// Hide front matter
if (!showFrontMatter) {
output += 'pre.front-matter{display:none!important;}'
}
if (theme) {
if (theme === 'academic') {
output += academicTheme
} else if (theme === 'liber') {
output += liberTheme
} else {
// Read theme from disk
const { userDataPath } = global.marktext.paths
const themePath = path.join(userDataPath, 'themes/export', theme)
if (isFile(themePath)) {
try {
const themeCSS = fs.readFileSync(themePath, 'utf8')
output += themeCSS
} catch (_) {
// No-op
}
}
}
}
if (headerFooterFontSize) {
output += `.page-header .hf-container,
.page-footer-fake .hf-container,
.page-footer .hf-container {
font-size: ${headerFooterFontSize}px;
}`
}
if (isPrintable) {
// Close @page
output += '}'
}
return unescapeHTML(sanitize(escapeHTML(output), EXPORT_DOMPURIFY_CONFIG))
}
const generateHtmlToc = (tocList, slugger, currentLevel, options) => {
if (!tocList || tocList.length === 0) {
return ''
}
const topLevel = tocList[0].lvl
if (!options.tocIncludeTopHeading && topLevel <= 1) {
tocList.shift()
return generateHtmlToc(tocList, slugger, currentLevel, options)
} else if (topLevel <= currentLevel) {
return ''
}
const { content, lvl } = tocList.shift()
const slug = slugger.slug(content)
let html = `<li><span><a class="toc-h${lvl}" href="#${slug}">${content}</a><span class="dots"></span></span>`
// Generate sub-items
if (tocList.length !== 0 && tocList[0].lvl > lvl) {
html += '<ul>' + generateHtmlToc(tocList, slugger, lvl, options) + '</ul>'
}
html += '</li>' + generateHtmlToc(tocList, slugger, currentLevel, options)
return html
}
export const getHtmlToc = (toc, options = {}) => {
const list = cloneObj(toc)
const slugger = new Slugger()
const tocList = generateHtmlToc(list, slugger, 0, options)
if (!tocList) {
return ''
}
const title = options.tocTitle ? options.tocTitle : 'Table of Contents'
const html = `<div class="toc-container"><p class="toc-title">${title}</p><ul class="toc-list">${tocList}</ul></div>`
return sanitize(html, EXPORT_DOMPURIFY_CONFIG)
}
// Don't use "Noto Color Emoji" because it will result in PDF files with multiple MB and weird looking emojis.
const FALLBACK_FONT_FAMILIES = '"Open Sans","Segoe UI","Helvetica Neue",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji"'
const autoNumberingHeadingsCss = `body {counter-reset: h2}
h2 {counter-reset: h3}
h3 {counter-reset: h4}
h4 {counter-reset: h5}
h5 {counter-reset: h6}
h2:before {counter-increment: h2; content: counter(h2) ". "}
h3:before {counter-increment: h3; content: counter(h2) "." counter(h3) ". "}
h4:before {counter-increment: h4; content: counter(h2) "." counter(h3) "." counter(h4) ". "}
h5:before {counter-increment: h5; content: counter(h2) "." counter(h3) "." counter(h4) "." counter(h5) ". "}
h6:before {counter-increment: h6; content: counter(h2) "." counter(h3) "." counter(h4) "." counter(h5) "." counter(h6) ". "}
h2.nocount:before, h3.nocount:before, h4.nocount:before, h5.nocount:before, h6.nocount:before { content: ""; counter-increment: none }`