feat(frontend): Markdown 支持 GFM、代码高亮与图片灯箱
统一 SSR/客户端渲染路径,评论与预览共用增强组件。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
38
frontend/components/markdown/MdLink.tsx
Normal file
38
frontend/components/markdown/MdLink.tsx
Normal file
@@ -0,0 +1,38 @@
|
||||
"use client";
|
||||
|
||||
import type { AnchorHTMLAttributes } from "react";
|
||||
|
||||
type MdLinkProps = AnchorHTMLAttributes<HTMLAnchorElement>;
|
||||
|
||||
function isExternalHref(href?: string): boolean {
|
||||
if (!href) return false;
|
||||
if (href.startsWith("/") || href.startsWith("#") || href.startsWith("?")) {
|
||||
return false;
|
||||
}
|
||||
if (href.startsWith("mailto:") || href.startsWith("tel:")) return false;
|
||||
try {
|
||||
if (typeof window !== "undefined") {
|
||||
const url = new URL(href, window.location.origin);
|
||||
return url.origin !== window.location.origin;
|
||||
}
|
||||
} catch {
|
||||
/* 相对或非法 URL 当站内 */
|
||||
}
|
||||
return /^https?:\/\//i.test(href);
|
||||
}
|
||||
|
||||
/** Markdown 链接:外链新开页 */
|
||||
export default function MdLink({ href, children, ...rest }: MdLinkProps) {
|
||||
const external = isExternalHref(href);
|
||||
return (
|
||||
<a
|
||||
{...rest}
|
||||
href={href}
|
||||
{...(external
|
||||
? { target: "_blank", rel: "noopener noreferrer" }
|
||||
: {})}
|
||||
>
|
||||
{children}
|
||||
</a>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user