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

64 lines
1.9 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/blockquote.ts — 区块引用blockquote语法处理与样式。 */
import { Tokens, MarkedExtension } from "marked";
import { Extension, MDRendererCallback } from "./extension";
import { NMPSettings } from "src/settings";
import { App, Vault } from "obsidian";
import AssetsManager from "../assets";
import { CalloutRenderer } from "./callouts";
import { WidgetBox } from "./widget-box";
export class Blockquote extends Extension {
callout: CalloutRenderer;
box: WidgetBox;
constructor(app: App, settings: NMPSettings, assetsManager: AssetsManager, callback: MDRendererCallback) {
super(app, settings, assetsManager, callback);
this.callout = new CalloutRenderer(app, settings, assetsManager, callback);
if (settings.isAuthKeyVaild()) {
this.box = new WidgetBox(app, settings, assetsManager, callback);
}
}
async prepare() {
if (!this.marked) {
console.error("marked is not ready");
return;
}
if (this.callout) this.callout.marked = this.marked;
if (this.box) this.box.marked = this.marked;
return;
}
async renderer(token: Tokens.Blockquote) {
if (this.callout.matched(token.text)) {
return await this.callout.renderer(token);
}
if (this.box && this.box.matched(token.text)) {
return await this.box.renderer(token);
}
const body = this.marked.parser(token.tokens);
return `<blockquote>${body}</blockquote>`;
}
markedExtension(): MarkedExtension {
return {
async: true,
walkTokens: async (token: Tokens.Generic) => {
if (token.type !== 'blockquote') {
return;
}
token.html = await this.renderer(token as Tokens.Blockquote);
},
extensions: [{
name: 'blockquote',
level: 'block',
renderer: (token: Tokens.Generic) => {
return token.html;
},
}]
}
}
}