diff --git a/html.js b/html.js index 3aed862..fdf764e 100644 --- a/html.js +++ b/html.js @@ -11,6 +11,26 @@ export const tokenize = (gemtext) => ) ); +export function toHTML(gemtext, options) { + options.inlineCSS = options.inlineCSS || CSS.inline(options); + options.styleTag = options.styleTag || CSS.style(options); + + const tokens = tokenize(gemtext); + + if (options.body) return body(tokens, options); + + return ` + +
${head(tokens, options)} + +${body(tokens, options)} + + +`; +} + export function block( { text, href, title, pre, alt, h1, h2, h3, li, quote }, options = {} @@ -21,6 +41,14 @@ export function block( if (text) { content = text; } + if (li) { + type = "li"; + content = li; + } + if (quote) { + type = "blockquote"; + content = quote; + } if (h1) { type = "h1"; content = h1; @@ -33,17 +61,10 @@ export function block( type = "h3"; content = h3; } - if (li) { - type = "li"; - content = li; - } - if (quote) { - type = "blockquote"; - content = quote; - } if (href) { const matchesExt = (url, exts) => exts.some((ext) => new RegExp(`\.${ext}$`).test(url)); + if (options.image && matchesExt(href, options.image)) { type = "img"; props += ` src="${href}"`; @@ -62,10 +83,12 @@ export function block( props += ` href="${href}"`; } } - if (options.body || options.inline) + + if (options.body || options.inline) { props += options.inlineCSS( type === "p" && content === "" ? "p:empty" : type ); + } return `<${type}${props}>${escape(content)}${type}>`; } @@ -102,52 +125,7 @@ export function body(tokens, options) { return blocks.join("\n"); } -export function toHTML(gemtext, options) { - options.inlineCSS = options.inlineCSS || CSS.inline(options); - options.styleTag = options.styleTag || CSS.style(options); - - const tokens = tokenize(gemtext); - - if (options.body) return body(tokens, options); - - return ` - -${head( - Object.assign(options, { - title: tokens[0].h1, - description: description(tokens, options), - }) - )} - -${body(tokens, options)} - - -`; -} - -export function head(options) { - return ` - -${ - options.styleTag - } -