2021-01-28 23:07:57 +00:00
|
|
|
const TOKENS_EXT = /\.tokens\.json$/;
|
2021-01-28 23:13:02 +00:00
|
|
|
module.exports = ({ css } = { css: true }) => (file, cb) => {
|
2021-01-28 23:07:57 +00:00
|
|
|
if (!TOKENS_EXT.test(file.path)) return cb(null, file);
|
|
|
|
file.path = file.path.replace(TOKENS_EXT, ".html");
|
|
|
|
file.contents = Buffer.from(`<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<meta charset="utf-8">
|
2021-01-28 23:13:02 +00:00
|
|
|
<meta name="viewport" content="width=device-width,initial-scale=1">${
|
|
|
|
css
|
|
|
|
? '\n<meta name="color-scheme" content="dark light">\n<link rel="stylesheet" href="/gmi.min.css">'
|
|
|
|
: ""
|
|
|
|
}
|
2021-01-28 23:07:57 +00:00
|
|
|
<body>
|
|
|
|
${toHTML(JSON.parse(file.contents.toString("utf8")))}
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`);
|
|
|
|
return cb(null, file);
|
|
|
|
};
|
|
|
|
|
|
|
|
function toHTML(tokens) {
|
2021-01-28 23:13:02 +00:00
|
|
|
let body = [];
|
2021-01-28 23:07:57 +00:00
|
|
|
|
2021-01-28 23:13:02 +00:00
|
|
|
let cursor = tokens.shift();
|
2021-01-28 23:07:57 +00:00
|
|
|
while (tokens.length) {
|
|
|
|
if (cursor.pre) {
|
2021-01-28 23:13:02 +00:00
|
|
|
body.push(`<pre${cursor.alt ? `title="${cursor.alt}"` : ""}>`);
|
|
|
|
const closing = tokens.findIndex((token) => token.pre);
|
|
|
|
body = body.concat(tokens.slice(0, closing).map(({ text }) => text));
|
|
|
|
body.push("</pre>");
|
|
|
|
tokens = tokens.slice(closing + 1);
|
2021-01-28 23:07:57 +00:00
|
|
|
}
|
|
|
|
if (cursor.li) {
|
2021-01-28 23:13:02 +00:00
|
|
|
body.push(`<ul>`);
|
|
|
|
const closing = tokens.findIndex((token) => !token.li);
|
|
|
|
body = body.concat(tokens.slice(0, closing).map(line));
|
|
|
|
body.push("</ul>");
|
|
|
|
tokens = tokens.slice(closing + 1);
|
2021-01-28 23:07:57 +00:00
|
|
|
}
|
2021-01-28 23:13:02 +00:00
|
|
|
body.push(line(cursor));
|
|
|
|
cursor = tokens.shift();
|
2021-01-28 23:07:57 +00:00
|
|
|
}
|
|
|
|
|
2021-01-28 23:13:02 +00:00
|
|
|
return body.join("\n");
|
2021-01-28 23:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function line({ text, href, title, pre, alt, h1, h2, h3, li, quote }) {
|
|
|
|
if (text) return `<p>${text}</p>`;
|
|
|
|
if (href) return `<a href="${href}">${title || href}</a>`;
|
|
|
|
if (h1) return `<h1>${h1}</h1>`;
|
|
|
|
if (h2) return `<h2>${h2}</h2>`;
|
|
|
|
if (h3) return `<h3>${h3}</h3>`;
|
|
|
|
if (li) return `<li>${li}</li>`;
|
|
|
|
if (quote) return `<blockquote>${quote}</blockquote>`;
|
|
|
|
return `<p><br></p>`;
|
|
|
|
}
|