/* 文件:markdown/link.ts — 处理行内与外部链接的解析与转义规则。 */ import { Tokens, MarkedExtension } from "marked"; import { Extension } from "./extension"; export class LinkRenderer extends Extension { allLinks:string[] = []; async prepare() { this.allLinks = []; } async postprocess(html: string) { if (this.settings.linkStyle !== 'footnote' || this.allLinks.length == 0) { return html; } const links = this.allLinks.map((href, i) => { return `
  • ${href} ↩
  • `; }); return `${html}
      ${links.join('')}
    `; } markedExtension(): MarkedExtension { return { extensions: [{ name: 'link', level: 'inline', renderer: (token: Tokens.Link) => { if (token.href.startsWith('mailto:')) { return token.text; } if (token.text.indexOf(token.href) === 0 || (token.href.indexOf('https://mp.weixin.qq.com/mp') === 0) || (token.href.indexOf('https://mp.weixin.qq.com/s') === 0)) { return `${token.text}`; } this.allLinks.push(token.href); if (this.settings.linkStyle == 'footnote') { return `${token.text}[${this.allLinks.length}]`; } else { return `${token.text}[${token.href}]`; } } }] } } }