@studyllm/resume-preview 0.0.0-stage → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.en.md +318 -0
- package/README.md +313 -2
- package/build/constants.js +217 -0
- package/build/diagram-service.js +143 -0
- package/build/file-utils.js +350 -0
- package/build/handlers.js +172 -0
- package/build/html-utils.js +20 -0
- package/build/i18n.js +198 -0
- package/build/index.js +171 -0
- package/build/live-server.js +488 -0
- package/build/logger.js +92 -0
- package/build/mermaid-cli.js +48 -0
- package/build/page-renderer.js +152 -0
- package/build/preview/favicon.svg +23 -0
- package/build/preview/gallery.css +217 -0
- package/build/preview/gallery.html +59 -0
- package/build/preview/gallery.js +259 -0
- package/build/preview/i18n/en.json +57 -0
- package/build/preview/i18n/zh-CN.json +57 -0
- package/build/preview/i18n.js +125 -0
- package/build/preview/resume-template.html +13 -0
- package/build/preview/resume.css +214 -0
- package/build/preview/resume.js +393 -0
- package/build/preview/shared.css +529 -0
- package/build/resume-renderer.js +486 -0
- package/build/resume-service.js +117 -0
- package/build/routes.js +335 -0
- package/build/serve.js +46 -0
- package/build/types.js +5 -0
- package/package.json +63 -4
|
@@ -0,0 +1,486 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resume Renderer
|
|
3
|
+
* Single Responsibility: turn a resume content model into HTML, a
|
|
4
|
+
* standalone HTML export, or an A4 PDF.
|
|
5
|
+
*
|
|
6
|
+
* The renderer owns two artifacts:
|
|
7
|
+
* - `resume.html` the resume body, embedded into the preview page
|
|
8
|
+
* - `resume.export.html` a standalone document that can be opened directly
|
|
9
|
+
*
|
|
10
|
+
* Both come out of the same page template; only the shell (toolbar) and the
|
|
11
|
+
* way styles are delivered differ between preview and export mode.
|
|
12
|
+
*/
|
|
13
|
+
import { readFile } from "fs/promises";
|
|
14
|
+
import { existsSync } from "fs";
|
|
15
|
+
import { execFile } from "child_process";
|
|
16
|
+
import { promisify } from "util";
|
|
17
|
+
import { join, dirname } from "path";
|
|
18
|
+
import { fileURLToPath } from "url";
|
|
19
|
+
import puppeteer from "puppeteer";
|
|
20
|
+
import { escapeHtml } from "./html-utils.js";
|
|
21
|
+
import { TEMPLATE_FILES, ASSET_FILES, CJK_FONT_FILES, CJK_CHAR_REGEX, CJK_ALWAYS_LOCALES, TEMPLATE_VARS, ROUTES, RESUME_ROUTES, } from "./constants.js";
|
|
22
|
+
import { mcpLogger } from "./logger.js";
|
|
23
|
+
import { getDictionary, getHtmlLang, renderLanguageSwitcher, t } from "./i18n.js";
|
|
24
|
+
const execFileAsync = promisify(execFile);
|
|
25
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
26
|
+
const __dirname = dirname(__filename);
|
|
27
|
+
const PREVIEW_DIR = join(__dirname, "preview");
|
|
28
|
+
// The resume template has its own cache: the gallery template is cached
|
|
29
|
+
// separately in live-server.ts and the two must not overwrite each other.
|
|
30
|
+
const templateCache = new Map();
|
|
31
|
+
async function loadCachedAsset(filename) {
|
|
32
|
+
const cached = templateCache.get(filename);
|
|
33
|
+
if (cached)
|
|
34
|
+
return cached;
|
|
35
|
+
const content = await readFile(join(PREVIEW_DIR, filename), "utf-8");
|
|
36
|
+
templateCache.set(filename, content);
|
|
37
|
+
return content;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Loads the resume page template. Kept in its own cache so it can never
|
|
41
|
+
* collide with the gallery template.
|
|
42
|
+
*/
|
|
43
|
+
export async function loadResumeTemplate() {
|
|
44
|
+
return loadCachedAsset(TEMPLATE_FILES.RESUME_TEMPLATE);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Loads the resume stylesheet. The standalone HTML export inlines it, so the
|
|
48
|
+
* export has to carry its own copy of the rules.
|
|
49
|
+
*/
|
|
50
|
+
export async function loadResumeStyles() {
|
|
51
|
+
return loadCachedAsset(ASSET_FILES.RESUME_STYLE);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Clears the cached templates. Useful in tests and development.
|
|
55
|
+
*/
|
|
56
|
+
export function clearResumeTemplateCache() {
|
|
57
|
+
templateCache.clear();
|
|
58
|
+
}
|
|
59
|
+
// ===== Content model → HTML =====
|
|
60
|
+
function renderHighlights(items) {
|
|
61
|
+
if (!items || items.length === 0)
|
|
62
|
+
return "";
|
|
63
|
+
const list = items.map((item) => `<li>${escapeHtml(item)}</li>`).join("");
|
|
64
|
+
return `<ul class="resume-list">${list}</ul>`;
|
|
65
|
+
}
|
|
66
|
+
function renderPeriod(start, end) {
|
|
67
|
+
const parts = [start, end].filter((part) => part && part.trim().length > 0);
|
|
68
|
+
return parts.join(" – ");
|
|
69
|
+
}
|
|
70
|
+
function renderSection(title, body) {
|
|
71
|
+
if (!body)
|
|
72
|
+
return "";
|
|
73
|
+
return `<section class="resume-section">
|
|
74
|
+
<h2 class="resume-section-title">${escapeHtml(title)}</h2>
|
|
75
|
+
${body}
|
|
76
|
+
</section>`;
|
|
77
|
+
}
|
|
78
|
+
function renderBasics(basics, dictionary) {
|
|
79
|
+
if (!basics)
|
|
80
|
+
return "";
|
|
81
|
+
const contactParts = [];
|
|
82
|
+
if (basics.email)
|
|
83
|
+
contactParts.push(`<span class="resume-contact-item">${escapeHtml(basics.email)}</span>`);
|
|
84
|
+
if (basics.phone)
|
|
85
|
+
contactParts.push(`<span class="resume-contact-item">${escapeHtml(basics.phone)}</span>`);
|
|
86
|
+
if (basics.location)
|
|
87
|
+
contactParts.push(`<span class="resume-contact-item">${escapeHtml(basics.location)}</span>`);
|
|
88
|
+
if (basics.links?.length) {
|
|
89
|
+
for (const link of basics.links) {
|
|
90
|
+
contactParts.push(`<span class="resume-contact-item">${escapeHtml(link.label)}: ${escapeHtml(link.url)}</span>`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const title = basics.title ? `<p class="resume-title">${escapeHtml(basics.title)}</p>` : "";
|
|
94
|
+
const contact = contactParts.length
|
|
95
|
+
? `<div class="resume-contact">${contactParts.join("")}</div>`
|
|
96
|
+
: "";
|
|
97
|
+
return `<header class="resume-header">
|
|
98
|
+
<h1 class="resume-name">${escapeHtml(basics.name || t(dictionary, "resume.namePlaceholder"))}</h1>
|
|
99
|
+
${title}
|
|
100
|
+
${contact}
|
|
101
|
+
</header>`;
|
|
102
|
+
}
|
|
103
|
+
function renderExperience(items) {
|
|
104
|
+
if (!items?.length)
|
|
105
|
+
return "";
|
|
106
|
+
const blocks = items
|
|
107
|
+
.map((item) => {
|
|
108
|
+
const period = renderPeriod(item.start, item.end);
|
|
109
|
+
const heading = [
|
|
110
|
+
`<span class="resume-entry-title">${escapeHtml(item.title || "")}</span>`,
|
|
111
|
+
`<span class="resume-entry-org">${escapeHtml(item.company)}</span>`,
|
|
112
|
+
period ? `<span class="resume-entry-period">${escapeHtml(period)}</span>` : "",
|
|
113
|
+
item.location
|
|
114
|
+
? `<span class="resume-entry-period">${escapeHtml(item.location)}</span>`
|
|
115
|
+
: "",
|
|
116
|
+
]
|
|
117
|
+
.filter(Boolean)
|
|
118
|
+
.join("");
|
|
119
|
+
return `<article class="resume-entry">
|
|
120
|
+
<div class="resume-entry-heading">${heading}</div>
|
|
121
|
+
${renderHighlights(item.highlights)}
|
|
122
|
+
</article>`;
|
|
123
|
+
})
|
|
124
|
+
.join("");
|
|
125
|
+
return blocks;
|
|
126
|
+
}
|
|
127
|
+
function renderProjects(items) {
|
|
128
|
+
if (!items?.length)
|
|
129
|
+
return "";
|
|
130
|
+
const blocks = items
|
|
131
|
+
.map((item) => {
|
|
132
|
+
const heading = [
|
|
133
|
+
`<span class="resume-entry-title">${escapeHtml(item.name)}</span>`,
|
|
134
|
+
item.role ? `<span class="resume-entry-org">${escapeHtml(item.role)}</span>` : "",
|
|
135
|
+
item.period ? `<span class="resume-entry-period">${escapeHtml(item.period)}</span>` : "",
|
|
136
|
+
]
|
|
137
|
+
.filter(Boolean)
|
|
138
|
+
.join("");
|
|
139
|
+
return `<article class="resume-entry">
|
|
140
|
+
<div class="resume-entry-heading">${heading}</div>
|
|
141
|
+
${renderHighlights(item.highlights)}
|
|
142
|
+
</article>`;
|
|
143
|
+
})
|
|
144
|
+
.join("");
|
|
145
|
+
return blocks;
|
|
146
|
+
}
|
|
147
|
+
function renderEducation(items) {
|
|
148
|
+
if (!items?.length)
|
|
149
|
+
return "";
|
|
150
|
+
const blocks = items
|
|
151
|
+
.map((item) => {
|
|
152
|
+
const heading = [
|
|
153
|
+
`<span class="resume-entry-title">${escapeHtml(item.school)}</span>`,
|
|
154
|
+
item.degree ? `<span class="resume-entry-org">${escapeHtml(item.degree)}</span>` : "",
|
|
155
|
+
item.period ? `<span class="resume-entry-period">${escapeHtml(item.period)}</span>` : "",
|
|
156
|
+
]
|
|
157
|
+
.filter(Boolean)
|
|
158
|
+
.join("");
|
|
159
|
+
return `<article class="resume-entry">
|
|
160
|
+
<div class="resume-entry-heading">${heading}</div>
|
|
161
|
+
${renderHighlights(item.highlights)}
|
|
162
|
+
</article>`;
|
|
163
|
+
})
|
|
164
|
+
.join("");
|
|
165
|
+
return blocks;
|
|
166
|
+
}
|
|
167
|
+
function renderSkills(items, dictionary) {
|
|
168
|
+
if (!items?.length)
|
|
169
|
+
return "";
|
|
170
|
+
const separator = t(dictionary, "common.itemSeparator");
|
|
171
|
+
const groups = items
|
|
172
|
+
.map((group) => {
|
|
173
|
+
const values = (group.items || []).map((value) => escapeHtml(value)).join(separator);
|
|
174
|
+
return `<div class="resume-skill-group">
|
|
175
|
+
<span class="resume-skill-category">${escapeHtml(group.category)}</span>
|
|
176
|
+
<span class="resume-skill-items">${values}</span>
|
|
177
|
+
</div>`;
|
|
178
|
+
})
|
|
179
|
+
.join("");
|
|
180
|
+
return groups;
|
|
181
|
+
}
|
|
182
|
+
function renderExtras(items, dictionary) {
|
|
183
|
+
if (!items?.length)
|
|
184
|
+
return "";
|
|
185
|
+
const separator = t(dictionary, "common.itemSeparator");
|
|
186
|
+
const groups = items
|
|
187
|
+
.map((group) => {
|
|
188
|
+
const values = (group.items || []).map((value) => escapeHtml(value)).join(separator);
|
|
189
|
+
return `<div class="resume-skill-group">
|
|
190
|
+
<span class="resume-skill-category">${escapeHtml(group.label)}</span>
|
|
191
|
+
<span class="resume-skill-items">${values}</span>
|
|
192
|
+
</div>`;
|
|
193
|
+
})
|
|
194
|
+
.join("");
|
|
195
|
+
return groups;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Renders the resume content model into the body markup that gets stored as
|
|
199
|
+
* `resume.html`. Every user-supplied value is escaped, so the content can
|
|
200
|
+
* never introduce markup of its own.
|
|
201
|
+
*
|
|
202
|
+
* The section headings come from the dictionary of the resume's own locale —
|
|
203
|
+
* the resume language, not the browser's UI language.
|
|
204
|
+
*/
|
|
205
|
+
export async function renderResumeHtml(content, locale) {
|
|
206
|
+
const dictionary = await getDictionary(locale);
|
|
207
|
+
const sections = [];
|
|
208
|
+
sections.push(renderBasics(content.basics, dictionary));
|
|
209
|
+
if (content.summary) {
|
|
210
|
+
sections.push(renderSection(t(dictionary, "resume.sectionSummary"), `<p class="resume-text">${escapeHtml(content.summary)}</p>`));
|
|
211
|
+
}
|
|
212
|
+
sections.push(renderSection(t(dictionary, "resume.sectionExperience"), renderExperience(content.experience)));
|
|
213
|
+
sections.push(renderSection(t(dictionary, "resume.sectionProjects"), renderProjects(content.projects)));
|
|
214
|
+
sections.push(renderSection(t(dictionary, "resume.sectionEducation"), renderEducation(content.education)));
|
|
215
|
+
sections.push(renderSection(t(dictionary, "resume.sectionSkills"), renderSkills(content.skills, dictionary)));
|
|
216
|
+
sections.push(renderSection(t(dictionary, "resume.sectionExtras"), renderExtras(content.extras, dictionary)));
|
|
217
|
+
return `<div class="resume-page">${sections.filter(Boolean).join("")}</div>`;
|
|
218
|
+
}
|
|
219
|
+
// ===== Page assembly =====
|
|
220
|
+
function replaceAll(template, variables) {
|
|
221
|
+
let result = template;
|
|
222
|
+
for (const [key, value] of Object.entries(variables)) {
|
|
223
|
+
result = result.replaceAll(key, value);
|
|
224
|
+
}
|
|
225
|
+
return result;
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* The preview shell: a status bar with the live-reload indicator, zoom
|
|
229
|
+
* controls and the export menu. Preview mode only — the export document has
|
|
230
|
+
* no toolbar.
|
|
231
|
+
*/
|
|
232
|
+
function renderPreviewShell(timestamp, dictionary, options) {
|
|
233
|
+
const statusText = options.liveEnabled
|
|
234
|
+
? t(dictionary, "preview.statusLive")
|
|
235
|
+
: t(dictionary, "preview.statusStatic");
|
|
236
|
+
return `<div class="status-bar" lang="${options.locale}">
|
|
237
|
+
<div class="toolbar-buttons">
|
|
238
|
+
<span><span class="status-indicator" id="status-indicator"></span><span id="status-text">${escapeHtml(statusText)}</span></span>
|
|
239
|
+
<div class="toolbar-buttons">
|
|
240
|
+
<button class="toolbar-btn" id="back-button" title="${escapeHtml(t(dictionary, "preview.btnBack"))}">◄</button>
|
|
241
|
+
<button class="toolbar-btn" id="zoom-out" title="${escapeHtml(t(dictionary, "preview.btnZoomOut"))}">−</button>
|
|
242
|
+
<span class="zoom-level" id="zoom-level">100%</span>
|
|
243
|
+
<button class="toolbar-btn" id="zoom-in" title="${escapeHtml(t(dictionary, "preview.btnZoomIn"))}">+</button>
|
|
244
|
+
<button class="toolbar-btn" id="reset-pan" title="${escapeHtml(t(dictionary, "preview.btnReset"))}">⊙</button>
|
|
245
|
+
<div class="export-dropdown">
|
|
246
|
+
<button class="toolbar-btn" id="export-btn" title="${escapeHtml(t(dictionary, "preview.btnExport"))}">⬇</button>
|
|
247
|
+
<div class="export-menu" id="export-menu">
|
|
248
|
+
<button class="export-option" data-format="pdf">${escapeHtml(t(dictionary, "preview.exportPdf"))}</button>
|
|
249
|
+
<button class="export-option" data-format="html">${escapeHtml(t(dictionary, "preview.exportHtml"))}</button>
|
|
250
|
+
</div>
|
|
251
|
+
</div>
|
|
252
|
+
${renderLanguageSwitcher(dictionary, options.locale, options.path)}
|
|
253
|
+
</div>
|
|
254
|
+
</div>
|
|
255
|
+
<div id="last-update">${escapeHtml(t(dictionary, "preview.lastUpdated", { time: escapeHtml(timestamp) }))}</div>
|
|
256
|
+
</div>`;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Builds the full preview page around an already rendered resume body.
|
|
260
|
+
* The live server calls this per request so the port and the live-reload
|
|
261
|
+
* flag always reflect the current session.
|
|
262
|
+
*
|
|
263
|
+
* The chrome is rendered in the UI locale, the resume body keeps the
|
|
264
|
+
* language it was written in.
|
|
265
|
+
*/
|
|
266
|
+
export async function createResumeHtmlWrapper(bodyHtml, options) {
|
|
267
|
+
const template = await loadResumeTemplate();
|
|
268
|
+
const dictionary = await getDictionary(options.locale);
|
|
269
|
+
const timestamp = options.timestamp ?? new Date().toLocaleTimeString(options.locale);
|
|
270
|
+
return replaceAll(template, {
|
|
271
|
+
[TEMPLATE_VARS.RESUME_ID]: escapeHtml(options.resumeId),
|
|
272
|
+
[TEMPLATE_VARS.PORT]: options.port.toString(),
|
|
273
|
+
[TEMPLATE_VARS.LIVE_ENABLED]: options.liveEnabled ? "true" : "false",
|
|
274
|
+
[TEMPLATE_VARS.CONTENT]: `<div class="resume-viewport"><div class="resume-sheet" lang="${options.resumeLocale}">${bodyHtml}</div></div>`,
|
|
275
|
+
[TEMPLATE_VARS.PAGE_TITLE]: escapeHtml(options.resumeId),
|
|
276
|
+
[TEMPLATE_VARS.PAGE_HEAD]: `<link rel="icon" href="/favicon.svg" type="image/svg+xml" />
|
|
277
|
+
<link rel="stylesheet" href="/shared.css" />
|
|
278
|
+
<link rel="stylesheet" href="/resume.css" />`,
|
|
279
|
+
[TEMPLATE_VARS.BODY_ATTRIBUTES]: "",
|
|
280
|
+
[TEMPLATE_VARS.PAGE_SHELL]: renderPreviewShell(timestamp, dictionary, {
|
|
281
|
+
liveEnabled: options.liveEnabled,
|
|
282
|
+
locale: options.locale,
|
|
283
|
+
path: options.path ?? RESUME_ROUTES.VIEW + options.resumeId,
|
|
284
|
+
}),
|
|
285
|
+
[TEMPLATE_VARS.PAGE_SCRIPTS]: `<script src="${ROUTES.I18N_SCRIPT}"></script>
|
|
286
|
+
<script src="${RESUME_ROUTES.SCRIPT}"></script>`,
|
|
287
|
+
[TEMPLATE_VARS.LANG]: getHtmlLang(dictionary, options.locale),
|
|
288
|
+
[TEMPLATE_VARS.LANG_SWITCH]: "",
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Builds the standalone HTML document that is written to
|
|
293
|
+
* `resume.export.html`. It carries its own inlined styles so it can be
|
|
294
|
+
* opened directly in a browser.
|
|
295
|
+
*
|
|
296
|
+
* The document is entirely in the resume's own language — it has no chrome
|
|
297
|
+
* to translate and no request to read a language from.
|
|
298
|
+
*/
|
|
299
|
+
export async function renderResumeExportHtml(content, locale) {
|
|
300
|
+
const template = await loadResumeTemplate();
|
|
301
|
+
const styles = await loadResumeStyles();
|
|
302
|
+
const dictionary = await getDictionary(locale);
|
|
303
|
+
const bodyHtml = await renderResumeHtml(content, locale);
|
|
304
|
+
const title = content.basics?.name || t(dictionary, "resume.docTitle");
|
|
305
|
+
return replaceAll(template, {
|
|
306
|
+
[TEMPLATE_VARS.RESUME_ID]: "",
|
|
307
|
+
[TEMPLATE_VARS.PORT]: "",
|
|
308
|
+
[TEMPLATE_VARS.LIVE_ENABLED]: "false",
|
|
309
|
+
[TEMPLATE_VARS.CONTENT]: bodyHtml,
|
|
310
|
+
[TEMPLATE_VARS.PAGE_TITLE]: escapeHtml(title),
|
|
311
|
+
[TEMPLATE_VARS.PAGE_HEAD]: `<style>
|
|
312
|
+
${styles}
|
|
313
|
+
</style>`,
|
|
314
|
+
[TEMPLATE_VARS.BODY_ATTRIBUTES]: ` class="resume-export"`,
|
|
315
|
+
[TEMPLATE_VARS.PAGE_SHELL]: "",
|
|
316
|
+
[TEMPLATE_VARS.PAGE_SCRIPTS]: "",
|
|
317
|
+
[TEMPLATE_VARS.LANG]: getHtmlLang(dictionary, locale),
|
|
318
|
+
[TEMPLATE_VARS.LANG_SWITCH]: "",
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
// ===== PDF rendering =====
|
|
322
|
+
let browserPromise = null;
|
|
323
|
+
let renderQueue = Promise.resolve();
|
|
324
|
+
/**
|
|
325
|
+
* Launches (or reuses) a single headless Chromium instance. Rendering is
|
|
326
|
+
* serialized through a queue so two concurrent exports cannot fight over the
|
|
327
|
+
* same browser.
|
|
328
|
+
*/
|
|
329
|
+
export async function ensureBrowser() {
|
|
330
|
+
if (!browserPromise) {
|
|
331
|
+
browserPromise = puppeteer
|
|
332
|
+
.launch({
|
|
333
|
+
headless: true,
|
|
334
|
+
args: ["--disable-gpu", "--disable-dev-shm-usage"],
|
|
335
|
+
})
|
|
336
|
+
.catch((error) => {
|
|
337
|
+
browserPromise = null;
|
|
338
|
+
throw error;
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
return browserPromise;
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Closes the headless browser if one is running.
|
|
345
|
+
*/
|
|
346
|
+
export async function closeBrowser() {
|
|
347
|
+
if (!browserPromise)
|
|
348
|
+
return;
|
|
349
|
+
const pending = browserPromise;
|
|
350
|
+
browserPromise = null;
|
|
351
|
+
try {
|
|
352
|
+
const browser = await pending;
|
|
353
|
+
await browser.close();
|
|
354
|
+
}
|
|
355
|
+
catch (error) {
|
|
356
|
+
mcpLogger.debug("Failed to close the resume browser", {
|
|
357
|
+
error: error instanceof Error ? error.message : String(error),
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
function enqueueRender(task) {
|
|
362
|
+
const result = renderQueue.then(task, task);
|
|
363
|
+
renderQueue = result.catch(() => undefined);
|
|
364
|
+
return result;
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Renders the resume to a PDF.
|
|
368
|
+
*
|
|
369
|
+
* Page margins come from the template's `@page` rule; puppeteer's `margin`
|
|
370
|
+
* option would override it, so it is deliberately left unset.
|
|
371
|
+
*
|
|
372
|
+
* The document language is the resume's own locale — the export never
|
|
373
|
+
* depends on the language of the request that triggered it.
|
|
374
|
+
*/
|
|
375
|
+
export async function renderResumePdf(content, locale, options = {}) {
|
|
376
|
+
// Only a document that actually contains CJK characters needs a CJK font.
|
|
377
|
+
// A pure-Latin resume renders fine with the generic sans-serif fallback.
|
|
378
|
+
// The check runs before Chromium is launched, so a font-less machine fails
|
|
379
|
+
// immediately instead of paying the browser startup first.
|
|
380
|
+
await assertPdfFontAvailable(content, locale, options.detectFont);
|
|
381
|
+
const html = await renderResumeExportHtml(content, locale);
|
|
382
|
+
return enqueueRender(async () => {
|
|
383
|
+
const browser = await ensureBrowser();
|
|
384
|
+
const page = await browser.newPage();
|
|
385
|
+
try {
|
|
386
|
+
await page.setContent(html, { waitUntil: "load" });
|
|
387
|
+
const pdf = await page.pdf({
|
|
388
|
+
format: "A4",
|
|
389
|
+
printBackground: true,
|
|
390
|
+
});
|
|
391
|
+
return Buffer.from(pdf);
|
|
392
|
+
}
|
|
393
|
+
finally {
|
|
394
|
+
await page.close();
|
|
395
|
+
}
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
// ===== CJK font detection =====
|
|
399
|
+
const WINDOWS_CJK_FONT_FILES = CJK_FONT_FILES;
|
|
400
|
+
/**
|
|
401
|
+
* Whether a value anywhere in the resume content contains characters that need
|
|
402
|
+
* a CJK-capable font.
|
|
403
|
+
*/
|
|
404
|
+
function containsCjkCharacters(value) {
|
|
405
|
+
if (typeof value === "string")
|
|
406
|
+
return CJK_CHAR_REGEX.test(value);
|
|
407
|
+
if (Array.isArray(value))
|
|
408
|
+
return value.some(containsCjkCharacters);
|
|
409
|
+
if (value && typeof value === "object") {
|
|
410
|
+
return Object.values(value).some(containsCjkCharacters);
|
|
411
|
+
}
|
|
412
|
+
return false;
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Whether this resume needs a CJK font to render.
|
|
416
|
+
*
|
|
417
|
+
* Two independent sources can introduce CJK characters:
|
|
418
|
+
*
|
|
419
|
+
* - the resume's own locale: the zh-CN dictionary supplies the section headings
|
|
420
|
+
* (工作经历), the item separator (、) and the placeholders (你的名字 / 简历),
|
|
421
|
+
* so a zh-CN resume needs one even when its content is written in English.
|
|
422
|
+
* That rule is written out explicitly rather than left to the character scan,
|
|
423
|
+
* because a zh-CN resume with nothing but a Latin name and no sections would
|
|
424
|
+
* otherwise scan as "no CJK needed" while its document title is still Chinese;
|
|
425
|
+
* - the content itself, which may hold Chinese (or Japanese, Korean) text.
|
|
426
|
+
*
|
|
427
|
+
* A pure-Latin resume needs no CJK font: the resume font stack falls back to
|
|
428
|
+
* the generic `sans-serif` family, which is always present.
|
|
429
|
+
*/
|
|
430
|
+
export function resumeNeedsCjkFont(content, locale) {
|
|
431
|
+
if (CJK_ALWAYS_LOCALES.includes(locale))
|
|
432
|
+
return true;
|
|
433
|
+
return containsCjkCharacters(content);
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* Checks whether the machine has a font that can render Chinese.
|
|
437
|
+
*
|
|
438
|
+
* Returns `true`/`false` when the check could be performed and `null` when
|
|
439
|
+
* the platform offers no way to tell — the caller then logs a warning and
|
|
440
|
+
* carries on rather than blocking a Chinese export on a minimal container.
|
|
441
|
+
*/
|
|
442
|
+
export async function detectCjkFont() {
|
|
443
|
+
if (process.platform === "win32") {
|
|
444
|
+
const fontDir = join(process.env.WINDIR || "C:\\Windows", "Fonts");
|
|
445
|
+
for (const file of WINDOWS_CJK_FONT_FILES) {
|
|
446
|
+
if (existsSync(join(fontDir, file)))
|
|
447
|
+
return true;
|
|
448
|
+
}
|
|
449
|
+
return false;
|
|
450
|
+
}
|
|
451
|
+
try {
|
|
452
|
+
const { stdout } = await execFileAsync("fc-list", [":lang=zh", "family"]);
|
|
453
|
+
return stdout.trim().length > 0;
|
|
454
|
+
}
|
|
455
|
+
catch {
|
|
456
|
+
return null;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Fails loudly when the machine cannot render Chinese, instead of silently
|
|
461
|
+
* producing a PDF full of tofu boxes.
|
|
462
|
+
*
|
|
463
|
+
* Only called for documents that actually need a CJK font — see
|
|
464
|
+
* `resumeNeedsCjkFont`.
|
|
465
|
+
*/
|
|
466
|
+
export async function assertCjkFontAvailable(detect = detectCjkFont) {
|
|
467
|
+
const available = await detect();
|
|
468
|
+
if (available === true)
|
|
469
|
+
return;
|
|
470
|
+
if (available === null) {
|
|
471
|
+
mcpLogger.warn("Could not determine whether a CJK font is installed; PDF export may render Chinese incorrectly");
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
throw new Error("No CJK font found for PDF export. Install a Chinese font (e.g. Microsoft YaHei, Noto Sans CJK SC) and try again.");
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* The font gate for PDF export: fails when the document needs a CJK font and
|
|
478
|
+
* the machine has none. A document without CJK characters never consults the
|
|
479
|
+
* detector at all.
|
|
480
|
+
*/
|
|
481
|
+
export async function assertPdfFontAvailable(content, locale, detect = detectCjkFont) {
|
|
482
|
+
if (!resumeNeedsCjkFont(content, locale))
|
|
483
|
+
return;
|
|
484
|
+
await assertCjkFontAvailable(detect);
|
|
485
|
+
}
|
|
486
|
+
//# sourceMappingURL=resume-renderer.js.map
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resume Service
|
|
3
|
+
* Single Responsibility: Business logic for resume data access and management
|
|
4
|
+
*/
|
|
5
|
+
import { readdir, stat } from "fs/promises";
|
|
6
|
+
import { getResumesDir, getResumeFilePath, validatePreviewId, loadResumeOptions, deleteResumeDirectory, } from "./file-utils.js";
|
|
7
|
+
import { webLogger } from "./logger.js";
|
|
8
|
+
/**
|
|
9
|
+
* Artifacts that can stand in for "the rendered resume", in the order they
|
|
10
|
+
* are probed. The format is deliberately not exposed in the API response —
|
|
11
|
+
* probing for it turned into a known modelling gap.
|
|
12
|
+
*/
|
|
13
|
+
const ARTIFACT_KINDS = ["html", "exportHtml", "pdf", "content"];
|
|
14
|
+
/**
|
|
15
|
+
* Lists all resumes stored under the resumes directory.
|
|
16
|
+
* @returns Array of resume info sorted by modification time (newest first)
|
|
17
|
+
*/
|
|
18
|
+
export async function listResumes() {
|
|
19
|
+
try {
|
|
20
|
+
const resumesDir = getResumesDir();
|
|
21
|
+
const entries = await readdir(resumesDir, { withFileTypes: true });
|
|
22
|
+
const resumes = [];
|
|
23
|
+
for (const entry of entries) {
|
|
24
|
+
if (!entry.isDirectory()) {
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
try {
|
|
28
|
+
// The directory name is the slug, which always satisfies the id rules
|
|
29
|
+
validatePreviewId(entry.name);
|
|
30
|
+
const info = await getResumeInfo(entry.name);
|
|
31
|
+
if (info) {
|
|
32
|
+
resumes.push(info);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
// Skip invalid or inaccessible resumes
|
|
37
|
+
webLogger.debug(`Skipping resume directory: ${entry.name}`, {
|
|
38
|
+
error: error instanceof Error ? error.message : String(error),
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
// Sort by modification time (newest first)
|
|
43
|
+
resumes.sort((a, b) => b.modifiedAt.getTime() - a.modifiedAt.getTime());
|
|
44
|
+
webLogger.debug(`Listed ${resumes.length} resumes`);
|
|
45
|
+
return resumes;
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
webLogger.error("Failed to list resumes", {
|
|
49
|
+
error: error instanceof Error ? error.message : String(error),
|
|
50
|
+
});
|
|
51
|
+
return [];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Gets detailed information about a specific resume.
|
|
56
|
+
* @param resumeId The resume id or slug
|
|
57
|
+
* @returns Resume info or null if not found
|
|
58
|
+
*/
|
|
59
|
+
export async function getResumeInfo(resumeId) {
|
|
60
|
+
try {
|
|
61
|
+
validatePreviewId(resumeId);
|
|
62
|
+
let modifiedAt = null;
|
|
63
|
+
let sizeBytes = 0;
|
|
64
|
+
for (const kind of ARTIFACT_KINDS) {
|
|
65
|
+
try {
|
|
66
|
+
const filePath = getResumeFilePath(resumeId, kind);
|
|
67
|
+
const stats = await stat(filePath);
|
|
68
|
+
modifiedAt = stats.mtime;
|
|
69
|
+
sizeBytes = stats.size;
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (!modifiedAt) {
|
|
77
|
+
webLogger.debug(`Resume not found: ${resumeId}`);
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
const options = await loadResumeOptions(resumeId);
|
|
81
|
+
return {
|
|
82
|
+
id: resumeId,
|
|
83
|
+
template: options.template,
|
|
84
|
+
theme: options.theme,
|
|
85
|
+
modifiedAt,
|
|
86
|
+
sizeBytes,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
webLogger.error(`Error getting resume info: ${resumeId}`, {
|
|
91
|
+
error: error instanceof Error ? error.message : String(error),
|
|
92
|
+
});
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Deletes a resume and all its associated files.
|
|
98
|
+
* @param resumeId The resume id or slug
|
|
99
|
+
*/
|
|
100
|
+
export async function deleteResume(resumeId) {
|
|
101
|
+
try {
|
|
102
|
+
validatePreviewId(resumeId);
|
|
103
|
+
const info = await getResumeInfo(resumeId);
|
|
104
|
+
if (!info) {
|
|
105
|
+
throw new Error(`Resume not found: ${resumeId}`);
|
|
106
|
+
}
|
|
107
|
+
await deleteResumeDirectory(resumeId);
|
|
108
|
+
webLogger.info(`Deleted resume: ${resumeId}`);
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
webLogger.error(`Error deleting resume: ${resumeId}`, {
|
|
112
|
+
error: error instanceof Error ? error.message : String(error),
|
|
113
|
+
});
|
|
114
|
+
throw error;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=resume-service.js.map
|