refactor: add support for non-png icons
This commit is contained in:
+23
-3
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user