ttrpg-tools/src/markdown/index.ts

33 lines
804 B
TypeScript
Raw Normal View History

2026-02-26 00:17:23 +08:00
import { Marked } from 'marked';
2026-02-27 17:54:09 +08:00
import {createDirectives, presetDirectiveConfigs} from 'marked-directive';
2026-02-26 00:17:23 +08:00
2026-02-26 00:47:26 +08:00
// 使用 marked-directive 来支持指令语法
2026-02-27 17:54:09 +08:00
const marked = new Marked().use(createDirectives([
...presetDirectiveConfigs,
{
marker: '::::',
level: 'container'
},
{
marker: ':::::',
level: 'container'
},
{
level: 'inline',
marker: ':',
// :[blah] becomes <i class="icon icon-blah"></i>
renderer(token) {
if (!token.meta.name) {
return `<icon class="icon-${token.text}"></icon>`;
}
return false;
}
},
]));
2026-02-26 00:17:23 +08:00
export function parseMarkdown(content: string): string {
2026-02-26 16:35:57 +08:00
return marked.parse(content.trimStart()) as string;
2026-02-26 00:17:23 +08:00
}
export { marked };