Skip to content

Commit 3d1216e

Browse files
committed
fix(mobile): preserve native Markdown syntax when seeding bidi
1 parent 2c8184c commit 3d1216e

2 files changed

Lines changed: 24 additions & 5 deletions

File tree

‎apps/web/src/textDirection.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ it("reads only the visible preview prose without losing truncated code boundarie
9191
});
9292

9393
it("seeds native paragraphs without changing Markdown block structure or code", () => {
94+
expect(markdownWithDirection("[!NOTE]", "rtl")).toBe("\u200f[!NOTE]");
95+
const rich =
96+
"| Name | Value |\n| --- | --- |\n| React | עברית |\n\n- [ ] React שינוי\n\n> [!NOTE]\n> React עדכון\n\n$$\nx^2\n$$\n\nReact המשך";
97+
expect(markdownWithDirection(rich, "rtl")).toBe(
98+
"| Name | Value |\n| --- | --- |\n| React | עברית |\n\n- [ ] \u200fReact שינוי\n\n> [!NOTE]\n> \u200fReact עדכון\n\n$$\nx^2\n$$\n\n\u200fReact המשך",
99+
);
94100
const markdown = "# React עדכון\n\n- React שינוי\n\n> English ציטוט\n\n```ts\nfoo()\n```";
95101
expect(markdownWithDirection(markdown, "rtl")).toBe(
96102
"# \u200fReact עדכון\n\n- \u200fReact שינוי\n\n> \u200fEnglish ציטוט\n\n```ts\nfoo()\n```",

‎packages/shared/src/textDirection.ts‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { Root, RootContent } from "mdast";
22
import remarkParse from "remark-parse";
3+
import remarkGfm from "remark-gfm";
4+
import remarkMath from "remark-math";
35
import { unified } from "unified";
46

57
export type TextDirection = "ltr" | "rtl";
@@ -32,6 +34,7 @@ export function textDirection(text: string): TextDirection | undefined {
3234
}
3335

3436
const markdownParser = unified().use(remarkParse).freeze();
37+
const nativeMarkdownParser = unified().use(remarkParse).use(remarkGfm).use(remarkMath).freeze();
3538

3639
function maskTextRanges(
3740
text: string,
@@ -165,14 +168,24 @@ export function markdownPreview(markdown: string, maxChars: number) {
165168
export function markdownWithDirection(markdown: string, direction: TextDirection | undefined) {
166169
if (!direction) return markdown;
167170
const offsets: number[] = [];
168-
const visit = (node: Root | RootContent): void => {
171+
const visit = (node: Root | RootContent, parent?: Root | RootContent): void => {
169172
if (node.type === "paragraph" || node.type === "heading") {
170-
const offset = node.children[0]?.position?.start.offset;
171-
if (offset !== undefined) offsets.push(offset);
173+
let offset = node.children[0]?.position?.start.offset;
174+
if (offset !== undefined) {
175+
// Keep the native admonition marker at the start of its quote line.
176+
const alert =
177+
parent?.type === "blockquote" && parent.children[0] === node
178+
? /^\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\][\t ]*(?:\r?\n[\t ]*(?:>[\t ]*)*|$)/i.exec(
179+
markdown.slice(offset, node.position?.end.offset),
180+
)
181+
: null;
182+
if (alert) offset += alert[0].length;
183+
if (offset < (node.position?.end.offset ?? markdown.length)) offsets.push(offset);
184+
}
172185
}
173-
if ("children" in node) node.children.forEach(visit);
186+
if ("children" in node) node.children.forEach((child) => visit(child, node));
174187
};
175-
visit(markdownParser.parse(markdown));
188+
visit(nativeMarkdownParser.parse(markdown));
176189
const mark = direction === "rtl" ? "\u200f" : "\u200e";
177190
for (const offset of offsets.reverse()) {
178191
markdown = markdown.slice(0, offset) + mark + markdown.slice(offset);

0 commit comments

Comments
 (0)