mirror of
https://github.com/VibedByKaKi/t3-code-android-nightly.git
synced 2026-11-27 08:03:27 +01:00
56 lines
2 KiB
TypeScript
56 lines
2 KiB
TypeScript
export type TextDirection = "ltr" | "rtl";
|
|
|
|
// Hebrew through Arabic Extended-A, their presentation forms, and the
|
|
// supplementary right-to-left blocks.
|
|
export const RTL_LETTER =
|
|
/[\u0590-\u08FF\uFB1D-\uFDFF\uFE70-\uFEFC\u{10800}-\u{10FFF}\u{1E800}-\u{1EFFF}]/u;
|
|
export const LETTER = /\p{L}/u;
|
|
const SENTENCE_END = /(?<=[.!?؟])(?<!\b(?:e\.g|i\.e)\.)\s+|\n+/i;
|
|
|
|
function majority(
|
|
units: Iterable<TextDirection | undefined>,
|
|
tieDirection?: () => TextDirection | undefined,
|
|
): TextDirection | undefined {
|
|
let rtl = 0;
|
|
let ltr = 0;
|
|
let first: TextDirection | undefined;
|
|
for (const direction of units) {
|
|
if (!direction) continue;
|
|
first ??= direction;
|
|
if (direction === "rtl") rtl += 1;
|
|
else ltr += 1;
|
|
}
|
|
if (rtl === ltr) return tieDirection ? tieDirection() : first;
|
|
return rtl > ltr ? "rtl" : "ltr";
|
|
}
|
|
|
|
export function hasRightToLeftLetter(text: string): boolean {
|
|
return RTL_LETTER.test(text);
|
|
}
|
|
|
|
function wordDirection(word: string): TextDirection | undefined {
|
|
const letter = LETTER.exec(word)?.[0];
|
|
if (!letter) return undefined;
|
|
return RTL_LETTER.test(letter) ? "rtl" : "ltr";
|
|
}
|
|
|
|
/**
|
|
* `dir="auto"` misreads Arabic prose that opens with an English term.
|
|
* Each sentence follows its main language by word count, and the text follows most of its
|
|
* sentences, so one long English sentence cannot outvote the Arabic ones around it.
|
|
* Sentence ties use the whole-text word majority, then the first word.
|
|
*/
|
|
export function textDirection(text: string): TextDirection | undefined {
|
|
if (!RTL_LETTER.test(text)) return LETTER.test(text) ? "ltr" : undefined;
|
|
return majority(
|
|
text.split(SENTENCE_END).map((sentence) => majority(sentence.split(/\s+/).map(wordDirection))),
|
|
() => majority(text.split(/\s+/).map(wordDirection)),
|
|
);
|
|
}
|
|
|
|
const MARKDOWN_CODE_OR_URL =
|
|
/(`{3,}|~{3,})[\s\S]*?(?:\1|$)|(`{1,2})(?!`)[\s\S]*?(?<!`)\2(?!`)|https?:\/\/\S+/g;
|
|
|
|
export function markdownProseDirection(markdown: string): TextDirection | undefined {
|
|
return textDirection(markdown.replace(MARKDOWN_CODE_OR_URL, " "));
|
|
}
|