55 lines
1.5 KiB
JavaScript
55 lines
1.5 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
const { loadArticles } = require("../lib/stats/articles");
|
|
const { renderWithPython } = require("../lib/stats/python");
|
|
|
|
function groupByYear(articles) {
|
|
const counts = new Map();
|
|
let first = null;
|
|
let last = null;
|
|
|
|
for (const article of articles) {
|
|
if (!article.date) continue;
|
|
const year = article.date.year;
|
|
counts.set(year, (counts.get(year) || 0) + 1);
|
|
if (!first || article.date < first) first = article.date;
|
|
if (!last || article.date > last) last = article.date;
|
|
}
|
|
|
|
const entries = Array.from(counts.entries()).sort((a, b) => a[0] - b[0]);
|
|
const labels = entries.map(([year]) => `${year}`);
|
|
const values = entries.map(([, value]) => value);
|
|
|
|
return { labels, values, first, last };
|
|
}
|
|
|
|
async function run({ contentDir, outputPath, publicPath }) {
|
|
if (!outputPath) {
|
|
throw new Error("outputPath manquant pour articles_per_year");
|
|
}
|
|
|
|
const articles = await loadArticles(contentDir || "content");
|
|
const { labels, values, first, last } = groupByYear(articles);
|
|
|
|
await renderWithPython({
|
|
type: "articles_per_year",
|
|
outputPath,
|
|
data: {
|
|
labels,
|
|
values,
|
|
title: "Articles par an",
|
|
},
|
|
});
|
|
|
|
return {
|
|
image: publicPath,
|
|
meta: {
|
|
from: first ? first.toISODate() : null,
|
|
to: last ? last.toISODate() : null,
|
|
years: labels.length,
|
|
},
|
|
};
|
|
}
|
|
|
|
module.exports = { run };
|