35 lines
817 B
TypeScript
35 lines
817 B
TypeScript
import MarkdownIt from "markdown-it";
|
|
|
|
const md = new MarkdownIt({
|
|
html: true,
|
|
linkify: true,
|
|
typographer: true,
|
|
breaks: true,
|
|
});
|
|
|
|
export function renderMarkdown(content: string): string {
|
|
const dirtyHtml = md.render(content);
|
|
|
|
if (typeof window !== "undefined") {
|
|
import("dompurify").then((DOMPurify) => {
|
|
return DOMPurify.default.sanitize(dirtyHtml);
|
|
});
|
|
}
|
|
|
|
return dirtyHtml;
|
|
}
|
|
|
|
export function convertMedia(content: string): string {
|
|
// 通过正则表达式替换Markdown中的图片链接
|
|
//  -> )
|
|
|
|
if (!content) return "";
|
|
|
|
const contentWithAbsoluteUrls = content.replace(
|
|
/!\[([^\]]*)\]\((\/uploads\/[^)]+)\)/g,
|
|
(_, alt, url) => `})`
|
|
);
|
|
|
|
return contentWithAbsoluteUrls;
|
|
}
|