import type { ReactNode } from "react";
import type { Inline } from "../inline.js";
/**
* Href guard for rendered links. Blocks contained in a database (the Cat, a
* composer, an admin form) flow through the same renderer as committed files,
* so `javascript:` and friends must never become a clickable href — the link
* degrades to a plain span instead.
*/
const KNOWN_SAFE_SCHEME = /^(https?:|mailto:|tel:)/i;
const HAS_SCHEME = /^[a-z][a-z0-9+.-]*:/i;
export function safeHref(href: string): string | null {
const trimmed = href.trim();
if (KNOWN_SAFE_SCHEME.test(trimmed)) return trimmed;
if (!HAS_SCHEME.test(trimmed)) return trimmed; // relative, absolute path, #anchor
return null;
}
/** Render inline spans to React nodes (semantic bp-* classes only). */
export function renderInline(spans: Inline[]): ReactNode {
return spans.map((span, i) => {
switch (span.t) {
case "text":
return span.text;
case "strong":
return {renderInline(span.children)};
case "em":
return {renderInline(span.children)};
case "code":
return (
{span.text}
);
case "link": {
const href = safeHref(span.href);
if (href === null) return {renderInline(span.children)};
const external = /^https?:/i.test(href);
return (
{renderInline(span.children)}
);
}
case "footnoteRef":
return (
{span.id}
);
}
});
}