markdown2html/src/icon.ts
2023-04-29 16:01:55 -07:00

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}" -->`;
}
};
}