33 lines
788 B
TypeScript
33 lines
788 B
TypeScript
|
|
import type { marked } from 'marked';
|
|
import { icons } from './icons';
|
|
import { MarkdownOptions } from './render';
|
|
|
|
export interface IconToken extends marked.Tokens.Generic {
|
|
text: string;
|
|
}
|
|
|
|
export function icon_ext(renderer: marked.Renderer, opts: MarkdownOptions) : marked.TokenizerExtension & marked.RendererExtension {
|
|
return {
|
|
name: 'icon',
|
|
level: 'inline',
|
|
start: (src) => src.match(/\{:/)?.index,
|
|
tokenizer(src, tokens) {
|
|
const rule = /^\{:([a-zA-Z0-9-]+):\}/;
|
|
const match = rule.exec(src);
|
|
|
|
if (match) {
|
|
return {
|
|
type: 'icon',
|
|
raw: match[0],
|
|
text: match[1],
|
|
tokens: this.lexer.inlineTokens(match[1], [ ])
|
|
};
|
|
}
|
|
},
|
|
renderer(token: IconToken) {
|
|
return icons[token.text] || `<!-- unknown icon "${token.text}" -->`;
|
|
}
|
|
};
|
|
}
|