Skip to content

Commit

Permalink
Add tag count listing tool
Browse files Browse the repository at this point in the history
  • Loading branch information
quincylvania committed Jul 2, 2024
1 parent b22c4e7 commit a7f52bf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
49 changes: 49 additions & 0 deletions _tools/stats-analyzer/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// stats-analyzer for dogwood

import fs from 'fs';
import graymatter from 'gray-matter';

import process from 'process';
import minimist from 'minimist';
const argv = minimist(process.argv.slice(2));
let sourcePath = process.env.npm_config_srcdir || argv.srcdir || '.';
if (sourcePath.endsWith('/')) sourcePath.slice(0, -1);

var tagCounts = {};

function collectFileStats(json) {
for (var i in json.tags || []) {
var tag = json.tags[i];
if (!tagCounts[tag]) tagCounts[tag] = 0;
tagCounts[tag] += 1;
}
}

function collectStats(path) {
const dir = fs.opendirSync(path);
let dirent;
while ((dirent = dir.readSync()) !== null) {
const subpath = path + '/' + dirent.name;
if (dirent.isFile()) {
const filedata = fs.readFileSync(subpath);
const info = graymatter(filedata);

// ignore files not in Jekyll format
if (Object.keys(info.data).length > 0) {
// parse to string and back again to make sure dates are in string format
let data = JSON.parse(JSON.stringify(info.data));
collectFileStats(data);
}
} else if (dirent.isDirectory()) {
collectStats(subpath);
}
}
dir.closeSync();
}

collectStats(sourcePath + '/_posts');

let tags = Object.keys(tagCounts).sort();
tags.forEach(function(tag) {
console.log(tagCounts[tag] + ' ' + tag);
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"cache:embedded": "node ./_tools/embedded-page-cacher/index.js --srcdir=.",
"cache:images": "node ./_tools/image-cacher/index.js --srcdir=.",
"cache": "npm-run-all -p cache:*",
"stats": "node ./_tools/stats-analyzer/index.js --srcdir=.",
"thumbnails": "node ./_tools/thumbnail-generator/index.js --srcdir=.",
"validate:frontmatter": "node ./_tools/frontmatter-validator/index.js --srcdir=.",
"validate:people": "node ./_tools/people-validator/index.js --srcdir=.",
Expand Down

0 comments on commit a7f52bf

Please sign in to comment.