Files
jinshen-website/app/utils/markdown.ts
R2m1liA 92c5a3baab style: 调整代码格式
- 根据ESLint文件规范格式化app文件夹中的代码
2025-09-15 17:02:04 +08:00

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中的图片链接
// ![alt text](image-url) -> ![alt text](strapiMedia(image-url))
if (!content) return "";
const contentWithAbsoluteUrls = content.replace(
/!\[([^\]]*)\]\((\/uploads\/[^)]+)\)/g,
(_, alt, url) => `![${alt}](${useStrapiMedia(url)})`
);
return contentWithAbsoluteUrls;
}