This commit is contained in:
董宏凯 committed 2026-06-10 19:08:50 +08:00
commit 406c02ad07
122 files changed
+17849 -1694

No files matched your search

@@ -16,12 +16,33 @@ import tsx from 'react-syntax-highlighter/dist/esm/languages/prism/tsx';
import typescript from 'react-syntax-highlighter/dist/esm/languages/prism/typescript';
import { oneDark } from 'react-syntax-highlighter/dist/esm/styles/prism';
export interface WikiLinkTarget {
target: string;
kind: 'note' | 'vault' | 'missing';
exists: boolean;
note_id?: string;
vault_path?: string;
title?: string;
}
export type WikiLinkMap = Record<string, WikiLinkTarget>;
interface MarkdownRendererProps {
content: string;
className?: string;
onCitationClick?: (index: number) => void;
/** Resolved `[[wikilink]]` targets; when provided, non-numeric `[[...]]` render as links. */
wikiLinks?: WikiLinkMap;
onWikiLinkClick?: (link: WikiLinkTarget) => void;
}
const escapeHtmlAttr = (value: string): string =>
value
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;');
SyntaxHighlighter.registerLanguage('bash', bash);
SyntaxHighlighter.registerLanguage('css', css);
SyntaxHighlighter.registerLanguage('html', markup);
@@ -126,8 +147,48 @@ const CodeBlock: React.FC<{ children: React.ReactNode; className?: string }> = (
);
};
const MarkdownRenderer: React.FC<MarkdownRendererProps> = ({ content, className = '', onCitationClick }) => {
const contentWithCitationBadge = content.replace(
const MarkdownRenderer: React.FC<MarkdownRendererProps> = ({
content,
className = '',
onCitationClick,
wikiLinks,
onWikiLinkClick,
}) => {
// 1) Non-numeric [[wikilinks]] → clickable (or greyed-out) links, driven by the
// resolver map. Numeric [[123]] are left untouched here for the citation pass.
let processed = content;
if (wikiLinks) {
processed = processed.replace(
/\[\[([^\]\n|]+)(?:\|([^\]\n]+))?\]\]/g,
(match, rawTarget: string, alias?: string) => {
const target = String(rawTarget).trim();
if (/^\d+$/.test(target)) return match; // citation badge, handled below
const resolved = wikiLinks[target];
const label = (alias?.trim() || resolved?.title || target);
const safeLabel = escapeHtmlAttr(label);
const safeTarget = escapeHtmlAttr(target);
if (resolved && resolved.exists) {
return `<button type="button" data-wikilink-target="${safeTarget}" class="inline items-center font-medium text-blue-600 dark:text-blue-400 hover:underline cursor-pointer align-baseline">${safeLabel}</button>`;
}
return `<span data-wikilink-missing="${safeTarget}" title="页面不存在" class="text-muted-foreground underline decoration-dashed decoration-from-font cursor-not-allowed">${safeLabel}</span>`;
},
);
}
// 2) wiki-capture provenance markers ^[inferred] / ^[ambiguous] → small badges
// (otherwise they render as raw text). They mark model-inferred / uncertain
// statements per the wiki-capture spec.
processed = processed
.replace(
/\s*\^\[inferred\]/g,
' <sup title="模型推断,非原文直接得出" class="ml-0.5 rounded bg-amber-50 px-1 text-[10px] font-medium text-amber-600 align-super dark:bg-amber-500/15 dark:text-amber-300">推测</sup>',
)
.replace(
/\s*\^\[ambiguous\]/g,
' <sup title="不确定 / 存在冲突" class="ml-0.5 rounded bg-rose-50 px-1 text-[10px] font-medium text-rose-600 align-super dark:bg-rose-500/15 dark:text-rose-300">存疑</sup>',
);
// 3) Numeric [[123]] citation badges.
const contentWithCitationBadge = processed.replace(
/\[\[(\d+)\]\]/g,
'<button type="button" data-citation-index="$1" class="inline-flex items-center justify-center min-w-[1.5rem] h-6 px-2 mx-0.5 rounded-md border text-xs font-semibold align-middle bg-red-50 text-red-600 border-red-200 dark:bg-slate-700 dark:text-slate-100 dark:border-slate-500 cursor-pointer transition-all hover:opacity-85">[$1]</button>',
);
@@ -136,8 +197,17 @@ const MarkdownRenderer: React.FC<MarkdownRendererProps> = ({ content, className
<div
className={`markdown-content ${className}`}
onClick={(event) => {
if (!onCitationClick) return;
const target = event.target as HTMLElement;
const wikiButton = target.closest('[data-wikilink-target]') as HTMLElement | null;
if (wikiButton && onWikiLinkClick && wikiLinks) {
const key = wikiButton.dataset.wikilinkTarget || '';
const link = wikiLinks[key];
if (link) {
onWikiLinkClick(link);
return;
}
}
if (!onCitationClick) return;
const button = target.closest('[data-citation-index]') as HTMLElement | null;
if (!button) return;
const index = Number(button.dataset.citationIndex || '0');