Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
367 lines (317 loc) · 11.1 KB
/
Copy pathscript.js
File metadata and controls
367 lines (317 loc) · 11.1 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/* ════════════════════════════════════════════════
DEBLOCK STUDIOS — blog.js
Markdown parser + article loader
════════════════════════════════════════════════ */
/* ── Minimal Markdown → HTML parser ── */
const md = (() => {
function escape(s) {
return s.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
}
function inline(s) {
return s
.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, '<img src="$2" alt="$1">')
.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>')
.replace(/`([^`]+)`/g, '<code>$1</code>')
.replace(/\*\*\*(.+?)\*\*\*/g, '<strong><em>$1</em></strong>')
.replace(/\*\*(.+?)\*\*/g, '<strong>$1</strong>')
.replace(/__(.+?)__/g, '<strong>$1</strong>')
.replace(/\*(.+?)\*/g, '<em>$1</em>')
.replace(/_(.+?)_/g, '<em>$1</em>')
.replace(/~~(.+?)~~/g, '<del>$1</del>');
}
function parse(raw) {
const lines = raw.split('\n');
const out = [];
let i = 0;
while (i < lines.length) {
const line = lines[i];
// Blank line
if (!line.trim()) { i++; continue; }
// HR
if (/^-{3,}$|^\*{3,}$|^_{3,}$/.test(line.trim())) {
out.push('<hr>'); i++; continue;
}
// Headings
const hm = line.match(/^(#{1,6})\s+(.+)/);
if (hm) {
const lvl = hm[1].length;
out.push(`<h${lvl}>${inline(hm[2])}</h${lvl}>`);
i++; continue;
}
// Blockquote
if (/^>\s?/.test(line)) {
const bqLines = [];
while (i < lines.length && /^>\s?/.test(lines[i])) {
bqLines.push(lines[i].replace(/^>\s?/, ''));
i++;
}
out.push(`<blockquote>${parse(bqLines.join('\n'))}</blockquote>`);
continue;
}
// Code block
if (/^```/.test(line)) {
const lang = line.slice(3).trim();
i++;
const codeLines = [];
while (i < lines.length && !/^```/.test(lines[i])) {
codeLines.push(escape(lines[i]));
i++;
}
i++;
out.push(`<pre><code class="lang-${lang}">${codeLines.join('\n')}</code></pre>`);
continue;
}
// Unordered list
if (/^[\*\-\+]\s/.test(line)) {
const items = [];
while (i < lines.length && /^[\*\-\+]\s/.test(lines[i])) {
items.push(`<li>${inline(lines[i].replace(/^[\*\-\+]\s/, ''))}</li>`);
i++;
}
out.push(`<ul>${items.join('')}</ul>`);
continue;
}
// Ordered list
if (/^\d+\.\s/.test(line)) {
const items = [];
while (i < lines.length && /^\d+\.\s/.test(lines[i])) {
items.push(`<li>${inline(lines[i].replace(/^\d+\.\s/, ''))}</li>`);
i++;
}
out.push(`<ol>${items.join('')}</ol>`);
continue;
}
// Paragraph
const paraLines = [];
while (i < lines.length && lines[i].trim() &&
!/^#{1,6}\s/.test(lines[i]) &&
!/^[\*\-\+]\s/.test(lines[i]) &&
!/^\d+\.\s/.test(lines[i]) &&
!/^```/.test(lines[i]) &&
!/^>\s?/.test(lines[i]) &&
!/^-{3,}$|^\*{3,}$|^_{3,}$/.test(lines[i].trim())) {
paraLines.push(lines[i]);
i++;
}
if (paraLines.length) {
out.push(`<p>${inline(paraLines.join(' '))}</p>`);
}
}
return out.join('\n');
}
return { parse };
})();
/* ── Date formatter ── */
function formatDate(dateStr) {
const [y, m, d] = dateStr.split('-').map(Number);
const date = new Date(y, m - 1, d);
return date.toLocaleDateString('fr-FR', { day: 'numeric', month: 'long', year: 'numeric' });
}
/* ── Parse markdown file content ── */
function parseArticleFile(filename, content) {
const lines = content.split('\n');
// First non-empty line must be the date (YYYY-MM-DD)
let dateStr = '';
let bodyStart = 0;
for (let i = 0; i < lines.length; i++) {
const trimmed = lines[i].trim();
if (!trimmed) continue;
if (/^\d{4}-\d{2}-\d{2}$/.test(trimmed)) {
dateStr = trimmed;
bodyStart = i + 1;
}
break;
}
// Title = filename without extension, dashes → spaces, capitalize
const slug = filename.replace(/\.md$/, '');
const title = slug
.split('-')
.map(w => w.charAt(0).toUpperCase() + w.slice(1))
.join(' ');
// Body = everything after the date line
const bodyRaw = lines.slice(bodyStart).join('\n').trim();
// Excerpt = first 20 words of plain text
const plainText = bodyRaw
.replace(/#{1,6}\s+/g, '')
.replace(/[*_`[\]()]/g, '')
.replace(/\s+/g, ' ')
.trim();
const words = plainText.split(' ');
const excerpt = words.slice(0, 20).join(' ') + (words.length > 20 ? '…' : '');
// Date sort value
const sortDate = dateStr ? new Date(dateStr).getTime() : 0;
return { slug, title, dateStr, dateFormatted: dateStr ? formatDate(dateStr) : '', bodyRaw, bodyHtml: md.parse(bodyRaw), excerpt, sortDate };
}
/* ── Blog State ── */
const Blog = {
articles: [],
currentSlug: null,
async init() {
// Load article index
await this.loadArticles();
this.renderList();
this.bindNav();
this.initScrollReveal();
this.initNavbar();
// Handle hash navigation
const hash = window.location.hash.slice(1);
if (hash && hash !== 'blog') {
const art = this.articles.find(a => a.slug === hash);
if (art) this.openArticle(art.slug, false);
}
window.addEventListener('popstate', () => {
const h = window.location.hash.slice(1);
if (!h || h === 'blog') {
this.showList();
} else {
const art = this.articles.find(a => a.slug === h);
if (art) this.openArticle(art.slug, false);
}
});
},
async loadArticles() {
// Try to fetch the manifest first, then fall back to individual loading
// Admin drops .md files in /posts/ — we fetch a manifest or scan known files
let files = [];
try {
const r = await fetch('posts/index.json');
if (r.ok) {
files = await r.json();
}
} catch(e) {}
if (!files.length) {
// Fallback: try to fetch a directory listing or use known files
// In a real server with directory listing enabled:
try {
const r = await fetch('posts/');
if (r.ok) {
const html = await r.text();
const matches = [...html.matchAll(/href="([^"]+\.md)"/g)];
files = matches.map(m => m[1]);
}
} catch(e) {}
}
// Load each file
const results = await Promise.allSettled(
files.map(async (fname) => {
const name = fname.split('/').pop();
const r = await fetch(`posts/${name}`);
if (!r.ok) throw new Error(`Failed to load ${name}`);
const content = await r.text();
return parseArticleFile(name, content);
})
);
this.articles = results
.filter(r => r.status === 'fulfilled')
.map(r => r.value)
.sort((a, b) => b.sortDate - a.sortDate);
},
renderList() {
const container = document.getElementById('articles-container');
const count = document.getElementById('articles-count');
if (!container) return;
count.textContent = `${this.articles.length} article${this.articles.length !== 1 ? 's' : ''}`;
if (!this.articles.length) {
container.innerHTML = `
<div class="blog-empty">
<h3>Aucun article pour l'instant</h3>
<p>Les articles apparaîtront ici dès qu'un fichier .md sera ajouté dans le dossier <code>posts/</code>.</p>
</div>`;
return;
}
container.innerHTML = this.articles.map((art, i) => `
<a class="article-card reveal${i > 0 ? ` reveal-delay-${Math.min(i, 3)}` : ''}"
data-slug="${art.slug}"
href="#${art.slug}">
<div class="article-meta">
<span class="article-date">${art.dateFormatted}</span>
</div>
<h2 class="article-title">${art.title}</h2>
<p class="article-excerpt">${art.excerpt}</p>
<span class="article-read-more">
Lire l'article
<svg width="14" height="14" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2.5" aria-hidden="true">
<path d="M9 18l6-6-6-6"/>
</svg>
</span>
</a>
`).join('');
// Bind clicks
container.querySelectorAll('.article-card').forEach(el => {
el.addEventListener('click', (e) => {
e.preventDefault();
this.openArticle(el.dataset.slug);
});
});
setTimeout(() => this.initScrollReveal(), 50);
},
openArticle(slug, pushState = true) {
const art = this.articles.find(a => a.slug === slug);
if (!art) return;
this.currentSlug = slug;
const page = document.getElementById('article-page');
const listView = document.getElementById('blog-list-view');
const titleEl = document.getElementById('article-title');
const dateEl = document.getElementById('article-date');
const bodyEl = document.getElementById('article-body');
titleEl.textContent = art.title;
dateEl.textContent = art.dateFormatted;
bodyEl.innerHTML = art.bodyHtml;
listView.classList.add('hidden');
page.classList.add('visible');
if (pushState) {
window.history.pushState({ slug }, '', `#${slug}`);
}
window.scrollTo({ top: 0, behavior: 'instant' });
document.title = `${art.title} — Deblock Studios Blog`;
},
showList() {
const page = document.getElementById('article-page');
const listView = document.getElementById('blog-list-view');
page.classList.remove('visible');
listView.classList.remove('hidden');
this.currentSlug = null;
document.title = 'Blog — Deblock Studios';
window.scrollTo({ top: 0, behavior: 'instant' });
},
bindNav() {
const backBtn = document.getElementById('back-btn');
if (backBtn) {
backBtn.addEventListener('click', () => {
window.history.pushState({}, '', '#blog');
this.showList();
});
}
// Hamburger
const hamburger = document.getElementById('hamburger-btn');
const drawer = document.getElementById('nav-drawer');
if (hamburger && drawer) {
hamburger.addEventListener('click', () => {
const open = drawer.classList.toggle('open');
hamburger.setAttribute('aria-expanded', open);
});
}
},
initNavbar() {
const navbar = document.getElementById('navbar');
const onScroll = () => {
navbar.classList.toggle('scrolled', window.scrollY > 20);
};
window.addEventListener('scroll', onScroll, { passive: true });
onScroll();
},
initScrollReveal() {
const els = document.querySelectorAll('.reveal:not(.visible)');
if (!els.length) return;
const obs = new IntersectionObserver((entries) => {
entries.forEach(e => {
if (e.isIntersecting) {
e.target.classList.add('visible');
obs.unobserve(e.target);
}
});
}, { threshold: 0.1 });
els.forEach(el => obs.observe(el));
}
};
document.addEventListener('DOMContentLoaded', () => Blog.init());