From 782998e325345e55eaaab3376aa7c9a67cc7ece3 Mon Sep 17 00:00:00 2001 From: AliReza Date: Wed, 14 Oct 2026 22:13:34 +0330 Subject: [PATCH] MathText: matrix [[..],[..]] drawn as a real matrix block instead of wrapping mid-row Co-Authored-By: Claude Sonnet 5.5 --- src/lib/MathText.tsx | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/lib/MathText.tsx b/src/lib/MathText.tsx index 2eaed7d..049634d 100644 --- a/src/lib/MathText.tsx +++ b/src/lib/MathText.tsx @@ -41,7 +41,43 @@ const FORMULA = /[=<>≤≥≠∈∉⊂⊆∪∩+×÷^√∑∫]|[0-9۰-۹A-Za-z * چشم وسطِ فرمول به خطِ بعد نیفتد. عددِ تنها و عبارتِ کوتاهِ بدونِ نمادِ عمل داخلِ جمله می‌ماند. * آزموده نشده: صفحه‌ی واقعیِ اپ در Chrome و Safari؛ فقط منطقِ تشخیص با نمونه‌های بانک سنجیده شد. */ +// ماتریس «[[۲، ۷]، [۴، ۱]]»: ردیف‌ها داخلِ کروشه‌ی تو در تو. اگر مثلِ متنِ عادی بگذاریم، در خطِ باریک +// وسطِ ردیف شکسته می‌شود و راست‌به‌چپ بودنِ جمله عددها را جابه‌جا نشان می‌دهد (گزارشِ مالک ۲۰۲۶-۱۰-۱۴). +const MATRIX = /\[\s*\[[^\[\]]*\](?:\s*[،,]\s*\[[^\[\]]*\])*\s*\]/g; + +function Matrix({ src }: { src: string }) { + const rows = [...src.matchAll(/\[([^\[\]]*)\]/g)].map((m) => m[1].split(/[،,]/).map((c) => plainPersian(plainMath(c.trim())))); + const border = '2px solid currentColor'; + return ( + + {rows.map((row, i) => ( + + {row.map((c, j) => {c})} + + ))} + + ); +} + export function MathText({ text: raw, display = false }: { text: string; display?: boolean }) { + if (!MATRIX.test(raw)) { MATRIX.lastIndex = 0; return ; } + MATRIX.lastIndex = 0; + const out: React.ReactNode[] = []; + let last = 0, k = 0; + for (const m of raw.matchAll(MATRIX)) { + const at = m.index ?? 0; + if (at > last) out.push(); + out.push(); + last = at + m[0].length; + } + if (last < raw.length) out.push(); + return <>{out}; +} + +function Inline({ raw, display }: { raw: string; display: boolean }) { const text = plainPersian(plainMath(raw)); const out: React.ReactNode[] = []; const depth = bracketDepth(text);