Skip to content

Instantly share code, notes, and snippets.

@lawlesscreation
Last active March 17, 2021 08:58
Show Gist options
  • Save lawlesscreation/01069499d9031cecbb708e15d21f4f43 to your computer and use it in GitHub Desktop.
Save lawlesscreation/01069499d9031cecbb708e15d21f4f43 to your computer and use it in GitHub Desktop.
Count tags in HTML files using Node
'use strict';
const fs = require('fs');
const path = require('path');
console.log('🛠 Scanning HTML files...');
const files = getAllFiles('./', ['.html']);
const tagCount = countTags(files);
console.log(`tags:`, tagCount);
console.log(`found ${Object.keys(tagCount).length} unique tags in ${files.length} files`);
function getAllFiles(initialPath, fileExtensions) {
var fullPaths = [];
var files = fs.readdirSync(initialPath);
files.forEach((file) => {
var joinedPath = path.join(initialPath, file);
if (fs.lstatSync(joinedPath).isDirectory()) {
fullPaths = fullPaths.concat(getAllFiles(joinedPath, fileExtensions));
} else {
fileExtensions.forEach((fileExtension) => {
if (file.indexOf(fileExtension) >= 0 || fileExtension == null) {
fullPaths.push(joinedPath);
}
});
}
});
return fullPaths;
};
function countTags(files) {
const tagCount = {};
for (let index in files) {
if (files.hasOwnProperty(index)) {
const file = files[index];
const htmlString = fs.readFileSync(file, 'utf8');
const htmlTagRe = RegExp('<[a-z\-]+((\/>)|( |>))','gi');
const allTags = [];
console.log(`finding tags in ${file}`);
const matches = htmlString.matchAll(htmlTagRe);
for (const match of matches) {
let newMatch = match[0].replace(/</g, '').replace(/>/g, '').trim();
allTags.push(newMatch);
}
allTags.forEach(tag => {
tagCount[tag] = (tagCount[tag] || 0) + 1;
});
}
};
// Sort and return
return Object.entries(tagCount).sort(([,a],[,b]) => b-a).reduce((r, [k, v]) => ({ ...r, [k]: v }), {});;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment