#!/usr/bin/env node const { loadArticles } = require("../lib/stats/articles"); const { renderWithPython } = require("../lib/stats/python"); const MONTH_LABELS = ["Jan", "Fev", "Mar", "Avr", "Mai", "Jun", "Jul", "Aou", "Sep", "Oct", "Nov", "Dec"]; function groupByMonthAndYear(articles) { const counts = new Map(); const years = new Set(); let first = null; let last = null; for (const article of articles) { if (!article.date) continue; const year = article.date.year; const month = article.date.month; // 1..12 years.add(year); const key = `${year}-${month}`; counts.set(key, (counts.get(key) || 0) + 1); if (!first || article.date < first) first = article.date; if (!last || article.date > last) last = article.date; } const monthNumbers = Array.from({ length: 12 }, (_, index) => index + 1); const labels = monthNumbers.map((month) => MONTH_LABELS[month - 1]); const sortedYears = Array.from(years).sort((a, b) => a - b); const series = sortedYears.map((year) => { return { label: String(year), values: monthNumbers.map((month) => counts.get(`${year}-${month}`) || 0), }; }); return { labels, series, first, last }; } async function run({ contentDir, outputPath, publicPath }) { if (!outputPath) { throw new Error("outputPath manquant pour articles_per_month"); } const articles = await loadArticles(contentDir || "content"); const { labels, series, first, last } = groupByMonthAndYear(articles); await renderWithPython({ type: "articles_per_month", outputPath, data: { labels, series, title: "Articles par mois", }, }); return { image: publicPath, meta: { from: first ? first.toISODate() : null, to: last ? last.toISODate() : null, months: labels.length, }, }; } module.exports = { run };