add timeElement()
[squeep-html-template-helper] / lib / template-helper.js
index 16a14cc4c3d0bdfd42406a03ef29d31f698eddaa..c983e80acd44329fc6c516ee874198fc2d2e6e58 100644 (file)
@@ -65,6 +65,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
@@ -367,6 +394,7 @@ function htmlPage(pagePathLevel, ctx, options, main = []) {
 module.exports = {
   dateOrNot,
   dateFormat,
+  timeElement,
   secondsToPeriod,
   htmlHead,
   htmlBody,