mirror of
https://git.zhongyuan.online/ytke/tap.git
synced 2026-11-24 22:15:18 -08:00
21 lines
1.1 KiB
TypeScript
21 lines
1.1 KiB
TypeScript
'use client';
|
|||
|
|
|
||
|
|
import Markdown from 'react-markdown';
|
||
|
|
import remarkGfm from 'remark-gfm';
|
||
|
|
import { linkedDocument, type DocumentId } from '@/lib/documents';
|
||
|
|
|
||
|
|
export type TemplateDocumentProps = { text: string; onOpenDocument: (id: DocumentId) => void };
|
||
|
|
|
||
|
|
export default function TemplateDocument({ text, onOpenDocument }: TemplateDocumentProps) {
|
||
|
|
return <article className="template-document"><Markdown remarkPlugins={[remarkGfm]} skipHtml components={{
|
||
|
|
a: ({ href, children }) => {
|
||
|
|
const related = linkedDocument(href || '');
|
||
|
|
if (related) return <button type="button" className="template-inline-link" onClick={() => onOpenDocument(related.id)}>{children}</button>;
|
||
|
|
if (!href || !/^https?:\/\//i.test(href)) return <span>{children}</span>;
|
||
|
|
return <a href={href} target="_blank" rel="noopener noreferrer">{children}</a>;
|
||
|
|
},
|
||
|
|
img: ({ alt }) => <span>{alt ? `图片:${alt}` : '图片请查看原文档'}</span>,
|
||
|
|
pre: ({ children }) => <div className="template-text-block">{children}</div>,
|
||
|
|
table: ({ children }) => <div className="template-table-wrap"><table>{children}</table></div>,
|
||
|
|
}}>{text}</Markdown></article>;
|
||
|
|
}
|