Skip to content

Commit 7a3144d

Browse files
committed
feat(toolbar): Extract HTML generation logic into reusable function
1 parent 359bc07 commit 7a3144d

1 file changed

Lines changed: 22 additions & 89 deletions

File tree

  • site/src/components/edit/toolbar

site/src/components/edit/toolbar/File.vue

Lines changed: 22 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,11 @@ const { data } = useDataStore();
4949
const { styles } = useStyleStore();
5050
const saveName = computed(() => data.curResumeName.trim().replace(/\s+/g, "_"));
5151
52-
const exportPDF = () => {
53-
const title = document.title;
54-
55-
document.title = saveName.value;
56-
window.print();
57-
document.title = title;
58-
};
59-
60-
const exportMd = () => {
61-
downloadFile(`${saveName.value}.md`, data.mdContent);
62-
};
63-
64-
const exportHtml = () => {
52+
// Generate complete HTML document with styles
53+
const generateHtmlDocument = () => {
6554
const html = renderMarkdown(data.mdContent);
6655
6756
// Get paper dimensions in pixels
68-
const paperWidthMm = PAPER[styles.paper].w;
69-
const paperHeightMm = PAPER[styles.paper].h;
7057
const paperWidthPx = getPaperPx(styles.paper, 'w');
7158
7259
// Generate dynamic CSS based on current styles
@@ -165,92 +152,38 @@ ${html}
165152
</body>
166153
</html>`;
167154
155+
return htmlDocument;
156+
};
157+
158+
const exportPDF = () => {
159+
const title = document.title;
160+
161+
document.title = saveName.value;
162+
window.print();
163+
document.title = title;
164+
};
165+
166+
const exportMd = () => {
167+
downloadFile(`${saveName.value}.md`, data.mdContent);
168+
};
169+
170+
const exportHtml = () => {
171+
const htmlDocument = generateHtmlDocument();
168172
downloadFile(`${saveName.value}.html`, htmlDocument);
169173
};
170174
171175
const exportDocx = async () => {
172176
try {
173-
const html = renderMarkdown(data.mdContent);
174-
175-
// Generate inline CSS for DOCX
176-
const inlineCss = `
177-
<style>
178-
body {
179-
font-family: ${styles.fontEN.fontFamily || styles.fontEN.name}, ${styles.fontCJK.fontFamily || styles.fontCJK.name};
180-
font-size: ${styles.fontSize}pt;
181-
line-height: ${styles.lineHeight.toFixed(2)};
182-
color: #000000;
183-
}
184-
185-
a {
186-
color: ${styles.themeColor};
187-
}
188-
189-
h1, h2, h3 {
190-
color: ${styles.themeColor};
191-
}
192-
193-
h1 {
194-
font-size: 2.5em;
195-
letter-spacing: 0.1em;
196-
text-align: center;
197-
margin-bottom: 0.25em;
198-
border-bottom: 1px solid ${styles.themeColor};
199-
}
200-
201-
h2, h3 {
202-
margin-bottom: 0.25em;
203-
margin-top: ${styles.paragraphSpace}px;
204-
font-size: 1.2em;
205-
border-bottom: 1px solid ${styles.themeColor};
206-
}
207-
208-
p, li {
209-
line-height: ${styles.lineHeight.toFixed(2)};
210-
margin: 0;
211-
}
212-
213-
ul, ol {
214-
padding-left: 1.5em;
215-
margin: 0.2em 0 1.0em 0;
216-
}
217-
218-
.resume-header {
219-
text-align: center;
220-
}
221-
222-
.resume-header h1 {
223-
text-align: center;
224-
line-height: 1;
225-
margin-bottom: 8px;
226-
}
227-
228-
.resume-header-item:not(.no-separator)::after {
229-
content: " | ";
230-
}
231-
</style>
232-
`;
233-
234-
// Create HTML document for DOCX conversion
235-
const docxHtml = `<!DOCTYPE html>
236-
<html lang="en">
237-
<head>
238-
<meta charset="UTF-8">
239-
<title>${data.curResumeName}</title>
240-
${inlineCss}
241-
</head>
242-
<body>
243-
${html}
244-
</body>
245-
</html>`;
177+
// Get the same HTML document used for HTML export
178+
const htmlDocument = generateHtmlDocument();
246179
247180
// Dynamic import of the libraries
248181
const { asBlob } = await import('html-docx-js-typescript');
249182
// @ts-ignore - file-saver types not available
250183
const { saveAs } = await import('file-saver');
251184
252185
// Convert HTML to DOCX and save
253-
asBlob(docxHtml).then(data => {
186+
asBlob(htmlDocument).then(data => {
254187
saveAs(data, `${saveName.value}.docx`);
255188
});
256189
} catch (error) {

0 commit comments

Comments
 (0)