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

46 lines
1.6 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/text-highlight.ts — 文本高亮(强调)语法的解析与样式。 */
import { Token, Tokens, Lexer, MarkedExtension } from "marked";
import { Extension } from "./extension";
const highlightRegex = /^==(.*?)==/;
export class TextHighlight extends Extension {
markedExtension(): MarkedExtension {
return {
extensions: [{
name: 'InlineHighlight',
level: 'inline',
start(src: string) {
let index;
let indexSrc = src;
while (indexSrc) {
index = indexSrc.indexOf('==');
if (index === -1) return;
return index;
}
},
tokenizer(src: string, tokens: Token[]) {
const match = src.match(highlightRegex);
if (match) {
return {
type: 'InlineHighlight',
raw: match[0],
text: match[1],
};
}
},
renderer(token: Tokens.Generic) {
const lexer = new Lexer();
const tokens = lexer.lex(token.text);
// TODO: 优化一下
let body = this.parser.parse(tokens)
body = body.replace('<p>', '')
body = body.replace('</p>', '')
return `<span class="note-highlight">${body}</span>`;
}
}]
};
}
}