41 lines
1.0 KiB
TypeScript
41 lines
1.0 KiB
TypeScript
/* 文件: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>`;
|
||
}
|
||
},
|
||
]
|
||
}
|
||
}
|
||
} |