feat: change css + watcher

Signed-off-by: Cédric OLIVIER <olivier.cedric@gmail.com>
This commit is contained in:
Cédric OLIVIER
2026-06-19 12:08:45 +02:00
parent 1265beefb8
commit 85f62bf842
5 changed files with 437 additions and 207 deletions
+65 -8
View File
@@ -42,16 +42,33 @@ function notifyClients(clients) {
return clients.filter((c) => !dead.includes(c) && !c.writableEnded);
}
function startServer({ inputFile, inputDir, port }) {
function startServer({ inputFile, inputDir, port, onReady }) {
let clients = [];
const isDir = !!inputDir;
const baseDir = isDir ? path.resolve(inputDir) : path.dirname(path.resolve(inputFile));
// --- cache ---
// pageCache: filePath → rendered HTML string
// fileListCache: cached list of all .md files (null = stale)
const pageCache = new Map();
let fileListCache = null;
function getCachedFiles() {
if (!fileListCache) fileListCache = findMarkdownFiles(baseDir);
return fileListCache;
}
// --- rebuild ---
function rebuild(changedFile) {
if (changedFile) {
const rel = path.relative(baseDir, changedFile);
console.log(`${rel} — rechargement...`);
// Only this page's HTML is stale; other pages' sidebars are still valid
pageCache.delete(changedFile);
} else {
// File added or removed: file list changed → all caches stale
pageCache.clear();
fileListCache = null;
}
clients = notifyClients(clients);
}
@@ -67,7 +84,9 @@ function startServer({ inputFile, inputDir, port }) {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
Connection: 'keep-alive',
'X-Accel-Buffering': 'no',
});
if (req.socket) req.socket.setNoDelay(true);
res.write(': keep-alive\n\n');
clients.push(res);
@@ -86,8 +105,11 @@ function startServer({ inputFile, inputDir, port }) {
// ── SINGLE FILE MODE ──────────────────────────────────────────────────────
if (!isDir) {
if (pathname === '/' || pathname === '/index.html') {
if (!pageCache.has(inputFile)) {
pageCache.set(inputFile, convert(inputFile, true));
}
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
res.end(convert(inputFile, true));
res.end(pageCache.get(inputFile));
return;
}
return serveStatic(req, res, pathname, baseDir, port);
@@ -97,7 +119,7 @@ function startServer({ inputFile, inputDir, port }) {
// Index page
if (pathname === '/') {
const files = findMarkdownFiles(baseDir);
const files = getCachedFiles();
// Redirect to Home.md if it exists
const homePage = files.find((f) =>
/^home\.md$/i.test(path.basename(f)) && path.dirname(f) === baseDir
@@ -108,8 +130,12 @@ function startServer({ inputFile, inputDir, port }) {
res.end();
return;
}
const indexKey = '__index__';
if (!pageCache.has(indexKey)) {
pageCache.set(indexKey, buildIndex(baseDir, files, true));
}
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
res.end(buildIndex(baseDir, files, true));
res.end(pageCache.get(indexKey));
return;
}
@@ -127,10 +153,12 @@ function startServer({ inputFile, inputDir, port }) {
if (!candidate.startsWith(baseDir + path.sep) && candidate !== baseDir) continue;
if (fs.existsSync(candidate) && /\.md$/i.test(candidate)) {
try {
const files = findMarkdownFiles(baseDir);
const html = convert(candidate, true, { baseDir, files });
if (!pageCache.has(candidate)) {
const files = getCachedFiles();
pageCache.set(candidate, convert(candidate, true, { baseDir, files }));
}
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
res.end(html);
res.end(pageCache.get(candidate));
} catch (err) {
res.writeHead(500);
res.end(`Erreur de conversion : ${err.message}`);
@@ -143,12 +171,41 @@ function startServer({ inputFile, inputDir, port }) {
serveStatic(req, res, pathname, baseDir, port);
});
function warmCache() {
const files = getCachedFiles();
let count = 0;
if (isDir) {
for (const file of files) {
if (!pageCache.has(file)) {
try {
pageCache.set(file, convert(file, true, { baseDir, files }));
count++;
} catch { /* ignore errors on warm-up */ }
}
}
} else {
if (!pageCache.has(inputFile)) {
try {
pageCache.set(inputFile, convert(inputFile, true));
count++;
} catch { /* ignore */ }
}
}
if (count > 0) console.log(`🔥 Cache préchauffé : ${count} page(s) prête(s)\n`);
}
server.listen(port, () => {
console.log(`\n🚀 Serveur démarré : http://localhost:${port}`);
if (isDir) {
console.log(`📂 Dossier : ${baseDir}`);
}
console.log('👁 Surveillance des modifications... (Ctrl+C pour arrêter)\n');
if (onReady) {
onReady(port);
} else {
console.log('👁 Surveillance des modifications... (Ctrl+C pour arrêter)');
}
// Pre-render all pages so first visits are instant
setImmediate(warmCache);
});
server.on('error', (err) => {