Files
note2any/src/markdown/topic.ts
2025-10-08 09:18:20 +08:00

41 lines
1.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* 文件markdown/topic.ts — 话题/标签语法的解析与链接生成。 */
import { Tokens, MarkedExtension } from "marked";
import { Extension } from "./extension";
const topicRegex = /^#([^\s#]+)/;
export class Topic extends Extension {
markedExtension(): MarkedExtension {
return {
extensions: [{
name: 'Topic',
level: 'inline',
start(src: string) {
let index;
let indexSrc = src;
while (indexSrc) {
index = indexSrc.indexOf('#');
if (index === -1) return;
return index;
}
},
tokenizer(src: string) {
const match = src.match(topicRegex);
if (match) {
return {
type: 'Topic',
raw: match[0],
text: match[1],
};
}
},
renderer(token: Tokens.Generic) {
return `<a class="wx_topic_link" style="color: #576B95 !important;" data-topic="1">${'#' + token.text.trim()}</a>`;
}
},
]
}
}
}