Skip to content

Instantly share code, notes, and snippets.

@wbamberg
Last active April 7, 2021 21:26
Show Gist options
  • Save wbamberg/20d0d7a99f38ea9c188b6003a653661c to your computer and use it in GitHub Desktop.
Save wbamberg/20d0d7a99f38ea9c188b6003a653661c to your computer and use it in GitHub Desktop.
"use strict";
const fs = require("fs")
const path = require("path")
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
/* get all ".html" files under the given directory into an array */
function getAllFiles(dirPath, files) {
const dir = fs.readdirSync(dirPath);
files = files || [];
for (const file of dir) {
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
files = getAllFiles(dirPath + "/" + file, files)
} else {
if (file.endsWith(".html")) {
files.push(path.join(__dirname, dirPath, "/", file))
}
}
}
return files
}
const allDocPaths = getAllFiles(process.argv[2]);
const classToFind = process.argv[3];
/* scrape off the front matter.
We're considering this very roughly as the second occurrence of "---" in the file.*/
function removeFrontMatter(doc) {
const regexp = /---/g;
const matchArray = [...doc.matchAll(regexp)];
let end = matchArray[1].index + 3;
return doc.substr(end);
}
for (const docPath of allDocPaths) {
let docHTML = fs.readFileSync(docPath, {encoding: "utf-8"});
docHTML = removeFrontMatter(docHTML);
const { window } = new JSDOM(docHTML);
const found = window.document.querySelectorAll(`.${classToFind}`);
for (const item of found) {
console.log(docPath);
}
window.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment