X-Git-Url: http://git.squeep.com/?p=squeep-html-template-helper;a=blobdiff_plain;f=lib%2Ftemplate-helper.js;fp=lib%2Ftemplate-helper.js;h=e8163abbe41733c0e6af0293b49cf22102a98178;hp=7f7021ae010f2a58be394f17e23ea4120a4570d7;hb=d6b0485d9f665ccc886279e8bb447137acca2b9e;hpb=55ff163110484141dd9eb7f55e49a85bad082378 diff --git a/lib/template-helper.js b/lib/template-helper.js index 7f7021a..e8163ab 100644 --- a/lib/template-helper.js +++ b/lib/template-helper.js @@ -19,7 +19,7 @@ const initContext = (ctx) => { /** * Some fields may have values outside normal dates, handle them here. - * @param {Date} date + * @param {Date|Number} date * @param {String} otherwise */ const dateOrNot = (date, otherwise) => { @@ -203,7 +203,8 @@ ${htmlFooter(ctx, options)} * @returns {String} */ function renderNavLink(nav) { - return `${nav.text}`; + const aClass = nav.class ? ` class="${nav.class}"` : ''; + return `${nav.text}`; } @@ -277,7 +278,10 @@ ${spacer}` : ''; * @returns {String} */ function elementAttributes(attributes) { - const attr = Object.entries(attributes).map(([name, value]) => `${name}="${value}"`).join(' '); + const attr = Object.entries(attributes).map(([name, value]) => { + const v = value ? `="${value}"` : ''; + return `${name}${v}`; + }).join(' '); return attr ? ' ' + attr : ''; } @@ -295,19 +299,33 @@ function LI(item, indent = 0, attributes = {}) { } +/** + * Wrap an array of items in a list container element. + * @param {String} element + * @param {Number} indent + * @param {Object} attributes + * @param {String[]} items + * @param {(item, index, array) => {Object}} itemAttributeGenerator + * @returns {String} + */ +function listContainer(element, indent, attributes, items, itemAttributeGenerator) { + const spacer = '\t'.repeat(indent); + return `${spacer}<${element}${elementAttributes(attributes)}> +${items.map((item, index, array) => LI(item, indent + 1, itemAttributeGenerator(item, index, array))).join('\n')} +${spacer}`; +} + + /** * Wrap a list of items in an unordered list. * @param {String[]} items * @param {Number} indent * @param {Object} attributes - * @param {(item) => Object} itemAttributeGenerator + * @param {(item, index, array) => Object} itemAttributeGenerator * @returns {String} */ function UL(items, indent = 0, attributes = {}, itemAttributeGenerator = () => {}) { - const spacer = '\t'.repeat(indent); - return `${spacer} -${items.map((item) => LI(item, indent + 1, itemAttributeGenerator(item))).join('\n')} -${spacer}`; + return listContainer('ul', indent, attributes, items, itemAttributeGenerator); } @@ -316,15 +334,12 @@ ${spacer}`; * @param {String[]} items * @param {Number} indent * @param {Object} attributes - * @param {(item) => Object} itemAttributeGenerator + * @param {(item, index, array) => Object} itemAttributeGenerator * @returns {String} */ function OL(items, indent = 0, attributes = {}, itemAttributeGenerator = () => {}) { - const spacer = '\t'.repeat(indent); - return `${spacer} -${items.map((item) => LI(item, indent + 1, itemAttributeGenerator(item))).join('\n')} -${spacer}`; + return listContainer('ol', indent, attributes, items, itemAttributeGenerator); } @@ -404,7 +419,9 @@ module.exports = { indented, renderNavLink, LI, + listContainer, UL, OL, htmlPage, + elementAttributes, };