fix: update paths for style resources in electron-builder configuration and ExportService

This commit is contained in:
suyao 2025-09-12 15:10:52 +08:00
parent bb76f83367
commit 1ec308f9a3
No known key found for this signature in database
3 changed files with 50 additions and 5 deletions

View File

@ -11,10 +11,6 @@ export const PRINT_HTML_TEMPLATE = `
<meta charset="utf-8">
<title>{{filename}}</title>
<style>
@page {
margin: 1cm;
size: A4;
}
@media print {
body {
-webkit-print-color-adjust: exact;

View File

@ -438,12 +438,36 @@ export class ExportService {
const fontCss = await loadCssFile('font.css')
const richtextCss = await loadCssFile('richtext.css')
// PDF专用样式解决代码块溢出问题
const pdfSpecificCss = `
@media print {
.tiptap pre {
white-space: pre-wrap !important;
word-wrap: break-word !important;
overflow-wrap: break-word !important;
max-width: 100% !important;
box-sizing: border-box !important;
}
.tiptap pre code {
white-space: pre-wrap !important;
word-wrap: break-word !important;
overflow-wrap: break-word !important;
}
.tiptap {
max-width: 100% !important;
overflow: hidden !important;
}
}
`
const tempHtmlPath = path.join(os.tmpdir(), `temp_${Date.now()}.html`)
await fs.promises.writeFile(
tempHtmlPath,
PRINT_HTML_TEMPLATE.replace('{{filename}}', filename.replace('.pdf', ''))
.replace('{{colorCss}}', colorCss)
.replace('{{richtextCss}}', richtextCss)
.replace('{{richtextCss}}', richtextCss + pdfSpecificCss)
.replace('{{fontCss}}', fontCss)
.replace('{{content}}', content)
)

View File

@ -55,7 +55,32 @@ const addSyntaxHighlighting = async (html: string): Promise<string> => {
const highlightedCode = tempDoc.querySelector('code')
if (highlightedCode) {
// 保留原有的类名和属性
const originalClasses = codeElement.className
const originalAttributes = Array.from(codeElement.attributes)
// 替换内容
codeElement.innerHTML = highlightedCode.innerHTML
// 合并类名
const highlightedClasses = highlightedCode.className
const mergedClasses = [originalClasses, highlightedClasses]
.filter((cls) => cls && cls.trim())
.join(' ')
.split(' ')
.filter((cls, index, arr) => cls && arr.indexOf(cls) === index) // 去重
.join(' ')
if (mergedClasses) {
codeElement.className = mergedClasses
}
// 保留原有的其他属性除了class
originalAttributes.forEach((attr) => {
if (attr.name !== 'class' && !codeElement.hasAttribute(attr.name)) {
codeElement.setAttribute(attr.name, attr.value)
}
})
}
}
} catch (error) {