mirror of
https://gitlab.com/OpenAlt/OpenAltRO/openalt_ro.git
synced 2026-11-23 15:19:15 +00:00
52 lines
1.7 KiB
JavaScript
52 lines
1.7 KiB
JavaScript
'use strict';
|
|
|
|
// Inline SVG icons at build time.
|
|
// Templates: <%- icon('si:gitlab') %> / <%- icon('lucide:map-pin') %>
|
|
// Markdown: {% icon lucide:download %}
|
|
// si: Simple Icons (brand logos) https://simpleicons.org
|
|
// lucide: Lucide (general icons) https://lucide.dev
|
|
// custom: themes/openalt/icons/*.svg (logos missing from the above, e.g. LinkedIn)
|
|
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const sets = {
|
|
si: path.join(hexo.base_dir, 'node_modules/simple-icons/icons'),
|
|
lucide: path.join(hexo.base_dir, 'node_modules/lucide-static/icons'),
|
|
custom: path.join(hexo.theme_dir, 'icons')
|
|
};
|
|
|
|
const cache = new Map();
|
|
|
|
function icon(name) {
|
|
if (!name) return '';
|
|
if (cache.has(name)) return cache.get(name);
|
|
|
|
const [set, id] = name.split(':');
|
|
const dir = sets[set];
|
|
const file = dir && id && path.join(dir, id + '.svg');
|
|
|
|
if (!file || !fs.existsSync(file)) {
|
|
hexo.log.warn('icon: unknown icon "%s"', name);
|
|
cache.set(name, '');
|
|
return '';
|
|
}
|
|
|
|
const svg = fs.readFileSync(file, 'utf8')
|
|
.replace(/<!--[\s\S]*?-->/g, '')
|
|
.replace(/<title>[\s\S]*?<\/title>/, '')
|
|
.replace(/\s+/g, ' ')
|
|
.trim()
|
|
// Drop the root's own sizing/class/role; CSS sizes icons via .icon
|
|
.replace(/<svg([^>]*)>/, function (m, attrs) {
|
|
attrs = attrs.replace(/\s(width|height|class|role)="[^"]*"/g, '');
|
|
const fill = set === 'lucide' ? '' : ' fill="currentColor"';
|
|
return '<svg' + attrs + fill + ' class="icon icon-' + set + '" aria-hidden="true">';
|
|
});
|
|
|
|
cache.set(name, svg);
|
|
return svg;
|
|
}
|
|
|
|
hexo.extend.helper.register('icon', icon);
|
|
hexo.extend.tag.register('icon', function (args) { return icon(args[0]); });
|