refactor: add support for non-png icons

This commit is contained in:
hyper
2026-04-06 18:31:08 +08:00
parent 1eaef04215
commit 4d1c40ed3e
2 changed files with 39 additions and 3 deletions
+23 -3
View File
@@ -36,11 +36,31 @@ const marked = new Marked()
{
level: 'inline',
marker: ':',
// :[blah] becomes <i class="icon icon-blah"></i>
// :[icon] 或 :[icon.ext] 语法
// 支持的扩展名: .svg, .png, .gif, .jpg, .jpeg, .webp
// 如果不指定扩展名,默认为 .png
renderer(token) {
if (!token.meta.name) {
const style = globalIconPrefix ? `style="--icon-src: url('${globalIconPrefix}/${token.text}.png')"` : '';
return `<icon ${style} class="icon-${token.text} ${token.attrs?.class || ""}"></icon>`;
const iconText = token.text || '';
// 已知支持的图片扩展名
const supportedExtensions = ['svg', 'png', 'gif', 'jpg', 'jpeg', 'webp'];
// 检查是否包含扩展名(查找最后一个点)
let iconName = iconText;
let extension = 'png'; // 默认扩展名
const lastDotIndex = iconName.lastIndexOf('.');
if (lastDotIndex > 0) {
const potentialExt = iconName.slice(lastDotIndex + 1).toLowerCase();
if (supportedExtensions.includes(potentialExt)) {
extension = potentialExt;
iconName = iconName.slice(0, lastDotIndex);
}
}
const style = globalIconPrefix ? `style="--icon-src: url('${globalIconPrefix}/${iconName}.${extension}')"` : '';
return `<icon ${style} class="icon-${iconName} ${token.attrs?.class || ""}"></icon>`;
}
return false;
}