-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
202 lines (168 loc) · 8.66 KB
/
script.js
File metadata and controls
202 lines (168 loc) · 8.66 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
document.addEventListener('DOMContentLoaded', () => {
const resumeContent = document.getElementById('resume-content');
const downloadPdfBtn = document.getElementById('downloadPdfBtn');
// Dynamically resolve the newest markdown file
async function resolveLatestResumeFile() {
const months = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];
function getFileScore(filename) {
// Support both _7th.April.2026.md and _Feb2026.md string styles
const match1 = filename.match(/_([0-9a-zA-Z]+)\.([a-zA-Z]+)\.(\d{4})\.md$/i);
const match2 = filename.match(/_([a-zA-Z]+)(\d{4})\.md$/i);
let dateStr, monthStr, yearStr;
if (match1) { dateStr = match1[1]; monthStr = match1[2]; yearStr = match1[3]; }
else if (match2) { monthStr = match2[1]; yearStr = match2[2]; }
else return 0;
const monthIdx = months.findIndex(m => monthStr.toLowerCase().startsWith(m));
const year = parseInt(yearStr);
let day = 0;
if (dateStr) {
const dayMatch = dateStr.match(/(\d+)/);
if (dayMatch) day = parseInt(dayMatch[1]);
}
// year * 10000 + month * 100 + day guarantees perfect hierarchical date ranking
return year * 10000 + (monthIdx >= 0 ? monthIdx : 0) * 100 + day;
}
let fileCandidates = [];
// 1. Try local server directory scrape explicitly looking inside resume_md/
try {
const localRes = await fetch('./resume_md/');
const localHtml = await localRes.text();
const matches = [...localHtml.matchAll(/href="(.*?\.md)"/gi)].map(m => `resume_md/${m[1]}`);
fileCandidates.push(...matches);
} catch (e) { }
// Also check root as safe fallback
try {
const localResRoot = await fetch('./');
const localHtmlRoot = await localResRoot.text();
const matchesRoot = [...localHtmlRoot.matchAll(/href="(.*?\.md)"/gi)].map(m => m[1]);
fileCandidates.push(...matchesRoot);
} catch (e) { }
// Extract valid resumes
fileCandidates = fileCandidates.filter(f => f.toLowerCase().includes('resume') && f.endsWith('.md'));
// 2. Absolute fallback (used on GitHub Pages where directory listing is unavailable)
if (fileCandidates.length === 0) {
return 'resume_md/YourName_Resume_7th.April.2026.md';
}
// Descending sort by chronological date using regex score extraction
fileCandidates.sort((a, b) => getFileScore(b) - getFileScore(a));
return fileCandidates[0];
}
// Resolve, extract UI info, and orchestrate rendering
resolveLatestResumeFile().then(resumeFile => {
// Extract timestamp from file name for display
const match1 = resumeFile.match(/_([0-9a-zA-Z]+)\.([a-zA-Z]+)\.(\d{4})\.md$/i);
const match2 = resumeFile.match(/_([a-zA-Z]+)(\d{4})\.md$/i);
let dateStr, monthStr, yearStr;
if (match1) { dateStr = match1[1]; monthStr = match1[2]; yearStr = match1[3]; }
else if (match2) { monthStr = match2[1]; yearStr = match2[2]; }
if (monthStr && yearStr) {
const updatedLabel = document.getElementById('updated-label');
if (updatedLabel) {
const cleanMonth = monthStr.charAt(0).toUpperCase() + monthStr.slice(1).toLowerCase();
if (dateStr) {
updatedLabel.textContent = `Updated: ${dateStr} ${cleanMonth} ${yearStr}`;
} else {
updatedLabel.textContent = `Updated: ${cleanMonth} ${yearStr}`;
}
}
}
// Fetch and render the dynamically matched markdown
fetch(resumeFile)
.then(response => {
if (!response.ok) {
throw new Error(`Failed to load ${resumeFile} (Status: ${response.status})`);
}
return response.text();
})
.then(markdown => {
// Preserve explicit empty newlines as visual space
// In markdown \n\n is a standard block break. Any extra \n will become a <br>
let processedMarkdown = markdown.replace(/\n\n\n+/g, match => {
return '\n\n' + '<br>'.repeat(match.length - 2) + '\n\n';
});
// Handle custom $text$ syntax for 9pt font and 8 whitespace indent (2 tabs)
processedMarkdown = processedMarkdown.replace(/\$([^$]+)\$/g, '<span class="small-text"> $1</span>');
// Parse Markdown to HTML
const html = marked.parse(processedMarkdown);
// Create a temporary container to extract elements
const tempDiv = document.createElement('div');
tempDiv.innerHTML = html;
const elements = Array.from(tempDiv.children);
// Robust grouping: gather all introductory paragraphs before the first heading into a dedicated header wrapper
const headerWrapper = document.createElement('div');
headerWrapper.className = 'resume-header';
while(elements.length > 0 && !elements[0].tagName.match(/^H[1-6]$/i)) {
headerWrapper.appendChild(elements.shift());
}
// Put the consolidated header back at the top of the processing queue
if (headerWrapper.children.length > 0) {
elements.unshift(headerWrapper);
}
// Setup wrapper for pages
resumeContent.innerHTML = '';
resumeContent.className = 'resume-wrapper';
resumeContent.style.opacity = 0;
const maxPageHeight = 979; // 11 inches (1056px) minus exactly 0.8 inches of total vertical padding (76.8px rounded mathematically)
function createNewPage() {
const page = document.createElement('div');
page.className = 'page markdown-body';
const inner = document.createElement('div');
inner.className = 'page-inner';
page.appendChild(inner);
return { page, inner };
}
let current = createNewPage();
resumeContent.appendChild(current.page);
elements.forEach(el => {
current.inner.appendChild(el);
// Measure the unconstrained inner div height, not the fixed 11in parent
if (current.inner.offsetHeight > maxPageHeight) {
// It overflowed, create a new page and move this element
current = createNewPage();
resumeContent.appendChild(current.page);
current.inner.appendChild(el);
}
});
// Smoothly animate in
let opacity = 0;
const fadeIn = setInterval(() => {
if (opacity >= 1) clearInterval(fadeIn);
resumeContent.style.opacity = opacity;
opacity += 0.05;
}, 10);
// Post-process HTML links
processLinks();
})
.catch(error => {
resumeContent.innerHTML = `
<div style="color: #e11d48; padding: 20px; background: #ffe4e6; border-radius: 8px; font-weight: 500;">
Error loading resume markdown: ${error.message}<br><br>
Make sure you're running this via a web server (e.g., using GitHub Pages or a local server) and not double-clicking the file directly.
</div>`;
console.error('Error fetching resume:', error);
});
});
function processLinks() {
const links = resumeContent.querySelectorAll('a');
links.forEach(link => {
if (link.textContent.includes('@') && !link.href) {
link.href = `mailto:${link.textContent}`;
}
// Force link to open in new tab securely
link.setAttribute('target', '_blank');
link.setAttribute('rel', 'noopener noreferrer');
});
}
// Handle print/download PDF
downloadPdfBtn.addEventListener('click', () => {
window.print();
});
// Handle font toggle group
document.querySelectorAll('.font-opt').forEach(btn => {
btn.addEventListener('click', () => {
document.querySelectorAll('.font-opt').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
document.body.classList.toggle('sans-serif-font', btn.dataset.font === 'sans');
});
});
});