const fs = require("fs"); const path = require("path"); const TOKENS_EXT = /\.tokens\.json$/; // https://developer.mozilla.org/en-US/docs/Web/Media/Formats const IMG_EXT = (exports.IMG_EXT = /\.(apng|avif|gif|jpg|jpeg|jfif|pjpeg|pjp|png|svg|webp)$/); const AUDIO_EXT = (exports.AUDIO_EXT = /\.(mp3|wav|aac|aacp|mpeg|off|flac)$/); const VIDEO_EXT = (exports.VIDEO_EXT = /\.(mp4|webm)$/); const BASE_STYLE = (exports.baseStyle = ""); const toHTML = (exports.toHTML = (file, options) => ` ${head(file.path, options)}
${gemtext(JSON.parse(file.contents.toString("utf8")), options)} `); module.exports = (options) => (file, cb) => { if (!TOKENS_EXT.test(file.path)) return cb(null); file.contents = Buffer.from(toHTML(file, options)); file.path = file.path.replace(TOKENS_EXT, ".html"); if (options.verbose) console.log(file.path); return cb(null, file); }; // TODO: meta: language, description, canonical function head(file, options) { return ` ${ !options.css ? BASE_STYLE : `\n\n` } `; } function gemtext(tokens, options) { let body = []; let cursor = tokens.shift(); while (tokens.length) { if (cursor.pre) { body.push(``); const closing = tokens.findIndex((token) => token.pre); body = body.concat(tokens.slice(0, closing).map(({ text }) => text)); body.push(""); tokens = tokens.slice(closing + 1); } if (cursor.li) { body.push(`
${text}
`; if (href) { if (inline.images && IMG_EXT.test(href)) return ``; if (inline.audio && AUDIO_EXT.test(href)) return ``; if (inline.video && VIDEO_EXT.test(href)) return ``; return `${title || href}`; } if (h1) return `${quote}`; return `