Spaces:
Running on Zero
Running on Zero
Naufal Arhab
chore: full codebase audit fixes, L2/whale/calibration upgrades, and rebuild frontend dist
48f60d7 Download frontend/src/components/ui/FormattedMarkdown.tsx from agus1111/Perps-Terminal: direct link, hf CLI and curl.
- Browser
- Download file 14.3 kB
-
https://huggingface.co/spaces/agus1111/Perps-Terminal/resolve/main/frontend/src/components/ui/FormattedMarkdown.tsx
- Command line
-
hf download hf://spaces/agus1111/Perps-Terminal/frontend/src/components/ui/FormattedMarkdown.tsx
-
curl -L -o FormattedMarkdown.tsx https://huggingface.co/spaces/agus1111/Perps-Terminal/resolve/main/frontend/src/components/ui/FormattedMarkdown.tsx
14.3 kB
| 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]+?\*|(?<![A-Za-z0-9_])_[^_\s][^_\n]*?_(?![A-Za-z0-9_]))/g; | |
| const parts = cleaned.split(tokenRegex); | |
| return parts.map((part, i) => { | |
| 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 ( | |
| <span | |
| key={i} | |
| className="inline-flex items-center gap-1 px-2 py-0.5 rounded-md text-[11px] font-mono font-bold bg-emerald-500/20 text-emerald-300 border border-emerald-500/40 shadow-sm align-middle mx-0.5" | |
| > | |
| <TrendingUp size={11} className="text-emerald-400" /> | |
| <span>{boldText}</span> | |
| </span> | |
| ); | |
| } | |
| if ( | |
| upper.includes('SELL ON RALLY') || | |
| upper.includes('STRONG SHORT') || | |
| upper.includes('STRONG SELL') || | |
| upper === 'SHORT' || | |
| upper === 'SELL' | |
| ) { | |
| return ( | |
| <span | |
| key={i} | |
| className="inline-flex items-center gap-1 px-2 py-0.5 rounded-md text-[11px] font-mono font-bold bg-rose-500/20 text-rose-300 border border-rose-500/40 shadow-sm align-middle mx-0.5" | |
| > | |
| <TrendingDown size={11} className="text-rose-400" /> | |
| <span>{boldText}</span> | |
| </span> | |
| ); | |
| } | |
| if ( | |
| upper.includes('WAIT-AND-SEE') || | |
| upper.includes('WAIT AND SEE') || | |
| upper.includes('WAIT & SEE') || | |
| upper === 'NEUTRAL' | |
| ) { | |
| return ( | |
| <span | |
| key={i} | |
| className="inline-flex items-center gap-1 px-2 py-0.5 rounded-md text-[11px] font-mono font-bold bg-amber-500/20 text-amber-300 border border-amber-500/40 shadow-sm align-middle mx-0.5" | |
| > | |
| <ShieldAlert size={11} className="text-amber-400" /> | |
| <span>{boldText}</span> | |
| </span> | |
| ); | |
| } | |
| return ( | |
| <strong key={i} className="font-bold text-text-primary"> | |
| {boldText} | |
| </strong> | |
| ); | |
| } | |
| // 2. Code (`code`) | |
| if (part.startsWith('`') && part.endsWith('`') && part.length >= 2) { | |
| return ( | |
| <code | |
| key={i} | |
| className="font-mono text-[11px] bg-bg-elevated px-1.5 py-0.5 rounded text-amber-300 border border-border/60 mx-0.5 font-semibold" | |
| > | |
| {part.slice(1, -1)} | |
| </code> | |
| ); | |
| } | |
| // 3. Italic (*text* or _text_) | |
| if ( | |
| (part.startsWith('*') && part.endsWith('*') && part.length >= 2) || | |
| (part.startsWith('_') && part.endsWith('_') && part.length >= 2) | |
| ) { | |
| return ( | |
| <em key={i} className="italic text-text-secondary"> | |
| {part.slice(1, -1)} | |
| </em> | |
| ); | |
| } | |
| // Plain text: safely strip any leftover stray double asterisks | |
| const cleanText = part.replace(/\*\*/g, ''); | |
| return <span key={i}>{cleanText}</span>; | |
| }); | |
| } | |
| 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: <Sparkles size={14} className="text-purple-400" />, | |
| 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: <Waves size={14} className="text-cyan-400" />, | |
| 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: <Magnet size={14} className="text-amber-400" />, | |
| 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: <Crosshair size={14} className="text-emerald-400" />, | |
| 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: <Brain size={14} className="text-rose-400" />, | |
| dot: 'bg-rose-400', | |
| }; | |
| } | |
| return { | |
| border: 'border-border/80', | |
| bg: 'bg-bg-elevated/40', | |
| badgeBg: 'bg-bg-elevated text-text-primary border-border', | |
| icon: <Sparkles size={14} className="text-purple-400" />, | |
| 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 ( | |
| <div className={`space-y-2 text-xs sm:text-[13px] text-text-primary leading-relaxed ${className}`}> | |
| {sections[0].items.map((line, idx) => ( | |
| <p key={idx}>{renderInlineMarkdown(line)}</p> | |
| ))} | |
| </div> | |
| ); | |
| } | |
| return ( | |
| <div className={`space-y-3 font-sans text-xs sm:text-[13px] ${className}`}> | |
| {sections.map((sec, sIdx) => { | |
| const meta = getSectionHeaderMeta(sec.title); | |
| return ( | |
| <div | |
| key={sec.id || sIdx} | |
| className={`rounded-xl border p-3.5 space-y-2.5 transition-all shadow-sm ${meta.border} ${meta.bg}`} | |
| > | |
| {/* Section Header Title Banner */} | |
| {sec.title && ( | |
| <div className="flex items-center gap-2 pb-2 border-b border-border/40"> | |
| <span className="p-1 rounded-md bg-bg-card/80 border border-border/50 shrink-0"> | |
| {meta.icon} | |
| </span> | |
| <h4 className="font-bold text-xs sm:text-sm font-mono text-text-primary tracking-tight"> | |
| {renderInlineMarkdown(sec.title)} | |
| </h4> | |
| </div> | |
| )} | |
| {/* Section Items / Bullets */} | |
| <div className="space-y-2"> | |
| {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 ( | |
| <div | |
| key={iIdx} | |
| className="flex items-start gap-2 text-xs sm:text-[13px] leading-relaxed group" | |
| > | |
| <span className={`w-1.5 h-1.5 rounded-full mt-2 shrink-0 ${meta.dot}`} /> | |
| <div className="flex-1"> | |
| <span className="font-semibold text-purple-200 mr-1.5"> | |
| {key}: | |
| </span> | |
| <span className="text-text-primary/95"> | |
| {renderInlineMarkdown(val)} | |
| </span> | |
| </div> | |
| </div> | |
| ); | |
| } | |
| // 2. Generic Bullet: "- Some bullet item" | |
| if (item.startsWith('- ') || item.startsWith('* ') || item.startsWith('• ')) { | |
| const bulletText = item.replace(/^[-*•]\s+/, '').trim(); | |
| return ( | |
| <div | |
| key={iIdx} | |
| className="flex items-start gap-2 text-xs sm:text-[13px] leading-relaxed" | |
| > | |
| <span className={`w-1.5 h-1.5 rounded-full mt-2 shrink-0 ${meta.dot}`} /> | |
| <div className="flex-1 text-text-primary/95"> | |
| {renderInlineMarkdown(bulletText)} | |
| </div> | |
| </div> | |
| ); | |
| } | |
| // 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 ( | |
| <div | |
| key={iIdx} | |
| className="flex items-start gap-2.5 text-xs sm:text-[13px] leading-relaxed p-2 rounded-lg bg-bg-card/50 border border-border/50" | |
| > | |
| <span className="px-1.5 py-0.2 rounded text-[10px] font-mono font-bold bg-bg-elevated text-purple-300 border border-border/60 shrink-0 mt-0.5"> | |
| {num} | |
| </span> | |
| <div className="flex-1"> | |
| <span className="font-bold text-purple-200 mr-1.5"> | |
| {key}: | |
| </span> | |
| <span className="text-text-primary/95"> | |
| {renderInlineMarkdown(val)} | |
| </span> | |
| </div> | |
| </div> | |
| ); | |
| } | |
| return ( | |
| <div | |
| key={iIdx} | |
| className="flex items-start gap-2.5 text-xs sm:text-[13px] leading-relaxed p-2 rounded-lg bg-bg-card/50 border border-border/50" | |
| > | |
| <span className="px-1.5 py-0.2 rounded text-[10px] font-mono font-bold bg-bg-elevated text-purple-300 border border-border/60 shrink-0 mt-0.5"> | |
| {num} | |
| </span> | |
| <div className="flex-1 text-text-primary/95"> | |
| {renderInlineMarkdown(rest)} | |
| </div> | |
| </div> | |
| ); | |
| } | |
| // 4. Callout / Alert Warning (e.g. ⚠️ or 🚨) | |
| if (item.startsWith('⚠️') || item.startsWith('🚨') || item.toUpperCase().startsWith('WARNING:')) { | |
| return ( | |
| <div | |
| key={iIdx} | |
| className="p-2.5 rounded-lg bg-amber-500/10 border border-amber-500/30 text-amber-200 text-xs sm:text-[12px] flex items-start gap-2" | |
| > | |
| <AlertTriangle size={14} className="text-amber-400 shrink-0 mt-0.5" /> | |
| <div className="leading-snug">{renderInlineMarkdown(item)}</div> | |
| </div> | |
| ); | |
| } | |
| // 5. Normal Paragraph | |
| return ( | |
| <p key={iIdx} className="text-text-primary/95 leading-relaxed"> | |
| {renderInlineMarkdown(item)} | |
| </p> | |
| ); | |
| })} | |
| </div> | |
| </div> | |
| ); | |
| })} | |
| </div> | |
| ); | |
| } | |