Add the V2 web app: design system, shell, study flows, AI, settings, sharing
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
parent
eae109c166
commit
74a98ac1f1
52 changed files with 11976 additions and 1 deletions
109
web/src/ui/Markdown.tsx
Normal file
109
web/src/ui/Markdown.tsx
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
import { memo, type ReactNode } from 'react';
|
||||
import ReactMarkdown, { type Components } from 'react-markdown';
|
||||
import remarkGfm from 'remark-gfm';
|
||||
import { cx } from '@/lib/util';
|
||||
|
||||
export interface Citation {
|
||||
ref: string;
|
||||
label: string;
|
||||
documentId: string;
|
||||
chunkId: number;
|
||||
}
|
||||
|
||||
const BASE: Components = {
|
||||
a: ({ href, children }) => (
|
||||
<a href={href} target={href?.startsWith('/') ? undefined : '_blank'} rel="noreferrer noopener">
|
||||
{children}
|
||||
</a>
|
||||
),
|
||||
table: ({ children }) => (
|
||||
<div className="table-wrap">
|
||||
<table>{children}</table>
|
||||
</div>
|
||||
),
|
||||
img: ({ src, alt }) => <img src={typeof src === 'string' ? src : undefined} alt={alt ?? ''} loading="lazy" />,
|
||||
};
|
||||
|
||||
/** Splits text on [c12] / [c12, c13] markers and swaps them for numbered citation chips. */
|
||||
function withCitations(children: ReactNode, cites: Map<string, { n: number; cite: Citation }>, onCite?: (c: Citation) => void): ReactNode {
|
||||
const walk = (node: ReactNode, key: string): ReactNode => {
|
||||
if (typeof node === 'string') {
|
||||
const parts = node.split(/(\[c\d+(?:\s*,\s*c\d+)*\])/g);
|
||||
if (parts.length === 1) return node;
|
||||
return parts.map((part, i) => {
|
||||
if (!/^\[c\d/.test(part)) return part;
|
||||
const refs = part.slice(1, -1).split(',').map((r) => r.trim());
|
||||
return (
|
||||
<span key={`${key}-${i}`} className="whitespace-nowrap">
|
||||
{refs.map((ref) => {
|
||||
const hit = cites.get(ref);
|
||||
if (!hit) return null;
|
||||
return (
|
||||
<button
|
||||
key={ref}
|
||||
type="button"
|
||||
title={hit.cite.label}
|
||||
onClick={() => onCite?.(hit.cite)}
|
||||
className="num mx-0.5 inline-grid h-[18px] min-w-[18px] -translate-y-px place-items-center rounded-[5px] bg-accent-soft px-1 align-middle text-[10.5px] font-semibold text-accent hover:bg-accent hover:text-accent-ink"
|
||||
>
|
||||
{hit.n}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</span>
|
||||
);
|
||||
});
|
||||
}
|
||||
if (Array.isArray(node)) return node.map((n, i) => walk(n, `${key}.${i}`));
|
||||
return node;
|
||||
};
|
||||
return walk(children, 'c');
|
||||
}
|
||||
|
||||
export const Markdown = memo(function Markdown({
|
||||
children,
|
||||
className,
|
||||
citations,
|
||||
onCite,
|
||||
serif,
|
||||
}: {
|
||||
children: string;
|
||||
className?: string;
|
||||
citations?: Citation[];
|
||||
onCite?: (c: Citation) => void;
|
||||
/** Use the reading serif (questions, cards, notes). */
|
||||
serif?: boolean;
|
||||
}) {
|
||||
let components = BASE;
|
||||
if (citations && citations.length > 0) {
|
||||
// Number citations in order of first appearance in the text.
|
||||
const order = new Map<string, { n: number; cite: Citation }>();
|
||||
const byRef = new Map(citations.map((c) => [c.ref, c]));
|
||||
for (const m of children.matchAll(/c\d+/g)) {
|
||||
const cite = byRef.get(m[0]);
|
||||
if (cite && !order.has(m[0])) order.set(m[0], { n: order.size + 1, cite });
|
||||
}
|
||||
const wrap =
|
||||
(Tag: 'p' | 'li' | 'td' | 'strong' | 'em' | 'h1' | 'h2' | 'h3' | 'h4' | 'blockquote') =>
|
||||
({ children: kids }: { children?: ReactNode }) => <Tag>{withCitations(kids, order, onCite)}</Tag>;
|
||||
components = { ...BASE, p: wrap('p'), li: wrap('li'), td: wrap('td'), strong: wrap('strong'), em: wrap('em'), h1: wrap('h1'), h2: wrap('h2'), h3: wrap('h3'), h4: wrap('h4'), blockquote: wrap('blockquote') };
|
||||
}
|
||||
return (
|
||||
<div className={cx('md', serif && 'reading', className)}>
|
||||
<ReactMarkdown remarkPlugins={[remarkGfm]} components={components}>
|
||||
{children}
|
||||
</ReactMarkdown>
|
||||
</div>
|
||||
);
|
||||
});
|
||||
|
||||
/** Ordered list of the sources actually cited in a piece of text. */
|
||||
export function citedSources(text: string, citations: Citation[]): { n: number; cite: Citation }[] {
|
||||
const byRef = new Map(citations.map((c) => [c.ref, c]));
|
||||
const out = new Map<string, { n: number; cite: Citation }>();
|
||||
for (const m of text.matchAll(/c\d+/g)) {
|
||||
const cite = byRef.get(m[0]);
|
||||
if (cite && !out.has(m[0])) out.set(m[0], { n: out.size + 1, cite });
|
||||
}
|
||||
return [...out.values()];
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue