title and language meta

This commit is contained in:
Talon Poole 2021-01-29 23:17:25 +00:00
parent 24d40581d5
commit 8ea7873403
3 changed files with 31 additions and 16 deletions

15
cli.js
View file

@ -39,6 +39,12 @@ require("yargs")
describe: "The *.gmi files to convert", describe: "The *.gmi files to convert",
}) })
.required("files", true) .required("files", true)
.option("language", {
alias: "lang",
required: true,
type: "string",
describe: "Set the document meta language tag",
})
.option("images", { .option("images", {
type: "boolean", type: "boolean",
default: false, default: false,
@ -61,6 +67,7 @@ require("yargs")
"gmi.css is included by default. Use --no-css to for the bare-minimum <style>", "gmi.css is included by default. Use --no-css to for the bare-minimum <style>",
}), }),
(argv) => { (argv) => {
console.log(argv)
const override = GMI_CSS_VARS.reduce((style, key) => { const override = GMI_CSS_VARS.reduce((style, key) => {
if (argv[key]) { if (argv[key]) {
style += `--${key}: ${argv[key]};`; style += `--${key}: ${argv[key]};`;
@ -72,13 +79,7 @@ require("yargs")
.pipe( .pipe(
map( map(
toHTML({ toHTML({
css: argv.css, ...argv,
inline: {
images: argv.images,
audio: argv.audio,
video: argv.video,
},
verbose: argv.verbose,
override, override,
}) })
) )

View file

@ -15,6 +15,9 @@ and mobile-friendly fashion!
# OPTIONS # OPTIONS
*--languge*
Required for <meta languge="">
*CSS Variables* *CSS Variables*
Any variable documented in *gmi.css*(5) can be set via _OPTIONS_ of the same Any variable documented in *gmi.css*(5) can be set via _OPTIONS_ of the same
name. name.

View file

@ -8,14 +8,19 @@ const VIDEO_EXT = (exports.VIDEO_EXT = /\.(mp4|webm)$/);
const BASE_STYLE = (exports.baseStyle = const BASE_STYLE = (exports.baseStyle =
"<style>p,a,pre,h1,h2,h3,ul,blockquote,img,audio,video{display:block;max-width:100%;margin:0;padding:0;overflow-wrap:anywhere;}</style>"); "<style>p,a,pre,h1,h2,h3,ul,blockquote,img,audio,video{display:block;max-width:100%;margin:0;padding:0;overflow-wrap:anywhere;}</style>");
const toHTML = (exports.toHTML = (file, options) => `<!DOCTYPE html> const toHTML = (exports.toHTML = (file, options) => {
const tokens = JSON.parse(file.contents.toString("utf8"));
const title = tokens[0].h1;
return `<!DOCTYPE html>
<html style="${options.override}"> <html style="${options.override}">
${head(file.path, options)} ${head(file.path, title, options)}
<body> <body>
${gemtext(JSON.parse(file.contents.toString("utf8")), options)} ${gemtext(tokens, options)}
</body> </body>
</html> </html>
`); `;
});
module.exports = (options) => (file, cb) => { module.exports = (options) => (file, cb) => {
if (!TOKENS_EXT.test(file.path)) return cb(null); if (!TOKENS_EXT.test(file.path)) return cb(null);
file.contents = Buffer.from(toHTML(file, options)); file.contents = Buffer.from(toHTML(file, options));
@ -25,9 +30,10 @@ module.exports = (options) => (file, cb) => {
}; };
// TODO: meta: language, description, canonical // TODO: meta: language, description, canonical
function head(file, options) { function head(file, title, options) {
return `<head> return `<head>
<meta charset="utf-8"> <meta charset="utf-8">
<meta language="${options.language}">
<meta name="viewport" content="width=device-width,initial-scale=1">${ <meta name="viewport" content="width=device-width,initial-scale=1">${
!options.css !options.css
? BASE_STYLE ? BASE_STYLE
@ -35,7 +41,12 @@ function head(file, options) {
path.resolve(__dirname, "./gmi.min.css"), path.resolve(__dirname, "./gmi.min.css"),
"utf8" "utf8"
)}</style>` )}</style>`
}${
!options.canonical
? ""
: `<link rel="canonical" href="gemini://${options.canonical}/${file.replace(TOKENS_EXT, ".gmi")}">`
} }
<title>${title}</title>
</head> </head>
`; `;
} }
@ -68,15 +79,15 @@ function gemtext(tokens, options) {
function line( function line(
{ text, href, title, pre, alt, h1, h2, h3, li, quote }, { text, href, title, pre, alt, h1, h2, h3, li, quote },
{ inline } { images, audio, video }
) { ) {
if (text) return `<p>${text}</p>`; if (text) return `<p>${text}</p>`;
if (href) { if (href) {
if (inline.images && IMG_EXT.test(href)) if (images && IMG_EXT.test(href))
return `<img src="${href}" title="${title}"/>`; return `<img src="${href}" title="${title}"/>`;
if (inline.audio && AUDIO_EXT.test(href)) if (audio && AUDIO_EXT.test(href))
return `<audio controls src="${href}" title="${title}"></audio>`; return `<audio controls src="${href}" title="${title}"></audio>`;
if (inline.video && VIDEO_EXT.test(href)) if (video && VIDEO_EXT.test(href))
return `<video controls src="${href}" title="${title}"/></video>`; return `<video controls src="${href}" title="${title}"/></video>`;
return `<a href="${href}">${title || href}</a>`; return `<a href="${href}">${title || href}</a>`;