Files
webb/src/components/sections/RichTextSection.tsx
T

238 lines
7.6 KiB
TypeScript
Raw Normal View History

'use client';
import React from 'react';
import {
ShieldCheck,
CheckCircle2,
Lock,
Cpu,
Zap,
FileCheck,
AlertCircle,
ExternalLink,
ChevronRight
} from 'lucide-react';
interface RichTextSectionProps {
markdown?: string;
}
export function RichTextSection({ markdown = '' }: RichTextSectionProps) {
// Helper to parse inline markdown (bold, italic, code, links)
const renderInline = (text: string): React.ReactNode[] => {
// Split on markdown tokens: **bold**, *italic*, `code`, [link](url)
const regex = /(\*\*.*?\*\*|\*.*?\*|`.*?`|\[.*?\]\(.*?\))/g;
const parts = text.split(regex);
return parts.map((part, i) => {
if (!part) return null;
if (part.startsWith('**') && part.endsWith('**')) {
return (
<strong key={i} className="font-bold text-slate-900">
{part.slice(2, -2)}
</strong>
);
}
if (part.startsWith('*') && part.endsWith('*')) {
return (
<em key={i} className="italic text-slate-700">
{part.slice(1, -1)}
</em>
);
}
if (part.startsWith('`') && part.endsWith('`')) {
return (
<code key={i} className="px-1.5 py-0.5 mx-0.5 rounded-md bg-slate-100 text-slate-800 font-mono text-xs border border-slate-200">
{part.slice(1, -1)}
</code>
);
}
if (part.startsWith('[') && part.includes('](') && part.endsWith(')')) {
const linkMatch = part.match(/\[(.*?)\]\((.*?)\)/);
if (linkMatch) {
const [, linkText, linkHref] = linkMatch;
return (
<a
key={i}
href={linkHref}
className="text-accent font-semibold hover:underline inline-flex items-center gap-0.5"
>
<span>{linkText}</span>
{linkHref.startsWith('http') && <ExternalLink className="w-3 h-3 inline-block" />}
</a>
);
}
}
return <React.Fragment key={i}>{part}</React.Fragment>;
});
};
const renderContent = (content: string) => {
const lines = content.split('\n');
const nodes: React.ReactNode[] = [];
let tableRows: string[][] = [];
let inTable = false;
const flushTable = (keyIdx: number) => {
if (tableRows.length === 0) return null;
const headers = tableRows[0];
const rows = tableRows.slice(2); // Skip separator row
const tableNode = (
<div key={`table-${keyIdx}`} className="my-6 overflow-x-auto rounded-2xl border border-slate-200 shadow-2xs">
<table className="w-full text-left text-xs font-sans">
<thead className="bg-slate-100 text-slate-900 uppercase font-bold tracking-wider border-b border-slate-200">
<tr>
{headers.map((h, hIdx) => (
<th key={hIdx} className="px-4 py-3">
{renderInline(h.trim())}
</th>
))}
</tr>
</thead>
<tbody className="divide-y divide-slate-100 bg-white">
{rows.map((row, rIdx) => (
<tr key={rIdx} className="hover:bg-slate-50/80 transition-colors">
{row.map((cell, cIdx) => (
<td key={cIdx} className="px-4 py-3 text-slate-700 leading-relaxed">
{renderInline(cell.trim())}
</td>
))}
</tr>
))}
</tbody>
</table>
</div>
);
tableRows = [];
inTable = false;
return tableNode;
};
for (let idx = 0; idx < lines.length; idx++) {
const line = lines[idx];
// Table parsing
if (line.trim().startsWith('|') && line.trim().endsWith('|')) {
inTable = true;
const cols = line.trim().slice(1, -1).split('|');
tableRows.push(cols);
continue;
} else if (inTable) {
const tableNode = flushTable(idx);
if (tableNode) nodes.push(tableNode);
}
// Horizontal Divider
if (line.trim() === '---' || line.trim() === '***') {
nodes.push(
<hr key={idx} className="my-8 border-t border-slate-200" />
);
continue;
}
// Headings
if (line.startsWith('## ')) {
nodes.push(
<div key={idx} className="pt-8 pb-3 border-b border-slate-200 mb-4 first:pt-0">
<h2 className="text-2xl sm:text-3xl font-display font-bold text-slate-900 tracking-tight flex items-center gap-2.5">
<span className="w-2 h-6 rounded-full bg-accent inline-block" />
<span>{line.replace('## ', '')}</span>
</h2>
</div>
);
continue;
}
if (line.startsWith('### ')) {
nodes.push(
<h3 key={idx} className="text-lg sm:text-xl font-display font-bold text-slate-900 mt-6 mb-2 tracking-tight">
{renderInline(line.replace('### ', ''))}
</h3>
);
continue;
}
if (line.startsWith('#### ')) {
nodes.push(
<h4 key={idx} className="text-sm sm:text-base font-display font-bold text-slate-800 mt-4 mb-1.5">
{renderInline(line.replace('#### ', ''))}
</h4>
);
continue;
}
// Blockquotes / Callout Boxes
if (line.startsWith('> ')) {
nodes.push(
<div key={idx} className="my-4 p-4 rounded-2xl bg-accent/5 border-l-4 border-accent text-slate-800 text-xs sm:text-sm font-sans flex items-start gap-3">
<ShieldCheck className="w-5 h-5 text-accent shrink-0 mt-0.5" />
<div className="leading-relaxed">
{renderInline(line.replace('> ', ''))}
</div>
</div>
);
continue;
}
// Bullet List items
if (line.trim().startsWith('- ') || line.trim().startsWith('* ')) {
nodes.push(
<div key={idx} className="flex items-start gap-2.5 my-2 text-xs sm:text-sm text-slate-700 font-sans leading-relaxed pl-1">
<div className="w-1.5 h-1.5 rounded-full bg-accent mt-2 shrink-0" />
<div className="flex-1">{renderInline(line.trim().slice(2))}</div>
</div>
);
continue;
}
// Numbered List items (e.g. "1. ", "12. ")
const numMatch = line.trim().match(/^(\d+)\.\s+(.*)/);
if (numMatch) {
const num = numMatch[1];
const rest = numMatch[2];
nodes.push(
<div key={idx} className="flex items-start gap-3 my-3 text-xs sm:text-sm text-slate-700 font-sans leading-relaxed">
<span className="w-6 h-6 rounded-full bg-slate-900 text-white font-mono text-xs font-bold flex items-center justify-center shrink-0 mt-0.5 shadow-2xs">
{num}
</span>
<div className="flex-1 pt-0.5">{renderInline(rest)}</div>
</div>
);
continue;
}
// Empty Lines
if (line.trim().length === 0) {
nodes.push(<div key={idx} className="h-3" />);
continue;
}
// Normal Paragraph
nodes.push(
<p key={idx} className="text-xs sm:text-sm text-slate-600 font-sans leading-relaxed my-2.5">
{renderInline(line)}
</p>
);
}
if (inTable) {
const tableNode = flushTable(lines.length);
if (tableNode) nodes.push(tableNode);
}
return nodes;
};
return (
<section className="py-10 sm:py-16 bg-paper">
<div className="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8">
<div className="studio-sheet rounded-3xl p-6 sm:p-12 bg-white border border-rule shadow-sheet">
{renderContent(markdown)}
</div>
</div>
</section>
);
}