Skip to content

Instantly share code, notes, and snippets.

@pdehaan
Created April 1, 2022 00:00
Show Gist options
  • Select an option

  • Save pdehaan/72988a462a56a7db5d21173952b963c3 to your computer and use it in GitHub Desktop.

Select an option

Save pdehaan/72988a462a56a7db5d21173952b963c3 to your computer and use it in GitHub Desktop.
An API version of `npx htmlhint --rules=tag-pair --nocolor **/*.ftl`
import fs from "node:fs/promises";
import glob from "fast-glob";
import {HTMLHint} from "htmlhint";
const res = await verifyGlob("**/*.ftl", false);
console.log(res);
async function verifyGlob(fileglob, ignoreComments=true) {
let errors = [];
for (const file of await glob([fileglob, "!node_modules/**"])) {
errors = errors.concat(await verifyFtl(file, ignoreComments));
process.errorCode = 1;
}
return errors;
}
async function verifyFtl(filename, ignoreComments=true) {
let ftl = await fs.readFile(filename, "utf-8");
if (ignoreComments) {
ftl = $ignoreComments(ftl);
}
return HTMLHint.verify(ftl, {"tag-pair": true})
.map(err => Object.assign(err, {filename}));
}
function $ignoreComments(str="") {
return str.split("\n")
.map(line => {
if (!line.trim().startsWith("#")) {
return line;
}
// Return a "fake" line so our line numbers match up; but this should effectively ignore any .ftl comments.
return "# (comment removed)";
})
.join("\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment