Skip to content

Commit a560266

Browse files
trancong12102claude
andcommitted
fix(mobile): paragraph direction comes from the enriched-markdown fork, not injected marks
Mobile picked each paragraph's direction in JS by injecting RLM/LRM marks, which broke autolinks next to them and re-parsed the whole document on every streamed update. The enriched-markdown fork 1.2.0-oni.22 adds a native word-majority writing direction on iOS and Android, matching desktop's counting rule, so the marks go away. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 238d71b commit a560266

14 files changed

Lines changed: 17 additions & 160 deletions

‎apps/mobile/deps/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
The Enriched Markdown tarball comes from `main` of [aislopware/enriched-markdown](https://github.com/aislopware/enriched-markdown). Its `oni-fork.json` records the exact source commit and upstream base. React block renderers, signed image sources, the `onLinksChange` link list (`renderers`, `resolveImageSource`, `onLinksChange`) and the iOS streaming memory bounds are upstream. The fork carries only changes with an open upstream PR: clipboard link metadata ([#962](https://github.com/software-mansion/enriched-markdown/pull/962)), development-host autolinks ([#961](https://github.com/software-mansion/enriched-markdown/pull/961)) and hash-route link fragments ([mity/md4c#454](https://github.com/mity/md4c/pull/454), arriving through [md4c#7](https://github.com/software-mansion-labs/md4c/pull/7)), `md4cFlags.preserveWindowsPaths`, which keeps backslashes in Windows drive paths in prose ([#1105](https://github.com/software-mansion/enriched-markdown/pull/1105), after [mity/md4c#477](https://github.com/mity/md4c/pull/477)), GitHub's strikethrough rules for `~` ([md4c#6](https://github.com/software-mansion-labs/md4c/pull/6)), `\(…\)` and `\[…\]` math delimiters ([#1043](https://github.com/software-mansion/enriched-markdown/pull/1043), [md4c#4](https://github.com/software-mansion-labs/md4c/pull/4)), iOS Copy as Markdown keeping bold spans whole ([#1143](https://github.com/software-mansion/enriched-markdown/pull/1143)), line breaks in copied table cells written as `<br>` ([#1144](https://github.com/software-mansion/enriched-markdown/pull/1144)), and Android measuring text in the span order its view draws it, so code blocks nested in list items no longer make a reply overlap the next message ([#1145](https://github.com/software-mansion/enriched-markdown/pull/1145)). It also carries two fork-only changes, both from T3 Code's fork: iOS code blocks too big for one backing store draw through tiles, so a tall block with long lines no longer shows an empty pane ([pingdotgg/enriched-markdown#15](https://github.com/pingdotgg/enriched-markdown/pull/15)), and a long-pressed iOS blockquote lifts on the background it sits on instead of drawing its text over the thread.
1+
The Enriched Markdown tarball comes from `main` of [aislopware/enriched-markdown](https://github.com/aislopware/enriched-markdown). Its `oni-fork.json` records the exact source commit and upstream base. React block renderers, signed image sources, the `onLinksChange` link list (`renderers`, `resolveImageSource`, `onLinksChange`) and the iOS streaming memory bounds are upstream. The fork carries only changes with an open upstream PR: clipboard link metadata ([#962](https://github.com/software-mansion/enriched-markdown/pull/962)), development-host autolinks ([#961](https://github.com/software-mansion/enriched-markdown/pull/961)) and hash-route link fragments ([mity/md4c#454](https://github.com/mity/md4c/pull/454), arriving through [md4c#7](https://github.com/software-mansion-labs/md4c/pull/7)), `md4cFlags.preserveWindowsPaths`, which keeps backslashes in Windows drive paths in prose ([#1105](https://github.com/software-mansion/enriched-markdown/pull/1105), after [mity/md4c#477](https://github.com/mity/md4c/pull/477)), GitHub's strikethrough rules for `~` ([md4c#6](https://github.com/software-mansion-labs/md4c/pull/6)), `\(…\)` and `\[…\]` math delimiters ([#1043](https://github.com/software-mansion/enriched-markdown/pull/1043), [md4c#4](https://github.com/software-mansion-labs/md4c/pull/4)), iOS Copy as Markdown keeping bold spans whole ([#1143](https://github.com/software-mansion/enriched-markdown/pull/1143)), line breaks in copied table cells written as `<br>` ([#1144](https://github.com/software-mansion/enriched-markdown/pull/1144)), and Android measuring text in the span order its view draws it, so code blocks nested in list items no longer make a reply overlap the next message ([#1145](https://github.com/software-mansion/enriched-markdown/pull/1145)). It also carries three fork-only changes. `writingDirection="word-majority"` sets each paragraph, list item, heading and table cell right to left when its right-to-left words outnumber its left-to-right ones. It counts words as the desktop client does, but decides per paragraph where desktop decides per top-level block (Android API 29+; older Android keeps first-strong). The other two come from T3 Code's fork: iOS code blocks too big for one backing store draw through tiles, so a tall block with long lines no longer shows an empty pane ([pingdotgg/enriched-markdown#15](https://github.com/pingdotgg/enriched-markdown/pull/15)), and a long-pressed iOS blockquote lifts on the background it sits on instead of drawing its text over the thread.
22

33
To replace it, commit the fork changes on `main` with the next `1.2.0-oni.N` version, run `yarn install --immutable` and `yarn workspace react-native-enriched-markdown prepare` in the fork checkout, then `yarn workspace react-native-enriched-markdown pack:onicode /absolute/path/to/onicode/apps/mobile/deps`. Tag the source commit `oni-v<version>` and push `main` and the tag to the fork. Rename the archive to include the first eight characters of the source commit, as in `react-native-enriched-markdown-<version>-<commit>.tgz`, so each build has a distinct dependency path. Update the tarball path in `apps/mobile/package.json` and `pnpm-workspace.yaml`'s `allowBuilds`, and run `vp i` to update the lockfile. Native clients need rebuilding when this dependency changes.
44

Binary file not shown.
Binary file not shown.

‎apps/mobile/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
"react": "catalog:",
105105
"react-dom": "catalog:",
106106
"react-native": "0.88.1",
107-
"react-native-enriched-markdown": "file:deps/react-native-enriched-markdown-1.2.0-oni.21-a423deee.tgz",
107+
"react-native-enriched-markdown": "file:deps/react-native-enriched-markdown-1.2.0-oni.22-0c421a6f.tgz",
108108
"react-native-gesture-handler": "~3.5.0",
109109
"react-native-image-viewing": "^0.2.2",
110110
"react-native-keyboard-controller": "1.23.0",

‎apps/mobile/src/features/threads/PendingUserInputCard.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ function QuestionText(props: {
109109
<SelectableMarkdownText
110110
markdown={markdown}
111111
textStyle={styles.nativeTextStyle}
112-
writingDirection={markdownProseDirection(markdown)}
112+
// Android maps an explicit direction to first-strong, so it keeps the word-majority default.
113+
writingDirection={Platform.OS === "android" ? undefined : markdownProseDirection(markdown)}
113114
// Android's renderer aligns relative to each paragraph's direction.
114115
textAlign={
115116
Platform.OS === "android" ? undefined : props.setDirection === "rtl" ? "right" : "left"

‎apps/mobile/src/lib/enrichedLinkPresentation.test.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ describe("Enriched link presentation", () => {
6868
"Try (model:codex/gpt-6.1).",
6969
"Try [model:codex/gpt-6.1] next",
7070
'"model:codex/gpt-6.1"',
71-
"‏model:codex/gpt-6.1 هذا شرح",
7271
]) {
7372
const source = regex?.exec(text)?.[0];
7473
expect(source, text).toBe("model:codex/gpt-6.1");

‎apps/mobile/src/lib/enrichedLinkPresentation.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,8 @@ export function enrichedModelLinkRegex(models?: ReadonlyArray<SelectableMarkdown
174174
.map((model) => model.source)
175175
.sort((a, b) => b.length - a.length)
176176
.map((source) => source.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
177-
// LRM and RLM can open a paragraph whose first word runs against the text direction.
178177
return new RegExp(
179-
`(?<![^\\s,(\\["'\\u200E\\u200F])(?:${alternatives.join("|")})(?=$|[\\s,"']|[.;:!?\\)\\]]+(?=$|[\\s,"']))`,
178+
`(?<![^\\s,(\\["'])(?:${alternatives.join("|")})(?=$|[\\s,"']|[.;:!?\\)\\]]+(?=$|[\\s,"']))`,
180179
"u",
181180
);
182181
}

‎apps/mobile/src/lib/markdownDocument.ts‎

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
parseMarkdownFileLink,
1010
} from "@onicode/shared/markdownLinks";
1111
import { isWindowsAbsolutePath } from "@onicode/shared/path";
12-
import { hasRightToLeftLetter, textDirection } from "@onicode/shared/textDirection";
1312

1413
const parser = unified().use(remarkParse).use(remarkGfm).use(remarkKeepWindowsProsePaths);
1514
export type MarkdownNode = Root | RootContent;
@@ -563,64 +562,3 @@ export function markdownWithFileEmbeds(
563562
parts.push(markdown.slice(offset));
564563
return { markdown: parts.join(""), fileHrefs };
565564
}
566-
567-
const DIRECTION_MARK = { ltr: "‎", rtl: "‏" } as const;
568-
569-
function proseText(node: MarkdownNode): string {
570-
if (node.type === "text") return node.value;
571-
if (node.type === "inlineCode" || node.type === "code") return " ";
572-
if (!("children" in node)) return "";
573-
const text = node.children.map(proseText).join("");
574-
return node.type === "paragraph" || node.type === "heading" || node.type === "tableCell"
575-
? ` ${text} `
576-
: text;
577-
}
578-
579-
// The native renderer scans inline code too when it picks a paragraph's direction.
580-
function renderedText(node: MarkdownNode): string {
581-
if (node.type === "text" || node.type === "inlineCode") return node.value;
582-
if (!("children" in node)) return "";
583-
return node.children.map(renderedText).join("");
584-
}
585-
586-
// A mark inside `[label]` or `<url>` would break the reference or autolink.
587-
function firstLeafOffset(node: MarkdownNode): number | undefined {
588-
if (
589-
!("children" in node) ||
590-
node.children.length === 0 ||
591-
node.type === "link" ||
592-
node.type === "linkReference"
593-
) {
594-
return node.position?.start.offset;
595-
}
596-
return firstLeafOffset(node.children[0]!);
597-
}
598-
599-
/**
600-
* The native renderer gives each paragraph the direction of its first letter, so an Arabic
601-
* line that opens with an English name renders left to right. Like the web's
602-
* `rehypeTextDirection`, each top-level block follows the majority of its words, and a
603-
* paragraph whose first letter disagrees gets a direction mark that the renderer reads first.
604-
*/
605-
export function markdownWithTextDirection(markdown: string): string {
606-
if (!hasRightToLeftLetter(markdown)) return markdown;
607-
const edits: MarkdownEdit[] = [];
608-
for (const block of parser.parse(markdown).children) {
609-
if (block.type === "code" || block.type === "html") continue;
610-
const direction = textDirection(proseText(block));
611-
if (!direction) continue;
612-
const mark = (node: MarkdownNode) => {
613-
if (node.type === "paragraph" || node.type === "heading" || node.type === "tableCell") {
614-
const offset = firstLeafOffset(node);
615-
const first = renderedText(node).match(/\p{L}/u)?.[0];
616-
if (offset !== undefined && first && textDirection(first) !== direction) {
617-
edits.push({ start: offset, end: offset, content: DIRECTION_MARK[direction] });
618-
}
619-
return;
620-
}
621-
if ("children" in node) node.children.forEach(mark);
622-
};
623-
mark(block);
624-
}
625-
return applyMarkdownEdits(markdown, edits);
626-
}

‎apps/mobile/src/lib/markdownTextDirection.test.ts‎

Lines changed: 0 additions & 71 deletions
This file was deleted.

‎apps/mobile/src/native/EnrichedMarkdownText.tsx‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
markdownForNativeParsing,
1111
markdownWithInlineFileLinks,
1212
markdownWithTableBreaks,
13-
markdownWithTextDirection,
1413
} from "../lib/markdownDocument";
1514
import { useGT } from "gt-react-native";
1615
import { isValidElement, useMemo, useState, type ReactElement, type ReactNode } from "react";
@@ -250,9 +249,7 @@ export function MobileEnrichedMarkdownText(props: SelectableMarkdownTextProps) {
250249
const { markdown, fileHrefs } = useMemo(() => {
251250
if (!fileEmbedLabel) {
252251
return {
253-
markdown: markdownWithTextDirection(
254-
markdownForNativeRendering(props.markdown, ENRICHED_INLINE_FILE_LINK_REGEX),
255-
),
252+
markdown: markdownForNativeRendering(props.markdown, ENRICHED_INLINE_FILE_LINK_REGEX),
256253
fileHrefs: new Set<string>(),
257254
};
258255
}
@@ -261,10 +258,7 @@ export function MobileEnrichedMarkdownText(props: SelectableMarkdownTextProps) {
261258
ENRICHED_INLINE_FILE_LINK_REGEX,
262259
);
263260
const files = markdownWithFileEmbeds(content, fileEmbedLabel);
264-
return {
265-
...files,
266-
markdown: markdownWithTextDirection(markdownWithTableBreaks(files.markdown)),
267-
};
261+
return { ...files, markdown: markdownWithTableBreaks(files.markdown) };
268262
}, [props.markdown, fileEmbedLabel]);
269263
const [parsedLinkUrls, setParsedLinkUrls] = useState<ReadonlyArray<string>>([]);
270264
const onLinksChange = (links: MarkdownLinkOccurrence[]) => {
@@ -440,7 +434,8 @@ export function MobileEnrichedMarkdownText(props: SelectableMarkdownTextProps) {
440434
<EnrichedMarkdownText
441435
markdown={markdown}
442436
markdownStyle={markdownStyle}
443-
writingDirection={props.writingDirection}
437+
// Each paragraph, item and cell goes the way most of its words read, as on desktop.
438+
writingDirection={props.writingDirection ?? "word-majority"}
444439
onLinksChange={onLinksChange}
445440
renderers={renderers}
446441
resolveImageSource={resolveNativeImageSource}

0 commit comments

Comments
 (0)