Page de statistiques
This commit is contained in:
99
tools/lib/content.js
Normal file
99
tools/lib/content.js
Normal file
@@ -0,0 +1,99 @@
|
||||
const fs = require("fs/promises");
|
||||
const path = require("path");
|
||||
|
||||
async function collectMarkdownFiles(rootDir, { skipIndex = true } = {}) {
|
||||
const entries = await fs.readdir(rootDir, { withFileTypes: true });
|
||||
const files = [];
|
||||
|
||||
for (const entry of entries) {
|
||||
const fullPath = path.join(rootDir, entry.name);
|
||||
|
||||
if (entry.isDirectory()) {
|
||||
const nested = await collectMarkdownFiles(fullPath, { skipIndex });
|
||||
files.push(...nested);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!entry.isFile()) continue;
|
||||
if (!entry.name.toLowerCase().endsWith(".md")) continue;
|
||||
if (skipIndex && entry.name === "_index.md") continue;
|
||||
|
||||
files.push(fullPath);
|
||||
}
|
||||
|
||||
return files;
|
||||
}
|
||||
|
||||
async function collectSectionIndexDirs(rootDir) {
|
||||
const sections = new Set();
|
||||
|
||||
async function walk(dir) {
|
||||
let entries;
|
||||
try {
|
||||
entries = await fs.readdir(dir, { withFileTypes: true });
|
||||
} catch (error) {
|
||||
console.error(`Skipping section scan for ${dir}: ${error.message}`);
|
||||
return;
|
||||
}
|
||||
|
||||
let hasIndex = false;
|
||||
for (const entry of entries) {
|
||||
if (entry.isFile() && entry.name.toLowerCase() === "_index.md") {
|
||||
hasIndex = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (hasIndex) {
|
||||
sections.add(path.resolve(dir));
|
||||
}
|
||||
|
||||
for (const entry of entries) {
|
||||
if (entry.isDirectory()) {
|
||||
await walk(path.join(dir, entry.name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await walk(rootDir);
|
||||
return sections;
|
||||
}
|
||||
|
||||
async function resolveMarkdownTargets(inputs, { rootDir = process.cwd(), skipIndex = true } = {}) {
|
||||
if (!inputs || inputs.length === 0) {
|
||||
return collectMarkdownFiles(rootDir, { skipIndex });
|
||||
}
|
||||
|
||||
const targets = new Set();
|
||||
|
||||
for (const input of inputs) {
|
||||
const resolved = path.resolve(input);
|
||||
|
||||
try {
|
||||
const stat = await fs.stat(resolved);
|
||||
|
||||
if (stat.isDirectory()) {
|
||||
const nested = await collectMarkdownFiles(resolved, { skipIndex });
|
||||
nested.forEach((file) => targets.add(file));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (stat.isFile()) {
|
||||
const lower = resolved.toLowerCase();
|
||||
if (!lower.endsWith(".md")) continue;
|
||||
if (skipIndex && path.basename(resolved) === "_index.md") continue;
|
||||
targets.add(resolved);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Skipping ${input}: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
return Array.from(targets);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
collectMarkdownFiles,
|
||||
collectSectionIndexDirs,
|
||||
resolveMarkdownTargets,
|
||||
};
|
||||
Reference in New Issue
Block a user