-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathmarkdown-to-pdf.js
More file actions
179 lines (154 loc) · 4.35 KB
/
markdown-to-pdf.js
File metadata and controls
179 lines (154 loc) · 4.35 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
'use strict'
const { chromium } = require('playwright-chromium')
const markdownit = require('markdown-it')
const path = require('path')
const fs = require('fs')
// Configure markdown-it similar to frontend
function createMarkdownRenderer () {
const md = markdownit('default', {
html: true,
breaks: true,
linkify: true,
typographer: true,
highlight: function (str, lang) {
try {
const hljs = require('highlight.js')
if (lang && hljs.getLanguage(lang)) {
return '<pre class="hljs"><code>' +
hljs.highlight(lang, str, true).value +
'</code></pre>'
}
} catch (error) {
// Fall back to no highlighting
}
return '<pre class="hljs"><code>' + md.utils.escapeHtml(str) + '</code></pre>'
}
})
// Add plugins commonly used in CodiMD
try {
md.use(require('markdown-it-abbr'))
md.use(require('markdown-it-footnote'))
md.use(require('markdown-it-deflist'))
md.use(require('markdown-it-mark'))
md.use(require('markdown-it-ins'))
md.use(require('markdown-it-sub'))
md.use(require('markdown-it-sup'))
} catch (error) {
// Some plugins may not be available, continue with basic rendering
console.warn('Some markdown-it plugins not available:', error.message)
}
return md
}
async function convertMarkdownToPDF (markdown, outputPath, options = {}) {
const md = createMarkdownRenderer()
// Convert markdown to HTML
const htmlContent = md.render(markdown)
// Read highlight.js CSS
const highlightCssPath = options.highlightCssPath || path.join(__dirname, '../../node_modules/highlight.js/styles/github-gist.css')
let highlightCss = ''
if (fs.existsSync(highlightCssPath)) {
highlightCss = fs.readFileSync(highlightCssPath, 'utf8')
}
// Create full HTML document
const fullHtml = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>PDF Export</title>
<style>
${highlightCss}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
line-height: 1.6;
color: #333;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
pre {
background-color: #f6f8fa;
border-radius: 6px;
padding: 16px;
overflow: auto;
}
code {
background-color: #f6f8fa;
border-radius: 3px;
padding: 2px 4px;
font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
}
pre code {
background-color: transparent;
padding: 0;
}
h1, h2, h3, h4, h5, h6 {
margin-top: 24px;
margin-bottom: 16px;
font-weight: 600;
line-height: 1.25;
}
blockquote {
padding: 0 1em;
color: #6a737d;
border-left: 0.25em solid #dfe2e5;
margin: 0;
}
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid #dfe2e5;
padding: 6px 13px;
}
th {
background-color: #f6f8fa;
font-weight: 600;
}
</style>
</head>
<body>
${htmlContent}
</body>
</html>`
// Launch Playwright Chromium and generate PDF
let browser = null
try {
browser = await chromium.launch({
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage']
})
const page = await browser.newPage()
// Set a timeout for page operations
page.setDefaultTimeout(30000)
await page.setContent(fullHtml, {
waitUntil: 'networkidle'
})
await page.pdf({
path: outputPath,
format: 'A4',
margin: {
top: '20px',
right: '20px',
bottom: '20px',
left: '20px'
},
printBackground: true
})
return true
} catch (error) {
throw new Error(`PDF generation failed: ${error.message}`)
} finally {
if (browser) {
try {
await browser.close()
} catch (closeError) {
console.warn('Failed to close browser:', closeError.message)
}
}
}
}
module.exports = {
convertMarkdownToPDF
}