From 3071e76493a061291524b59dd932ade359e497e9 Mon Sep 17 00:00:00 2001 From: Cory Dransfeldt Date: Sun, 15 Jun 2025 15:44:18 -0700 Subject: [PATCH] chore(tablericons.js): expose renderTablerIcon function --- package-lock.json | 4 +-- package.json | 2 +- tablericons.js | 62 +++++++++++++++++++++++++++-------------------- 3 files changed, 39 insertions(+), 29 deletions(-) diff --git a/package-lock.json b/package-lock.json index 9035e73..3a7b507 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@cdransf/eleventy-plugin-tabler-icons", - "version": "2.13.0", + "version": "2.13.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@cdransf/eleventy-plugin-tabler-icons", - "version": "2.13.0", + "version": "2.13.1", "license": "MIT", "devDependencies": { "@11ty/eleventy": "v3.0.0", diff --git a/package.json b/package.json index a0e6e50..94dede0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@cdransf/eleventy-plugin-tabler-icons", - "version": "2.13.0", + "version": "2.13.1", "description": "Shortcodes to add Tabler icons to your Eleventy projects", "type": "module", "main": "tablericons.js", diff --git a/tablericons.js b/tablericons.js index b959cba..4cb477e 100644 --- a/tablericons.js +++ b/tablericons.js @@ -1,48 +1,58 @@ -import ICONS from './icons.js' +import ICONS from './icons.js'; const tablericons = (eleventyConfig, config = {}) => { - const { className = '', errorOnMissing = false } = config + const { className = '', errorOnMissing = false } = config; - const renderIcon = (context = this, name, alt, attrs) => { - const contents = ICONS[name] + const renderIcon = (context = {}, name, alt = '', attrs = {}) => { + const contents = ICONS[name]; if (!contents) { - handleMissingIcon(name, context.page.inputPath) - return '' + handleMissingIcon(name, context.page?.inputPath); + return ''; } - return `${head(alt, className, name, attrs)}${contents}${ICONS.TAIL}` - } + return `${buildHead(alt, className, name, attrs)}${contents}${ICONS.TAIL}`; + }; const handleMissingIcon = (name, inputPath) => { - const message = `No tablericons found for name '${name}'` + const message = `No tablericon found for name '${name}'`; if (errorOnMissing) { - throw new Error(message) + throw new Error(message); } else { - console.warn(`${message} in ${inputPath}`) + console.warn(`${message}${inputPath ? ` in ${inputPath}` : ''}`); } - } + }; - // Register shortcode for Eleventy 3.0 - eleventyConfig.addShortcode('tablericon', (name, alt, attrs) => { - return renderIcon(this, name, alt, attrs) - }) -} + eleventyConfig.addShortcode('tablericon', function (name, alt, attrs) { + return renderIcon(this, name, alt, attrs); + }); +}; -const head = (alt, className, iconName, attrs) => { - let output = `${ICONS.HEAD.slice(0, -1)} aria-hidden='true'` +const buildHead = (alt = '', className = '', iconName, attrs = {}) => { + let output = `${ICONS.HEAD.slice(0, -1)} aria-hidden="true"`; - if (className) output += ` class='${className}'` - output += ` data-tablericon-name='${iconName}'` + if (className) output += ` class="${className}"`; + output += ` data-tablericon-name="${iconName}"`; if (typeof attrs === 'string') { - output += ` ${attrs}` + output += ` ${attrs}`; } else if (attrs && typeof attrs === 'object') { output += Object.entries(attrs) - .map(([property, value]) => (property && value ? ` ${property}='${value}'` : '')) - .join('') + .map(([key, val]) => (key && val ? ` ${key}="${val}"` : '')) + .join(''); } - return `${output}>` + return `${output}>`; +}; + +export function renderTablerIcon(name, alt = '', attrs = {}, options = {}) { + const context = { page: { inputPath: options.inputPath || '' } }; + const contents = ICONS[name]; + if (!contents) { + console.warn(`No tablericon found for name '${name}'${options.inputPath ? ` in ${options.inputPath}` : ''}`); + return ''; + } + + return `${buildHead(alt, options.className || '', name, attrs)}${contents}${ICONS.TAIL}`; } -export default tablericons \ No newline at end of file +export default tablericons;