gmi-web/tokenize.js

18 lines
481 B
JavaScript
Raw Normal View History

2021-01-28 21:03:30 +00:00
const GMI_EXT = /\.gmi$/;
const GMI_REGEX = require("./gmi.regex");
2021-01-29 23:59:12 +00:00
const tokenize = (exports.tokenize = (file) =>
JSON.stringify(
file.contents
.toString("utf8")
.split("\n")
.map((line) => GMI_REGEX.exec(line).groups),
null,
2
));
2021-01-28 21:03:30 +00:00
module.exports = (file, cb) => {
if (!GMI_EXT.test(file.path)) return cb(null, file);
2021-01-29 23:59:12 +00:00
file.contents = Buffer.from(tokenize(file));
2021-01-28 21:03:30 +00:00
file.path = file.path.replace(GMI_EXT, ".tokens.json");
cb(null, file);
};