/* ═══════════════════════════════════════════════════════════════ INLINE TEXT The little formatting a content block's text may carry, typed in the admin as plain characters: **bold** *italic* [link text](url) a line break keeps its place a blank line starts a new paragraph Parsed into React elements, never HTML, so nothing typed can inject markup. A link only becomes one when its URL is a site path (/history, #section), https/http, mailto: or tel:; anything else (javascript:, data:) shows as the plain text it was. `Paragraphs` is for block-level text (paragraph, callout): one
per blank-line-separated part. `Inline` is for text that sits
inside something else (a quote, a list item).
═══════════════════════════════════════════════════════════════ */
import { Fragment, type CSSProperties, type ReactNode } from 'react'
import { Link } from 'react-router-dom'
const TOKEN = /\*\*(.+?)\*\*|\*(.+?)\*|\[([^\]]+)\]\(([^)\s]+)\)/g
const SAFE_URL = /^(\/(?!\/)|#|https?:\/\/|mailto:|tel:)/i
/** External if it has a scheme or starts with //; otherwise it's
* one of our own routes and goes through the router. */
export const isExternalUrl = (url: string) => /^([a-z][a-z0-9+.-]*:|\/\/)/i.test(url)
export function Anchor({
href,
children,
className,
style,
}: {
href: string
children: ReactNode
className?: string
style?: CSSProperties
}) {
if (isExternalUrl(href)) {
const app = /^(mailto|tel):/i.test(href)
return (
{children}
)
}
if (href.startsWith('#')) {
return (
{children}
)
}
return (
{children}
)
}
function line(text: string, accent: string, key: string): ReactNode[] {
const out: ReactNode[] = []
let last = 0
let n = 0
for (const match of text.matchAll(TOKEN)) {
const at = match.index ?? 0
if (at > last) out.push(text.slice(last, at))
const [whole, bold, italic, label, url] = match
const k = `${key}-${n++}`
if (bold != null) out.push({bold})
else if (italic != null) out.push({italic})
else if (label != null && SAFE_URL.test(url)) {
out.push(
}
{line(part, accent, `l${i}`)}
per part separated by a blank line. */ export function Paragraphs({ text, accent, className, style, }: { text?: string | null accent: string className?: string style?: CSSProperties }) { const parts = (text ?? '') .split(/\n\s*\n/) .map((part) => part.trim()) .filter(Boolean) return ( <> {parts.map((part, i) => (