Blocks (migration 032): video (click to play, sharing the featured cards' player), button and callout types; quotes get an attribution and images alt text separate from the caption. Paragraphs, callouts, quotes and list items take **bold**, *italic*, [text](url), line breaks and blank-line paragraphs, parsed into elements (never HTML) with only safe link schemes. A list item's detail no longer uses an em-dash. Editor: each block type shows only its fields, with a named type and a description, and every open block previews itself with the site's renderer. Any repeater row can now be duplicated or have a new row inserted below it; nested collections take `when`, and help text can depend on the row. src/seed-showcase.js writes a local test post using every block type and option. It refuses the production database. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
137 lines
4 KiB
TypeScript
137 lines
4 KiB
TypeScript
/* ═══════════════════════════════════════════════════════════════
|
|
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
|
|
<p> 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 (
|
|
<a
|
|
href={href}
|
|
className={className}
|
|
style={style}
|
|
{...(app ? {} : { target: '_blank', rel: 'noreferrer' })}
|
|
>
|
|
{children}
|
|
</a>
|
|
)
|
|
}
|
|
if (href.startsWith('#')) {
|
|
return (
|
|
<a href={href} className={className} style={style}>
|
|
{children}
|
|
</a>
|
|
)
|
|
}
|
|
return (
|
|
<Link to={href} className={className} style={style}>
|
|
{children}
|
|
</Link>
|
|
)
|
|
}
|
|
|
|
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(<strong key={k} className="font-semibold">{bold}</strong>)
|
|
else if (italic != null) out.push(<em key={k}>{italic}</em>)
|
|
else if (label != null && SAFE_URL.test(url)) {
|
|
out.push(
|
|
<Anchor key={k} href={url} className="underline underline-offset-2" style={{ color: accent }}>
|
|
{label}
|
|
</Anchor>,
|
|
)
|
|
} else out.push(whole)
|
|
last = at + whole.length
|
|
}
|
|
if (last < text.length) out.push(text.slice(last))
|
|
return out
|
|
}
|
|
|
|
/** Text inside another element: formatting and line breaks. */
|
|
export function Inline({ text, accent }: { text?: string | null; accent: string }) {
|
|
const lines = (text ?? '').split('\n')
|
|
return (
|
|
<>
|
|
{lines.map((part, i) => (
|
|
<Fragment key={i}>
|
|
{i > 0 && <br />}
|
|
{line(part, accent, `l${i}`)}
|
|
</Fragment>
|
|
))}
|
|
</>
|
|
)
|
|
}
|
|
|
|
/** Block text: one <p> 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) => (
|
|
<p key={i} className={className} style={style}>
|
|
<Inline text={part} accent={accent} />
|
|
</p>
|
|
))}
|
|
</>
|
|
)
|
|
}
|
|
|
|
/** For the editor's help text under a formatted box. */
|
|
export const FORMATTING_HELP =
|
|
'**bold**, *italic*, [link text](https://…). A blank line starts a new paragraph.'
|