import React from 'react';
import {
TrendingUp,
TrendingDown,
ShieldAlert,
Waves,
Magnet,
Crosshair,
Sparkles,
AlertTriangle,
Brain,
} from 'lucide-react';
interface FormattedMarkdownProps {
content: string;
className?: string;
}
interface ParsedSection {
id: string;
title: string;
rawHeader: string;
items: string[];
}
/**
* Format inline tokens (**bold**, *italic*, `code`).
* Replaces raw markdown stars with actual HTML tags.
* Also highlights institutional trading bias terms as stylish badges.
*/
export function renderInlineMarkdown(text: string): React.ReactNode {
if (!text) return null;
// Clean any orphan double asterisks at the boundary
const cleaned = text.trim();
// Split by bold (**...**), code (`...`), and italic (*...* or standalone _..._ avoiding snake_case)
const tokenRegex = /(\*\*[^*]+?\*\*|`[^`]+`|\*[^*\n]+?\*|(? {
if (!part) return null;
// 1. Bold (**text**)
if (part.startsWith('**') && part.endsWith('**') && part.length >= 4) {
const boldText = part.slice(2, -2).trim();
// Check if boldText is a directional bias keyword
const upper = boldText.toUpperCase();
if (
upper.includes('BUY ON PULLBACK') ||
upper.includes('STRONG LONG') ||
upper.includes('STRONG BUY') ||
upper === 'LONG' ||
upper === 'BUY'
) {
return (
{boldText}
);
}
if (
upper.includes('SELL ON RALLY') ||
upper.includes('STRONG SHORT') ||
upper.includes('STRONG SELL') ||
upper === 'SHORT' ||
upper === 'SELL'
) {
return (
{boldText}
);
}
if (
upper.includes('WAIT-AND-SEE') ||
upper.includes('WAIT AND SEE') ||
upper.includes('WAIT & SEE') ||
upper === 'NEUTRAL'
) {
return (
{boldText}
);
}
return (
{boldText}
);
}
// 2. Code (`code`)
if (part.startsWith('`') && part.endsWith('`') && part.length >= 2) {
return (
{part.slice(1, -1)}
);
}
// 3. Italic (*text* or _text_)
if (
(part.startsWith('*') && part.endsWith('*') && part.length >= 2) ||
(part.startsWith('_') && part.endsWith('_') && part.length >= 2)
) {
return (
{part.slice(1, -1)}
);
}
// Plain text: safely strip any leftover stray double asterisks
const cleanText = part.replace(/\*\*/g, '');
return {cleanText};
});
}
function getSectionHeaderMeta(title: string) {
const upper = title.toUpperCase();
if (upper.includes('EXECUTIVE') || upper.includes('VERDICT') || upper.includes('🏛️')) {
return {
border: 'border-purple-500/30',
bg: 'bg-purple-950/15',
badgeBg: 'bg-purple-500/20 text-purple-300 border-purple-500/30',
icon: ,
dot: 'bg-purple-400',
};
}
if (upper.includes('ANATOMI') || upper.includes('MODAL') || upper.includes('CVD') || upper.includes('🌊')) {
return {
border: 'border-cyan-500/30',
bg: 'bg-cyan-950/15',
badgeBg: 'bg-cyan-500/20 text-cyan-300 border-cyan-500/30',
icon: ,
dot: 'bg-cyan-400',
};
}
if (upper.includes('MIKROSTRUKTUR') || upper.includes('VOLATILITAS') || upper.includes('MAGNET') || upper.includes('🧲')) {
return {
border: 'border-amber-500/30',
bg: 'bg-amber-950/15',
badgeBg: 'bg-amber-500/20 text-amber-300 border-amber-500/30',
icon: ,
dot: 'bg-amber-400',
};
}
if (upper.includes('SETUP') || upper.includes('ACTIONABLE') || upper.includes('TRADE') || upper.includes('🎯')) {
return {
border: 'border-emerald-500/30',
bg: 'bg-emerald-950/15',
badgeBg: 'bg-emerald-500/20 text-emerald-300 border-emerald-500/30',
icon: ,
dot: 'bg-emerald-400',
};
}
if (upper.includes('PENGAMAN') || upper.includes('MEMORI') || upper.includes('EVALUASI') || upper.includes('🛡️')) {
return {
border: 'border-rose-500/30',
bg: 'bg-rose-950/15',
badgeBg: 'bg-rose-500/20 text-rose-300 border-rose-500/30',
icon: ,
dot: 'bg-rose-400',
};
}
return {
border: 'border-border/80',
bg: 'bg-bg-elevated/40',
badgeBg: 'bg-bg-elevated text-text-primary border-border',
icon: ,
dot: 'bg-purple-400',
};
}
/**
* Check if a given line is a Section Header
*/
function isSectionHeader(line: string): boolean {
const l = line.trim();
if (!l) return false;
if (l.startsWith('#')) return true;
if (l.startsWith('===') && l.endsWith('===')) return true;
if (/^[-*•>]\s/.test(l)) return false;
// Match numbered or icon-prefixed ALL-CAPS section titles only (anchored to end of line)
if (/^(?:[^\w\s]+\s*)?\d+\.\s+[A-Z0-9][A-Z0-9\s/()&,.\-]{3,}$/u.test(l)) return true;
if (/^[^\w\s\-•*]+\s+[A-Z0-9][A-Z0-9\s/()&,.\-]{4,}$/u.test(l)) return true;
return false;
}
export function FormattedMarkdown({ content, className = '' }: FormattedMarkdownProps) {
if (!content) return null;
// Split lines
const rawLines = content.split(/\r?\n/);
// Group into sections
const sections: ParsedSection[] = [];
let currentSection: ParsedSection = {
id: 'intro',
title: '',
rawHeader: '',
items: [],
};
for (const rawLine of rawLines) {
const line = rawLine.trim();
if (!line) continue;
if (isSectionHeader(line)) {
if (currentSection.title || currentSection.items.length > 0) {
sections.push(currentSection);
}
currentSection = {
id: `sec-${sections.length}`,
title: line.replace(/^#+\s*/, '').replace(/^===\s*/, '').replace(/\s*===$/, ''),
rawHeader: line,
items: [],
};
} else {
currentSection.items.push(line);
}
}
if (currentSection.title || currentSection.items.length > 0) {
sections.push(currentSection);
}
// If no structured sections were detected, render simple paragraphs
if (sections.length === 1 && !sections[0].title) {
return (
{sections[0].items.map((line, idx) => (
{renderInlineMarkdown(line)}
))}
);
}
return (
{sections.map((sec, sIdx) => {
const meta = getSectionHeaderMeta(sec.title);
return (
{/* Section Header Title Banner */}
{sec.title && (
{meta.icon}
{renderInlineMarkdown(sec.title)}
)}
{/* Section Items / Bullets */}
{sec.items.map((item, iIdx) => {
// 1. Key-Value Bullet: "- **Directional Bias**: Value" or "• **Key**: Value"
const keyValueMatch = item.match(/^[-*•]\s*\*\*([^*]+?)\*\*[:\s]+(.*)$/);
if (keyValueMatch) {
const key = keyValueMatch[1].trim();
const val = keyValueMatch[2].trim();
return (
{key}:
{renderInlineMarkdown(val)}
);
}
// 2. Generic Bullet: "- Some bullet item"
if (item.startsWith('- ') || item.startsWith('* ') || item.startsWith('• ')) {
const bulletText = item.replace(/^[-*•]\s+/, '').trim();
return (
{renderInlineMarkdown(bulletText)}
);
}
// 3. Numbered Item: "1. **[Pantangan]**: Value" or "1. Text"
const numberedMatch = item.match(/^(\d+)\.\s+(.*)$/);
if (numberedMatch) {
const num = numberedMatch[1];
const rest = numberedMatch[2].trim();
// Check if rest has **Key**:
const restKeyVal = rest.match(/^\*\*([^*]+?)\*\*[:\s]+(.*)$/);
if (restKeyVal) {
const key = restKeyVal[1].trim();
const val = restKeyVal[2].trim();
return (
{num}
{key}:
{renderInlineMarkdown(val)}
);
}
return (
{num}
{renderInlineMarkdown(rest)}
);
}
// 4. Callout / Alert Warning (e.g. ⚠️ or 🚨)
if (item.startsWith('⚠️') || item.startsWith('🚨') || item.toUpperCase().startsWith('WARNING:')) {
return (
{renderInlineMarkdown(item)}
);
}
// 5. Normal Paragraph
return (
{renderInlineMarkdown(item)}
);
})}
);
})}
);
}