Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
"use client";
import ReactMarkdown from "react-markdown";
import remarkGfm from "remark-gfm";
import type { Components } from "react-markdown";
import { MermaidDiagram } from "./mermaid-diagram";
import { useState, useEffect, useMemo } from "react";
import { Copy, Check } from "lucide-react";
import { cn } from "../lib/cn";
import {
buildAnchorIndex,
type WikiLinkKind,
type WikiLinkRef,
} from "./wiki-links-types";
import {
buildPathIndex,
elidePath,
resolvePath,
type PathIndex,
type PathIndexablePage,
} from "./path-index";
/**
* Resolves an inline backtick ref to an internal wiki page href, or null
* when the ref doesn't map to a known page (renders as a plain code span).
*/
type WikiLinkLookup = (anchor: string) => { href: string; kind: WikiLinkKind } | null;
type WikiLinkComponent = React.ElementType<{
href: string;
className?: string;
title?: string;
children: React.ReactNode;
}>;
function slugify(text: string): string {
return text
.toLowerCase()
.replace(/[^\w\s-]/g, "")
.replace(/\s+/g, "-");
}
function ClientCodeBlock({ code, language }: { code: string; language: string }) {
const [copied, setCopied] = useState(false);
const [html, setHtml] = useState<string | null>(null);
useEffect(() => {
let cancelled = false;
import("shiki")
.then(({ codeToHtml }) =>
// Dual themes: tokens carry --shiki-light/--shiki-dark CSS vars and
// globals.css picks one per theme (see ".shiki span" rules there).
codeToHtml(code, {
lang: language as never,
themes: { light: "github-light", dark: "vesper" },
defaultColor: false,
}),
)
.then((result) => {
if (!cancelled) setHtml(result);
})
.catch(() => {});
return () => {
cancelled = true;
};
}, [code, language]);
async function copy() {
await navigator.clipboard.writeText(code);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
}
return (
<div className="group relative my-4 rounded-lg border border-[var(--color-border-default)] overflow-hidden">
<div className="flex items-center justify-between px-4 py-1.5 bg-[var(--color-bg-elevated)] border-b border-[var(--color-border-default)]">
{language && (
<span className="text-xs font-mono text-[var(--color-text-tertiary)]">
{language}
</span>
)}
<button
onClick={copy}
className={cn(
"ml-auto flex items-center gap-1 text-xs text-[var(--color-text-tertiary)]",
"hover:text-[var(--color-text-secondary)] transition-colors",
)}
aria-label="Copy code"
>
{copied ? (
<>
<Check className="h-3.5 w-3.5" />
Copied
</>
) : (
<>
<Copy className="h-3.5 w-3.5" />
Copy
</>
)}
</button>
</div>
{html ? (
<div
className="overflow-x-auto text-sm [&>pre]:p-4 [&>pre]:m-0 [&>pre]:bg-transparent!"
dangerouslySetInnerHTML={{ __html: html /* shiki output; repowise-security-ignore: unsafe_inner_html */ }}
/>
) : (
<pre className="overflow-x-auto p-4 bg-[var(--color-bg-inset)]">
<code className="text-xs font-mono text-[var(--color-text-primary)]">
{code}
</code>
</pre>
)}
</div>
);
}
function buildComponents(
resolveLink: WikiLinkLookup | null,
LinkComponent: WikiLinkComponent,
pathIndex: PathIndex,
buildPathHref: ((pageId: string) => string) | undefined,
): Components {
const Link = LinkComponent;
return {
h1: ({ children }) => {
const text = typeof children === "string" ? children : extractText(children);
const id = slugify(text);
return (
<h1 id={id} className="mt-10 mb-4 font-serif text-3xl font-semibold tracking-tight text-[var(--color-text-primary)] first:mt-0 scroll-mt-16">
{children}
</h1>
);
},
h2: ({ children }) => {
const text = typeof children === "string" ? children : extractText(children);
const id = slugify(text);
return (
<h2 id={id} className="mt-9 mb-3 font-serif text-2xl font-semibold tracking-tight text-[var(--color-text-primary)] scroll-mt-16">
{children}
</h2>
);
},
h3: ({ children }) => {
const text = typeof children === "string" ? children : extractText(children);
const id = slugify(text);
return (
<h3 id={id} className="mt-7 mb-2 font-serif text-xl font-semibold text-[var(--color-text-primary)] scroll-mt-16">
{children}
</h3>
);
},
p: ({ children }) => (
<p className="mb-4 text-base leading-[1.75] text-[var(--color-text-secondary)]">
{children}
</p>
),
ul: ({ children }) => (
<ul className="mb-4 ml-4 space-y-1.5 list-disc text-base text-[var(--color-text-secondary)]">
{children}
</ul>
),
ol: ({ children }) => (
<ol className="mb-4 ml-4 space-y-1.5 list-decimal text-base text-[var(--color-text-secondary)]">
{children}
</ol>
),
li: ({ children }) => <li className="leading-7">{children}</li>,
code: ({ className, children, ...props }) => {
const langMatch = className?.match(/language-(\w+)/);
const lang = langMatch?.[1];
const isBlock = !!lang;
if (isBlock) {
const code = typeof children === "string" ? children : String(children ?? "");
const trimmed = code.replace(/\n$/, "");
if (lang === "mermaid") {
return <MermaidDiagram chart={trimmed} securityLevel="loose" />;
}
return <ClientCodeBlock code={trimmed} language={lang} />;
}
// Inline code. Two states, and they have to look different:
//
// live — resolves to a page in this wiki. A faint ground, primary ink,
// accent only on hover.
// dead — nothing behind it. Plain mono in tertiary ink, no ground.
//
// Previously both rendered in full-saturation accent, which turned a list
// of file paths into a wall of orange and made the clickable ones no more
// clickable-looking than the rest. Decorating only what you can follow
// inverts that: the loud thing on the page is the thing that does
// something.
const text = typeof children === "string" ? children : extractText(children);
const anchor = text.trim();
// The interlinker's own resolutions win — it can resolve symbols and page
// titles, which the path index deliberately does not attempt.
const linked = resolveLink ? resolveLink(anchor) : null;
const viaPath = linked ? null : resolvePath(pathIndex, anchor);
const href = linked?.href ?? (viaPath ? buildPathHref?.(viaPath.pageId) : undefined);
if (href) {
const full = viaPath?.path ?? anchor;
const { head, tail } = elidePath(full);
return (
<Link
href={href}
title={full}
className="rounded bg-[color-mix(in_srgb,var(--color-text-primary)_6%,transparent)] px-1.5 py-0.5 text-[0.85em] font-mono whitespace-nowrap text-[var(--color-text-primary)] transition-colors hover:bg-[var(--color-accent-muted)] hover:text-[var(--color-accent-primary)] hover:underline hover:underline-offset-2"
>
{head ? (
<>
<span className="opacity-55">{head}</span>
{tail}
</>
) : (
children
)}
</Link>
);
}
return (
<code
className="text-[0.85em] font-mono text-[var(--color-text-tertiary)]"
{...props}
>
{children}
</code>
);
},
pre: ({ children }) => <>{children}</>,
blockquote: ({ children }) => (
<blockquote
className="my-4 border-l-2 border-[var(--color-text-tertiary)] pl-4 text-sm italic text-[var(--color-text-tertiary)]"
>
{children}
</blockquote>
),
table: ({ children }) => (
<div className="my-4 overflow-x-auto overflow-hidden border border-[var(--color-border-default)]">
<table className="w-full text-sm">{children}</table>
</div>
),
thead: ({ children }) => (
<thead className="bg-[var(--color-bg-surface)] border-b border-[var(--color-border-default)]">{children}</thead>
),
th: ({ children }) => (
<th className="px-3 py-2.5 text-left text-[11px] font-medium uppercase tracking-wider text-[var(--color-text-tertiary)]">
{children}
</th>
),
tr: ({ children }) => (
<tr className="border-t border-[var(--color-table-divider)] transition-colors hover:bg-[var(--color-bg-elevated)]">
{children}
</tr>
),
td: ({ children }) => (
<td className="px-3 py-2.5 text-[var(--color-text-secondary)]">
{children}
</td>
),
hr: () => <hr className="my-6 border-[var(--color-border-default)]" />,
a: ({ href, children }) => (
<a
href={href}
className="text-[var(--color-accent-primary)] underline underline-offset-2 hover:opacity-80 transition-opacity"
target="_blank"
rel="noopener noreferrer"
>
{children}
</a>
),
strong: ({ children }) => (
<strong className="font-semibold text-[var(--color-text-primary)]">
{children}
</strong>
),
};
}
function extractText(node: unknown): string {
if (typeof node === "string") return node;
if (Array.isArray(node)) return node.map(extractText).join("");
if (node && typeof node === "object" && "props" in node) {
const props = (node as { props?: { children?: unknown } }).props;
return extractText(props?.children);
}
return "";
}
interface WikiMarkdownProps {
content: string;
/**
* Resolved forward links for this page (``page.metadata.wiki_links``).
* When provided alongside ``buildHref``, inline backtick refs that match
* a link anchor render as clickable internal links.
*/
wikiLinks?: WikiLinkRef[];
/** Maps a resolved ``{ pageId, kind }`` to an in-app href. */
buildHref?: (pageId: string, kind: WikiLinkKind) => string;
/** Router-aware link (e.g. Next.js ``Link``). Defaults to a plain ``<a>``. */
LinkComponent?: WikiLinkComponent;
/**
* The loaded page list, used to resolve inline path refs the backend's
* interlinking pass left unresolved. Omit it and those refs simply render
* as plain code spans, exactly as before.
*/
pages?: readonly PathIndexablePage[];
}
export function WikiMarkdown({
content,
wikiLinks,
buildHref,
LinkComponent = "a",
pages,
}: WikiMarkdownProps) {
// Keyed on the page list identity: the reader loads it once per repo, so
// this runs once rather than on every content change.
const pathIndex = useMemo(() => buildPathIndex(pages ?? []), [pages]);
const components = useMemo(() => {
let resolveLink: WikiLinkLookup | null = null;
if (wikiLinks && wikiLinks.length > 0 && buildHref) {
const index = buildAnchorIndex(wikiLinks);
resolveLink = (anchor) => {
const hit = index.get(anchor);
if (!hit) return null;
return { href: buildHref(hit.pageId, hit.kind), kind: hit.kind };
};
}
// Path hits are always file-kind by construction.
const buildPathHref = buildHref
? (pageId: string) => buildHref(pageId, "file")
: undefined;
return buildComponents(resolveLink, LinkComponent, pathIndex, buildPathHref);
}, [wikiLinks, buildHref, LinkComponent, pathIndex]);
// Parsing + reconciling a large markdown document is the dominant cost when a
// page opens. Memoize the rendered tree on (content, components) so unrelated
// parent re-renders — sidebar toggles, hover state, etc. — don't re-parse it.
return useMemo(
() => (
<ReactMarkdown remarkPlugins={[remarkGfm]} components={components}>
{content}
</ReactMarkdown>
),
[content, components],
);
}