format
This commit is contained in:
parent
e587d1a933
commit
06a8bab54a
2
cli.js
2
cli.js
|
@ -24,7 +24,7 @@ require("yargs")
|
|||
(argv) => {
|
||||
fs.src(argv.files)
|
||||
.pipe(map(tokenize))
|
||||
.pipe(map(toHTML({css: argv["css"]})))
|
||||
.pipe(map(toHTML({ css: argv["css"] })))
|
||||
.pipe(fs.dest((file) => path.dirname(file.path)));
|
||||
}
|
||||
)
|
||||
|
|
40
to-html.js
40
to-html.js
|
@ -1,13 +1,15 @@
|
|||
const TOKENS_EXT = /\.tokens\.json$/;
|
||||
module.exports = ({css} = {css: true}) => (file, cb) => {
|
||||
module.exports = ({ css } = { css: true }) => (file, cb) => {
|
||||
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">
|
||||
<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">'
|
||||
: ''}
|
||||
<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">'
|
||||
: ""
|
||||
}
|
||||
<body>
|
||||
${toHTML(JSON.parse(file.contents.toString("utf8")))}
|
||||
</body>
|
||||
|
@ -17,29 +19,29 @@ ${toHTML(JSON.parse(file.contents.toString("utf8")))}
|
|||
};
|
||||
|
||||
function toHTML(tokens) {
|
||||
let body = []
|
||||
let body = [];
|
||||
|
||||
let cursor = tokens.shift()
|
||||
let cursor = tokens.shift();
|
||||
while (tokens.length) {
|
||||
if (cursor.pre) {
|
||||
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)
|
||||
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);
|
||||
}
|
||||
if (cursor.li) {
|
||||
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)
|
||||
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);
|
||||
}
|
||||
body.push(line(cursor))
|
||||
cursor = tokens.shift()
|
||||
body.push(line(cursor));
|
||||
cursor = tokens.shift();
|
||||
}
|
||||
|
||||
return body.join("\n")
|
||||
return body.join("\n");
|
||||
}
|
||||
|
||||
function line({ text, href, title, pre, alt, h1, h2, h3, li, quote }) {
|
||||
|
|
Loading…
Reference in a new issue