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