forked from Tadrisino/MathApp_frontend
math in Persian text: lists read right-to-left, every content text goes through MathText, chat input picks its direction per line
Measured by rendering every question, option and several tutor answers in
Chrome and comparing the on-screen x of each number (2026-09-29):
- A list inside a sentence ('از −۳ به −۲، −۱، ۰ میرسیم', 'الگویِ ۲، ۵، ۸')
was locked as one left-to-right run, so all 482 read backwards. '،' and
'؛' outside brackets now split the run; sets and coordinates in brackets
stay one run. Locked runs are isolates (isolate-override), otherwise the
comma between two of them was still pulled left-to-right.
- Without MathText 704 of 931 minus signs and 602 of 983 expressions were
misplaced; lesson and island titles, the lesson intro, unit blurb,
notification bodies and ticket messages now go through it.
- The chat box takes its direction from each line's first letter, so a
line of pure math stays left-to-right while typing. A line that starts
in Persian can still show its math reordered inside the box; a textarea
cannot isolate part of its text. The sent message renders correctly.
Four expressions with Persian words inside them ('f(x) = (ریشه دوم x) + ۵۰')
remain ambiguous.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -8,6 +8,12 @@ import { Fragment } from 'react';
|
||||
* میشد. هر تکهای که حرفِ فارسی ندارد ولی رقم دارد، اینجا با bidi-override چپبهراست
|
||||
* قفل میشود؛ جملهی فارسی دورش دست نمیخورد.
|
||||
*
|
||||
* فهرستِ داخلِ جمله («از −۳ به −۲، −۱، ۰ میرسیم»، «الگویِ ۲، ۵، ۸، …») مالِ جمله است و
|
||||
* از راست خوانده میشود؛ پس «،» و «؛» بیرونِ پرانتز و آکولاد تکه را میشکنند و هر عضو
|
||||
* جدا قفل میشود. داخلِ `{۱، ۲، ۳}` و `(۱، −۴)` ریاضی است و یکجا قفل میماند. سنجیده
|
||||
* با مختصاتِ رسمشده در Chrome روی همهی سؤالها و گزینههای بانک و چند جوابِ دستیار
|
||||
* (۲۰۲۶-۰۹-۲۹)؛ پیش از آن هر ۴۸۲ فهرستِ داخلِ جمله برعکس دیده میشد.
|
||||
*
|
||||
* آزموده روی Chrome (دسکتاپ و موبایلِ شبیهسازیشده)؛ روی Safariِ واقعیِ آیفون دیده نشده.
|
||||
*/
|
||||
// حروف، اِعراب (کسرهی «خودِ»)، و نیمفاصله؛ رقمهای ۰–۹ عمداً بیروناند.
|
||||
@@ -17,36 +23,67 @@ const RUN = new RegExp(`[^${LETTER}\\n]*[0-9۰-۹][^${LETTER}\\n]*`, 'g');
|
||||
// نشانههایی که مالِ جملهی فارسیاند، نه ریاضی: از دو سرِ تکه بیرون میمانند
|
||||
const EDGE_START = /^[\s،؛:!؟«»"'.=]+/;
|
||||
const EDGE_END = /[\s،؛:!؟«»"'.=]+$/;
|
||||
const LTR = { unicodeBidi: 'bidi-override', direction: 'ltr' } as const;
|
||||
// isolate، نه embedding: تکهی قفلشده از بیرون یک واحدِ خنثی است. با bidi-overrideِ ساده
|
||||
// تکهها از بیرون «حرفِ چپبهراست» حساب میشدند و «،»ِ بینِ دو عضوِ فهرست را هم به
|
||||
// سمتِ خودشان میکشیدند، پس فهرست باز برعکس میشد.
|
||||
const LTR = { unicodeBidi: 'isolate-override', direction: 'ltr' } as const;
|
||||
|
||||
export function MathText({ text }: { text: string }) {
|
||||
const out: React.ReactNode[] = [];
|
||||
const depth = bracketDepth(text);
|
||||
let last = 0, i = 0;
|
||||
for (const m of text.matchAll(RUN)) {
|
||||
const start = m.index ?? 0;
|
||||
let run = m[0];
|
||||
// شمارهی مرحله در سرِ خط («۴) …») مالِ جملهی فارسی است و باید سمتِ راست بماند.
|
||||
const atLineStart = start === 0 || text[start - 1] === '\n';
|
||||
const step = atLineStart ? /^\s*[0-9۰-۹]{1,2}\)\s*/.exec(run)?.[0] ?? '' : '';
|
||||
const lead = step + (EDGE_START.exec(run.slice(step.length))?.[0] ?? '');
|
||||
run = run.slice(lead.length);
|
||||
const tail = EDGE_END.exec(run)?.[0] ?? '';
|
||||
run = run.slice(0, run.length - tail.length);
|
||||
// «(۱، −۴)» باید کامل قفل شود، ولی پرانتزِ بیجفت در لبهی تکه مالِ جمله است:
|
||||
// در «۱۰ − ۲ (با محاسبه…)» پرانتزِ باز جزوِ جملهی فارسیِ بعدی است و اگر قفل شود
|
||||
// به سمتِ غلط میرود.
|
||||
const edge = trimUnpaired(run);
|
||||
const head = lead + edge.head;
|
||||
run = edge.run;
|
||||
if (!run || !/[0-9۰-۹]/.test(run)) continue;
|
||||
out.push(text.slice(last, start + head.length));
|
||||
out.push(<span key={i++} style={LTR}>{run}</span>);
|
||||
last = start + head.length + run.length;
|
||||
for (const [start, piece] of splitList(m[0], m.index ?? 0, depth)) {
|
||||
let run = piece;
|
||||
// شمارهی مرحله در سرِ خط («۴) …») مالِ جملهی فارسی است و باید سمتِ راست بماند.
|
||||
const atLineStart = start === 0 || text[start - 1] === '\n';
|
||||
const step = atLineStart ? /^\s*[0-9۰-۹]{1,2}\)\s*/.exec(run)?.[0] ?? '' : '';
|
||||
const lead = step + (EDGE_START.exec(run.slice(step.length))?.[0] ?? '');
|
||||
run = run.slice(lead.length);
|
||||
const tail = EDGE_END.exec(run)?.[0] ?? '';
|
||||
run = run.slice(0, run.length - tail.length);
|
||||
// «(۱، −۴)» باید کامل قفل شود، ولی پرانتزِ بیجفت در لبهی تکه مالِ جمله است:
|
||||
// در «۱۰ − ۲ (با محاسبه…)» پرانتزِ باز جزوِ جملهی فارسیِ بعدی است و اگر قفل شود
|
||||
// به سمتِ غلط میرود.
|
||||
const edge = trimUnpaired(run);
|
||||
const head = lead + edge.head;
|
||||
run = edge.run;
|
||||
if (!run || !/[0-9۰-۹]/.test(run)) continue;
|
||||
out.push(text.slice(last, start + head.length));
|
||||
out.push(<span key={i++} style={LTR}>{run}</span>);
|
||||
last = start + head.length + run.length;
|
||||
}
|
||||
}
|
||||
out.push(text.slice(last));
|
||||
return <>{out.map((n, j) => (typeof n === 'string' ? <Fragment key={`t${j}`}>{n}</Fragment> : n))}</>;
|
||||
}
|
||||
|
||||
/** عمقِ پرانتز/کروشه/آکولاد پیش از هر نویسهی متن. */
|
||||
function bracketDepth(text: string): number[] {
|
||||
const depth: number[] = [];
|
||||
let d = 0;
|
||||
for (const ch of text) {
|
||||
depth.push(d);
|
||||
if ('([{'.includes(ch)) d++;
|
||||
else if (')]}'.includes(ch)) d = Math.max(0, d - 1);
|
||||
}
|
||||
return depth;
|
||||
}
|
||||
|
||||
/** تکه را روی «،» و «؛»ِ بیرونِ پرانتز میشکند؛ جداکننده خودش بیرون میماند. */
|
||||
function splitList(run: string, offset: number, depth: number[]): [number, string][] {
|
||||
const pieces: [number, string][] = [];
|
||||
let from = 0;
|
||||
for (let k = 0; k < run.length; k++) {
|
||||
if ((run[k] === '،' || run[k] === '؛') && !depth[offset + k]) {
|
||||
pieces.push([offset + from, run.slice(from, k)]);
|
||||
from = k + 1;
|
||||
}
|
||||
}
|
||||
pieces.push([offset + from, run.slice(from)]);
|
||||
return pieces;
|
||||
}
|
||||
|
||||
const count = (s: string, ch: string) => s.split(ch).length - 1;
|
||||
|
||||
/**
|
||||
@@ -55,11 +92,13 @@ const count = (s: string, ch: string) => s.split(ch).length - 1;
|
||||
*/
|
||||
function trimUnpaired(run: string): { head: string; run: string } {
|
||||
let head = '';
|
||||
while (/[(\[]\s*$/.test(run) && count(run, '(') + count(run, '[') > count(run, ')') + count(run, ']')) {
|
||||
run = run.slice(0, run.length - /\s*[(\[]\s*$/.exec(run)![0].length);
|
||||
const opens = (r: string) => count(r, '(') + count(r, '[') + count(r, '{');
|
||||
const closes = (r: string) => count(r, ')') + count(r, ']') + count(r, '}');
|
||||
while (/[(\[{]\s*$/.test(run) && opens(run) > closes(run)) {
|
||||
run = run.slice(0, run.length - /\s*[(\[{]\s*$/.exec(run)![0].length);
|
||||
}
|
||||
while (/^\s*[)\]]/.test(run) && count(run, ')') + count(run, ']') > count(run, '(') + count(run, '[')) {
|
||||
const m = /^\s*[)\]]\s*/.exec(run)![0];
|
||||
while (/^\s*[)\]}]/.test(run) && closes(run) > opens(run)) {
|
||||
const m = /^\s*[)\]}]\s*/.exec(run)![0];
|
||||
head += m;
|
||||
run = run.slice(m.length);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user