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

49 lines
1.7 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/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 `<li>${href}&nbsp;↩</li>`;
});
return `${html}<seciton class="footnotes"><hr><ol>${links.join('')}</ol></section>`;
}
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 `<a href="${token.href}">${token.text}</a>`;
}
this.allLinks.push(token.href);
if (this.settings.linkStyle == 'footnote') {
return `<a>${token.text}<sup>[${this.allLinks.length}]</sup></a>`;
}
else {
return `<a>${token.text}[${token.href}]</a>`;
}
}
}]
}
}
}