- Add 4 new CV templates (ATS-Friendly, Classic, Modern, Minimal) with thumbnails - Implement template registry and gallery component for template selection - Add photoUrl field to personal info with URL validation - Update buildPrintableHtml to support template-specific styling - Modify ExportControls to use template registry - Add template preview thumbnails in SVG format
196 lines
7.7 KiB
JavaScript
196 lines
7.7 KiB
JavaScript
// ESM module: shared printable HTML builder
|
|
// Note: callers must sanitize any HTML fields (e.g., summary) before passing.
|
|
|
|
function escapeText(str) {
|
|
if (str == null) return '';
|
|
return String(str)
|
|
.replace(/&/g, '&')
|
|
.replace(/</g, '<')
|
|
.replace(/>/g, '>')
|
|
.replace(/\"/g, '"')
|
|
.replace(/'/g, ''');
|
|
}
|
|
|
|
export function buildPrintableHtml(cv, templateId) {
|
|
const { personal = {}, summary = '', work = [], education = [], skills = [], languages = [], certifications = [], templateId: cvTemplateId } = cv || {};
|
|
const tid = templateId || cvTemplateId || 'ats';
|
|
|
|
const baseStyles = `
|
|
@page { size: A4; margin: 20mm; }
|
|
body { font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, Noto Sans; color: #1f2937; }
|
|
.page { width: 210mm; min-height: 297mm; margin: 0 auto; background: white; }
|
|
h1 { font-size: 24px; margin: 0 0 6px 0; }
|
|
h2 { font-size: 16px; margin: 16px 0 8px 0; padding-bottom: 4px; border-bottom: 1px solid #e5e7eb; color: #374151; }
|
|
h3 { font-size: 14px; margin: 0; }
|
|
.header { border-bottom: 1px solid #e5e7eb; padding-bottom: 12px; margin-bottom: 16px; }
|
|
.meta { display: flex; flex-wrap: wrap; gap: 8px; font-size: 12px; color: #6b7280; }
|
|
.section { margin-bottom: 16px; }
|
|
.job, .edu { margin-bottom: 12px; }
|
|
.row { display: flex; justify-content: space-between; align-items: flex-start; }
|
|
.small { font-size: 12px; color: #6b7280; }
|
|
ul { padding-left: 20px; }
|
|
li { margin: 4px 0; }
|
|
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
|
|
.chip { background: #f3f4f6; color: #1f2937; padding: 2px 8px; border-radius: 12px; font-size: 12px; }
|
|
`;
|
|
|
|
const classicStyles = `
|
|
@page { size: A4; margin: 22mm; }
|
|
body { font-family: Georgia, Cambria, 'Times New Roman', Times, serif; color: #111827; }
|
|
h1 { font-size: 26px; }
|
|
h2 { font-size: 16px; margin: 18px 0 6px 0; padding-bottom: 0; border-bottom: none; color: #374151; letter-spacing: 0.2px; }
|
|
.header { border-bottom: none; padding-bottom: 0; margin-bottom: 10px; }
|
|
.chip { background: #f9fafb; color: #111827; }
|
|
`;
|
|
|
|
const modernStyles = `
|
|
@page { size: A4; margin: 18mm; }
|
|
body { font-family: Inter, ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, Noto Sans; color: #0f172a; }
|
|
.accent { height: 6px; background: #0ea5e9; margin-bottom: 12px; }
|
|
h1 { font-weight: 700; letter-spacing: 0.3px; }
|
|
h2 { color: #0ea5e9; border-color: #bae6fd; }
|
|
.header { border-left: 6px solid #0ea5e9; padding-left: 12px; }
|
|
.chip { background: #e0f2fe; color: #0c4a6e; }
|
|
`;
|
|
|
|
const minimalStyles = `
|
|
@page { size: A4; margin: 25mm; }
|
|
body { font-family: 'Source Serif Pro', Georgia, Cambria, 'Times New Roman', Times, serif; color: #1f2937; }
|
|
h1 { font-size: 24px; font-weight: 600; }
|
|
h2 { font-size: 15px; margin: 22px 0 6px 0; border-bottom: none; color: #374151; }
|
|
.header { border-bottom: none; margin-bottom: 8px; }
|
|
.section { margin-bottom: 22px; }
|
|
.chip { background: #f3f4f6; color: #374151; }
|
|
`;
|
|
|
|
const stylesVariant = tid === 'classic' ? classicStyles : tid === 'modern' ? modernStyles : tid === 'minimal' ? minimalStyles : '';
|
|
const styles = `
|
|
<style>
|
|
${baseStyles}
|
|
${stylesVariant}
|
|
</style>
|
|
`;
|
|
|
|
const headerLinks = (personal.links || [])
|
|
.map(l => `<a href="${escapeText(l.url)}" target="_blank" rel="noopener noreferrer">${escapeText(l.label || l.url)}</a>`)
|
|
.join(' · ');
|
|
|
|
const safePhotoUrl = typeof personal.photoUrl === 'string' && /^(https?:\/\/)/i.test(personal.photoUrl) ? personal.photoUrl : '';
|
|
const headerPhoto = tid === 'modern' && safePhotoUrl ? `<img src="${escapeText(safePhotoUrl)}" alt="${escapeText((personal.firstName || '') + ' ' + (personal.lastName || ''))} photo" style="width:64px;height:64px;border-radius:50%;object-fit:cover;border:2px solid rgba(255,255,255,0.7);margin-right:12px;" referrerpolicy="no-referrer" />` : '';
|
|
const headerHtml = `
|
|
<div class="header" style="display:flex;align-items:center;gap:12px;">
|
|
${headerPhoto}
|
|
<div>
|
|
<h1>${escapeText(personal.firstName || '')} ${escapeText(personal.lastName || '')}</h1>
|
|
<div class="meta">
|
|
${personal.email ? `<span>${escapeText(personal.email)}</span>` : ''}
|
|
${personal.phone ? `<span>${escapeText(personal.phone)}</span>` : ''}
|
|
${(personal.city || personal.country) ? `<span>${personal.city ? escapeText(personal.city) : ''}${personal.city && personal.country ? ', ' : ''}${personal.country ? escapeText(personal.country) : ''}</span>` : ''}
|
|
${headerLinks ? `<span>${headerLinks}</span>` : ''}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
`;
|
|
|
|
const summarySection = summary ? `
|
|
<div class="section">
|
|
<h2>Professional Summary</h2>
|
|
<div>${summary}</div>
|
|
</div>
|
|
` : '';
|
|
|
|
const workSection = (work && work.length) ? `
|
|
<div class="section">
|
|
<h2>Work Experience</h2>
|
|
${work.map(job => `
|
|
<div class="job">
|
|
<div class="row">
|
|
<div>
|
|
<h3>${escapeText(job.title)}</h3>
|
|
<div class="small">${escapeText(job.company)}</div>
|
|
</div>
|
|
<div class="small">
|
|
${escapeText(job.startDate || '')} - ${escapeText(job.endDate || 'Present')}
|
|
${job.location ? `<div>${escapeText(job.location)}</div>` : ''}
|
|
</div>
|
|
</div>
|
|
${(job.bullets && job.bullets.length) ? `
|
|
<ul>
|
|
${job.bullets.map(b => `<li>${escapeText(b)}</li>`).join('')}
|
|
</ul>
|
|
` : ''}
|
|
</div>
|
|
`).join('')}
|
|
</div>
|
|
` : '';
|
|
|
|
const eduSection = (education && education.length) ? `
|
|
<div class="section">
|
|
<h2>Education</h2>
|
|
${education.map(ed => `
|
|
<div class="edu">
|
|
<div class="row">
|
|
<div>
|
|
<h3>${escapeText(ed.degree)}</h3>
|
|
<div class="small">${escapeText(ed.school)}</div>
|
|
</div>
|
|
<div class="small">
|
|
${escapeText(ed.startDate || '')} - ${escapeText(ed.endDate || '')}
|
|
</div>
|
|
</div>
|
|
${ed.gpa ? `<div class="small">GPA: ${escapeText(ed.gpa)}</div>` : ''}
|
|
</div>
|
|
`).join('')}
|
|
</div>
|
|
` : '';
|
|
|
|
const skillsSection = (skills && skills.length) ? `
|
|
<div class="section">
|
|
<h2>Skills</h2>
|
|
<div class="chips">
|
|
${skills.map(s => `<span class="chip">${escapeText(s.name)}${s.level ? ` — ${escapeText(s.level)}` : ''}</span>`).join('')}
|
|
</div>
|
|
</div>
|
|
` : '';
|
|
|
|
const languagesSection = (languages && languages.length) ? `
|
|
<div class="section">
|
|
<h2>Languages</h2>
|
|
<div class="chips">
|
|
${languages.map(l => `<span class="chip">${escapeText(l.name)}${l.level ? ` — ${escapeText(l.level)}` : ''}</span>`).join('')}
|
|
</div>
|
|
</div>
|
|
` : '';
|
|
|
|
const certsSection = (certifications && certifications.length) ? `
|
|
<div class="section">
|
|
<h2>Certifications</h2>
|
|
<ul>
|
|
${certifications.map(c => `<li>${escapeText(c.name)}${c.issuer ? ` - ${escapeText(c.issuer)}` : ''}${c.date ? ` (${escapeText(c.date)})` : ''}</li>`).join('')}
|
|
</ul>
|
|
</div>
|
|
` : '';
|
|
|
|
const accentTop = tid === 'modern' ? '<div class="accent"></div>' : '';
|
|
return `<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<title>Printable CV</title>
|
|
${styles}
|
|
</head>
|
|
<body>
|
|
<div class="page">
|
|
${accentTop}
|
|
${headerHtml}
|
|
${summarySection}
|
|
${workSection}
|
|
${eduSection}
|
|
${skillsSection}
|
|
${languagesSection}
|
|
${certsSection}
|
|
</div>
|
|
</body>
|
|
</html>`;
|
|
} |