Skip to content

Instantly share code, notes, and snippets.

@glowcoil
Last active February 4, 2019 00:37
Show Gist options
  • Save glowcoil/4dcd79db86b277eb099a7e71a587e21b to your computer and use it in GitHub Desktop.
Save glowcoil/4dcd79db86b277eb099a7e71a587e21b to your computer and use it in GitHub Desktop.
var fs = require("fs");
var katex = require("katex");
function walk(dir) {
fs.readdir(dir, function(err, list) {
if (err) { console.log(err); return; }
list.forEach(function(file) {
var path = dir + "/" + file;
fs.stat(path, function(err, stat) {
if (stat && stat.isDirectory()) {
walk(path);
} else if (path.slice(-5) == ".html") {
console.log(path);
fs.readFile(path, "utf8", function(err, text) {
if (err) { console.log(err); return; }
var segments = [];
var start = 0;
var i = 0;
while (i < text.length) {
if (text.slice(i, i + 2) == "\\[") {
segments.push(text.slice(start, i));
start = i + 2;
i = start;
while (text.slice(i, i + 2) != "\\]" && i < text.length) {
i++;
}
segments.push(katex.renderToString(text.slice(start, i), { displayMode: true }));
start = i + 2;
i = start;
} else if (text.slice(i, i + 2) == "\\(") {
segments.push(text.slice(start, i));
start = i + 2;
i = start;
while (text.slice(i, i + 2) != "\\)" && i < text.length) {
i++;
}
segments.push(katex.renderToString(text.slice(start, i)));
start = i + 2;
i = start;
} else {
i++;
}
}
if (i > start) {
segments.push(text.slice(start, i));
}
var output = segments.join("");
fs.writeFile(path, output, "utf8", function(err) {
if (err) { console.log(err); return; }
});
});
}
});
});
});
}
walk("public");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment