feat: md icon prefix
This commit is contained in:
+14
-3
@@ -5,6 +5,15 @@ import markedAlert from "marked-alert";
|
||||
import markedMermaid from "./mermaid";
|
||||
import {gfmHeadingId} from "marked-gfm-heading-id";
|
||||
|
||||
let globalIconPrefix: string | undefined = undefined;
|
||||
function overrideIconPrefix(path?: string){
|
||||
globalIconPrefix = path;
|
||||
return {
|
||||
[Symbol.dispose](){
|
||||
globalIconPrefix = undefined;
|
||||
}
|
||||
}
|
||||
}
|
||||
// 使用 marked-directive 来支持指令语法
|
||||
const marked = new Marked()
|
||||
.use(gfmHeadingId())
|
||||
@@ -26,7 +35,8 @@ const marked = new Marked()
|
||||
// :[blah] becomes <i class="icon icon-blah"></i>
|
||||
renderer(token) {
|
||||
if (!token.meta.name) {
|
||||
return `<icon class="icon-${token.text}"></icon>`;
|
||||
const style = globalIconPrefix ? `style="--icon-src: url('${globalIconPrefix}${token.text}.png')"` : '';
|
||||
return `<icon ${style} class="icon-${token.text}"></icon>`;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -89,8 +99,9 @@ const marked = new Marked()
|
||||
}]
|
||||
});
|
||||
|
||||
export function parseMarkdown(content: string): string {
|
||||
return marked.parse(content.trimStart()) as string;
|
||||
export function parseMarkdown(content: string, iconPrefix?: string): string {
|
||||
using prefix = overrideIconPrefix(iconPrefix);
|
||||
return marked.parse(content.trimStart()) as string;
|
||||
}
|
||||
|
||||
export { marked };
|
||||
|
||||
Reference in New Issue
Block a user