fix custom css loading
[squeep-html-template-helper] / lib / template-helper.js
index 7e1693c857758c127d1063d30804c6c5fab9b2e6..c72e0ed348415d2c8adf6f1366b1639330819f65 100644 (file)
@@ -4,7 +4,18 @@
  * A bunch of shorthand to put together common parts of an HTML page. 
  */
 
-const { lazy } = require('@squeep/lazy');
+const { lazy } = require('@squeep/lazy-property');
+
+
+/**
+ * Set up expected fields for how we handle error reporting
+ * and whatnot.
+ * @param {Object} ctx
+ */
+const initContext = (ctx) => {
+  ctx.errors = [];
+  ctx.notifications = [];
+}
 
 /**
  * Some fields may have values outside normal dates, handle them here.
@@ -29,7 +40,9 @@ const dateOrNot = (date, otherwise) => {
 
 /**
  * Why is rendering a Date as a string this complicated?
- * @param {Date|Number} date
+ * We handle the infinities because pg-promise might provide those in
+ * lieu of a Date object from timestamp fields.
+ * @param {Date|Number|String} date
  * @param {String=} pInf
  * @param {String=} nInf
  * @param {String=} otherwise
@@ -43,6 +56,7 @@ const dateFormat = (date, pInf = 'Never', nInf = 'Forever', otherwise = '') => {
       return nInf;
     default:
       if (!date
+      ||  Number.isNaN(date.valueOf())
       ||  (!(date instanceof Date) && !isDatableType)) {
         return otherwise;
       }
@@ -62,6 +76,33 @@ lazy(dateFormat, '_dtf', () => {
 });
 
 
+/**
+ * Wrap a Date in a <time> block.
+ * @param {Date} date
+ * @param {Object} options
+ * @param {String=} options.title
+ */
+const timeElement = (date, options = {}) => {
+  const {
+    title,
+    pInf,
+    nInf,
+    otherwise,
+  } = options;
+  const attributes = {
+    ...(title && { title }),
+    ...(date instanceof Date && { datetime: date.toISOString() }),
+  };
+  return [
+    '<time',
+    elementAttributes(attributes),
+    '>',
+    dateFormat(date, pInf, nInf, otherwise),
+    '</time>',
+  ].join('');
+}
+
+
 /**
  * Render a duration.
  * @param {Number} seconds
@@ -90,6 +131,17 @@ const secondsToPeriod = (seconds) => {
 };
 
 
+/**
+ * Return array of strings prefixed with tabs.
+ * @param {Number} indent
+ * @param {String[]} list
+ */
+const indented = (indent, list) => {
+  const spacer = '\t'.repeat(indent);
+  return list.map((l) => `${spacer}${l}`);
+};
+
+
 /**
  * Render the preamble <head> for an HTML page.
  * @param {Number} pagePathLevel number of paths below root this page is
@@ -108,8 +160,8 @@ function htmlHead(pagePathLevel, ctx, options) {
   return `\t<head>
 \t\t<meta charset="utf-8">
 \t\t<meta name="viewport" content="width=device-width,initial-scale=1">
-\t\t<link rel="stylesheet" href="${rootPathPfx}static/theme.css" title="Default">
-\t\t<link rel="stylesheet" href="${rootPathPfx}static/custom.css" title="Site Specific">
+\t\t<link rel="stylesheet" href="${rootPathPfx}static/theme.css">
+\t\t<link rel="stylesheet" href="${rootPathPfx}static/custom.css">
 ${headElements.map((e) => '\t\t' + e).join('\n')}
 \t\t<title>${pageTitle}</title>
 \t</head>`;
@@ -351,8 +403,10 @@ function htmlPage(pagePathLevel, ctx, options, main = []) {
 
 
 module.exports = {
+  initContext,
   dateOrNot,
   dateFormat,
+  timeElement,
   secondsToPeriod,
   htmlHead,
   htmlBody,
@@ -360,6 +414,7 @@ module.exports = {
   htmlHeader,
   htmlFooter,
   htmlMessages,
+  indented,
   renderNavLink,
   LI,
   UL,