diff --git a/docs/_includes/page-card.njk b/docs/_includes/page-card.njk index 9b5f67f17..f03980e63 100644 --- a/docs/_includes/page-card.njk +++ b/docs/_includes/page-card.njk @@ -1,4 +1,4 @@ - +
{% include "svgs/" + (page.data.icon or "thumbnail-placeholder") + ".njk" %} diff --git a/docs/_includes/sidebar-group.njk b/docs/_includes/sidebar-group.njk index 6b440f4ee..4dd6b6586 100644 --- a/docs/_includes/sidebar-group.njk +++ b/docs/_includes/sidebar-group.njk @@ -1,5 +1,5 @@ - +

{% set groupUrl %}/docs/{{ tag }}/{% endset %} {% if groupUrl | getCollectionItemFromUrl %} diff --git a/docs/_utils/filters.js b/docs/_utils/filters.js index 562f5cfa5..e3949edf4 100644 --- a/docs/_utils/filters.js +++ b/docs/_utils/filters.js @@ -198,3 +198,41 @@ export function getCategoryTitle(category, categories) { // Capitalized return category.charAt(0).toUpperCase() + category.slice(1); } + +const IDENTITY = x => x; + +/** + * Helper to print out one or more HTML attributes, especially conditional ones. + * Usage in 11ty: + * - Single attribute: `` + * - Multiple attributes: `` + * + * @overload + * @param {any} value - The attribute value If falsey, the attribute is not printed. If `true` the attribute is printed without a value. + * @param {string} name - The name of the attribute + * + * @overload + * @param {Object} obj - Map of attribute names to values + * + * @returns {string} The attribute string. No `| safe` is needed. + */ +export function attr(value, name) { + const safe = this?.env.filters.safe ?? IDENTITY; + + if (arguments.length === 1 && value && typeof value === 'object') { + // Called with a single object argument of names to values + let ret = Object.entries(obj) + .map(([name, value]) => attr(value, name)) + .join(''); + return safe(ret); + } + + if (!value) { + // false, "", null, undefined + return ''; + } + + let ret = ' ' + name + (value === true ? '' : `="${value}"`); + + return safe(ret); +}