TypeScript
fix(mobile): preserve line breaks in Markdown table cells (#29355)
shivam9f2bb98a8508
diff --git a/apps/mobile/deps/README.md b/apps/mobile/deps/README.mdindex 939ac2d9147..67390ab1091 100644--- a/apps/mobile/deps/README.md+++ b/apps/mobile/deps/README.md@@ -1,3 +1,5 @@The Enriched Markdown tarball comes from Julius's [`fork/t3-mobile` branch](https://github.com/pingdotgg/enriched-markdown/tree/fork/t3-mobile). Its `t3-fork.json` records the exact source commit and upstream base. React block renderers and signed image sources (`renderers`, `resolveImageSource`) are upstream. The fork carries native streaming memory bounds (an 8 MiB measurement-key budget and coalesced pending renders), plus changes with an open upstream PR: the `onLinksChange` link list that link pills and iOS link menus read ([#1088](https://github.com/software-mansion/enriched-markdown/pull/1088)), Android inline image alignment ([#1089](https://github.com/software-mansion/enriched-markdown/pull/1089)), 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 `md4cFlags.preserveWindowsPaths`, which keeps backslashes in Windows drive paths in prose ([#1105](https://github.com/software-mansion/enriched-markdown/pull/1105), after [md4c#5](https://github.com/software-mansion-labs/md4c/pull/5)).To replace it, commit the fork changes, run `yarn install --immutable` and `yarn workspace react-native-enriched-markdown prepare` in the fork checkout, then `node scripts/pack-t3.mjs /path/to/t3code/apps/mobile/deps`. Keep the package version aligned with Julius's `fork/t3-mobile` branch. Rename the archive to include the first eight characters of the source commit, such as `react-native-enriched-markdown-1.2.0-t3.16-8d2f84a8.tgz`, so each build has a distinct dependency path without version bumps. 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.++`patches/[email protected]` writes a newline inside a copied table cell as `<br>`, so Copy as Markdown keeps the row whole. It is not in the fork yet; fold it into `fork/t3-mobile` or drop it when you replace the tarball, since pnpm applies it to any archive with the same version.diff --git a/apps/mobile/src/lib/markdownDocument.ts b/apps/mobile/src/lib/markdownDocument.tsindex c771813f176..23f6d5352c6 100644--- a/apps/mobile/src/lib/markdownDocument.ts+++ b/apps/mobile/src/lib/markdownDocument.ts@@ -229,8 +229,8 @@ function htmlTagMarkdown(const blockBreak = anchors.open === 0 ? "\n\n" : " ";if (/^<summary\b/i.test(tag)) return `${blockBreak}**`;if (/^<\/summary\s*>/i.test(tag)) return `**${blockBreak}`;- // A line break would end the table row, so a cell keeps its content on one line.- if (/^<br\b/i.test(tag)) return options.inTableCell ? " " : " \n";+ // A raw newline would end the table row; both parsers decode this reference to one inside the cell.+ if (/^<br\b/i.test(tag)) return options.inTableCell ? " " : " \n";if (/^<\/?(?:p|div|details)\b/i.test(tag)) return blockBreak;return "";}diff --git a/apps/mobile/src/lib/markdownHtml.test.ts b/apps/mobile/src/lib/markdownHtml.test.tsindex 911d6d46610..e7f5dd07e21 100644--- a/apps/mobile/src/lib/markdownHtml.test.ts+++ b/apps/mobile/src/lib/markdownHtml.test.ts@@ -85,19 +85,27 @@ describe("HTML in native Markdown", () => {expect(result).toContain('<img src="after.png">\n</details>');});- it("links HTML anchors in a table cell without breaking the row at <br>", () => {- const document = parseMarkdownDocument(- "| Status | Preview URL |\n| - | - |\n| Done | <a href='https://commit.example.com'>Commit</a><br><a href='https://branch.example.com'>Branch</a> |",+ it("keeps <br> line breaks in a table cell without breaking the row", () => {+ const markdown =+ "| Status | Preview URL |\n| - | - |\n| Done | <a href='https://commit.example.com'>Commit</a><br><a href='https://branch.example.com'>Branch</a> |\n| • One<br/>• Two<BR />**Three** | `<br>` |";+ // md4c decodes the reference to a newline in the cell's text, as remark does.+ expect(markdownForNativeRendering(markdown, ENRICHED_INLINE_FILE_LINK_REGEX)).toContain(+ "| • One • Two **Three** | `<br>` |",);- const table = document.children[0];+ const table = parseMarkdownDocument(markdown).children[0];expect(table?.type).toBe("table");if (table?.type !== "table") return;- expect(table.children).toHaveLength(2);+ expect(table.children).toHaveLength(3);+ expect(table.children.map((row) => row.children.length)).toEqual([2, 2, 2]);expect(table.children[1]?.children[1]?.children).toMatchObject([{ type: "link", url: "https://commit.example.com", children: [{ value: "Commit" }] },- { type: "text", value: " " },+ { type: "text", value: "\n" },{ type: "link", url: "https://branch.example.com", children: [{ value: "Branch" }] },]);+ expect(table.children[2]?.children).toMatchObject([+ { children: [{ value: "• One\n• Two\n" }, { type: "strong" }] },+ { children: [{ type: "inlineCode", value: "<br>" }] },+ ]);});it("keeps a table row intact when an anchor href contains a pipe", () => {@@ -121,7 +129,7 @@ describe("HTML in native Markdown", () => {const markdown ='> **!<a href="https://x.test">[one]</a>** and <span>tail</span>\n\n| A |\n| - |\n| !<a href="https://x.test">x[y]</a><br><img src="x.png"> |';expect(markdownWithHtml(markdown)).toBe(- "> **\\![\\[one\\]](<https://x.test>)** and tail\n\n| A |\n| - |\n| \\![x\\[y\\]](<https://x.test>)  |",+ "> **\\![\\[one\\]](<https://x.test>)** and tail\n\n| A |\n| - |\n| \\![x\\[y\\]](<https://x.test>)  |",);expect(markdownWithHtml(diff --git a/patches/[email protected] b/patches/[email protected]new file mode 100644index 00000000000..2b5d8eecdd3--- /dev/null+++ b/patches/[email protected]@@ -0,0 +1,92 @@+diff --git a/android/src/main/java/com/swmansion/enriched/markdown/segments/TableContainerView.kt b/android/src/main/java/com/swmansion/enriched/markdown/segments/TableContainerView.kt+--- a/android/src/main/java/com/swmansion/enriched/markdown/segments/TableContainerView.kt++++ b/android/src/main/java/com/swmansion/enriched/markdown/segments/TableContainerView.kt+@@ -131,7 +131,7 @@ class TableContainerView(+ TableCellData(+ attributedText = renderCellNode(cell, isHeader, align),+ plainText = extractPlainText(cell),+- markdownText = MarkdownASTSerializer.serializeChildren(cell),++ markdownText = MarkdownASTSerializer.serializeTableCell(cell),+ isHeader = isHeader,+ alignment = align,+ )+diff --git a/android/src/main/java/com/swmansion/enriched/markdown/utils/common/serialization/MarkdownASTSerializer.kt b/android/src/main/java/com/swmansion/enriched/markdown/utils/common/serialization/MarkdownASTSerializer.kt+--- a/android/src/main/java/com/swmansion/enriched/markdown/utils/common/serialization/MarkdownASTSerializer.kt++++ b/android/src/main/java/com/swmansion/enriched/markdown/utils/common/serialization/MarkdownASTSerializer.kt+@@ -20,6 +20,12 @@ object MarkdownASTSerializer {+ return buffer.toString()+ }+++ private val tableCellLineBreak = Regex("""(?<!\\)(\\*)\n""")++++ /** Serializes a table cell's content; a newline inside it becomes `<br>` so the row stays on one line. */++ fun serializeTableCell(cell: MarkdownASTNode): String =++ serializeChildren(cell).replace(tableCellLineBreak, "$1$1<br>")+++ /**+ * Serializes a Blockquote or Admonition node back to markdown, including+ * the `> ` prefix on every line and `> [!TYPE]` header for admonitions.+@@ -179,7 +185,7 @@ object MarkdownASTSerializer {+ for (section in node.children) {+ for (row in section.children) {+ if (row.type != NodeType.TableRow) continue+- val cells = row.children.map { serializeChildren(it) }++ val cells = row.children.map { serializeTableCell(it) }+ buffer.append("| ")+ buffer.append(cells.joinToString(" | "))+ buffer.append(" |\n")+diff --git a/ios/segments/TableContainerView.m b/ios/segments/TableContainerView.m+--- a/ios/segments/TableContainerView.m++++ b/ios/segments/TableContainerView.m+@@ -163,7 +163,7 @@ static NSArray<NSArray<TableCellData *> *> *ENRMTableBuildRows(MarkdownASTNode *+ cell.isHeader = isSectionHead || (cellNode.type == MarkdownNodeTypeTableHeaderCell);+ cell.alignment = ENRMTableTextAlignmentFromString(cellNode.attributes[@"align"]);+ cell.plainText = ENRMTablePlainTextFromNode(cellNode);+- cell.markdownText = markdownFromASTNodeChildren(cellNode);++ cell.markdownText = markdownFromTableCellNode(cellNode);++ StyleConfig *cellConfig = cell.isHeader ? headerCellConfig : bodyCellConfig;+ cell.attributedText =+diff --git a/ios/utils/MarkdownASTSerializer.h b/ios/utils/MarkdownASTSerializer.h+--- a/ios/utils/MarkdownASTSerializer.h++++ b/ios/utils/MarkdownASTSerializer.h+@@ -17,6 +17,9 @@ NSString *markdownFromASTNode(MarkdownASTNode *node);+ /// Useful for extracting the markdown content of a container node (e.g. a table cell).+ NSString *markdownFromASTNodeChildren(MarkdownASTNode *node);+++/// Serializes a table cell's content; a newline inside it becomes `<br>` so the row stays on one line.++NSString *markdownFromTableCellNode(MarkdownASTNode *node);+++ /// Serializes a Blockquote or Admonition node to full markdown, including `> ` line+ /// prefixes and `> [!TYPE]` header for admonitions.+ NSString *markdownFromBlockquoteNode(MarkdownASTNode *node);+diff --git a/ios/utils/MarkdownASTSerializer.m b/ios/utils/MarkdownASTSerializer.m+--- a/ios/utils/MarkdownASTSerializer.m++++ b/ios/utils/MarkdownASTSerializer.m+@@ -148,7 +148,7 @@ static void appendTableMarkdown(MarkdownASTNode *node, NSMutableString *buffer)+ continue;+ NSMutableArray<NSString *> *cells = [NSMutableArray arrayWithCapacity:row.children.count];+ for (MarkdownASTNode *cell in row.children) {+- [cells addObject:markdownFromASTNodeChildren(cell)];++ [cells addObject:markdownFromTableCellNode(cell)];+ }+ [buffer appendFormat:@"| %@ |\n", [cells componentsJoinedByString:@" | "]];+ if (!headerDone) {+@@ -333,6 +333,17 @@ NSString *markdownFromASTNodeChildren(MarkdownASTNode *node)+ return [buffer copy];+ }+++NSString *markdownFromTableCellNode(MarkdownASTNode *node)++{++ NSString *markdown = markdownFromASTNodeChildren(node);++ if (![markdown containsString:@"\n"])++ return markdown;++ return [markdown stringByReplacingOccurrencesOfString:@"(?<!\\\\)(\\\\*)\n"++ withString:@"$1$1<br>"++ options:NSRegularExpressionSearch++ range:NSMakeRange(0, markdown.length)];++}+++ NSString *markdownFromBlockquoteNode(MarkdownASTNode *node)+ {+ if (!node)diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 0c2b6927c87..f56b9fb3f0a 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -382,6 +382,7 @@ patchedDependencies:[email protected]: 4eeb9d9e27ebc75e98795e21c0719005483dad7d233055698257f7d10e91603b[email protected]: cb79edde7eca83bbcaaafb6ceb7b044b81b987314cde4910823b15257ecada67[email protected]: f1e680afec85f650699ea756f91f469844ec4c976f4f168492a45c6b3f67d651+ [email protected]: 542e6932066897941f54b902fa7823b2a1861c4a741b5369d5c2d49bd18485a2[email protected]: abe20e1d1c23549297cbbc818e14fe479b41455930d9663135d6b40fe359b626[email protected]: 4aa038d541c7ddcc8f1437eb3e7b4f769f61de78e7f91b53f9805dfca608474d[email protected]: 4e9b5a52aa8c1f9de8082bad281ee979df7860fc77ec49820d4ed00c3c7cc5a2@@ -736,7 +737,7 @@ importers:version: 0.25.1([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])react-native-enriched-markdown:specifier: file:deps/react-native-enriched-markdown-1.2.0-t3.16-29ddd424.tgz- version: file:apps/mobile/deps/react-native-enriched-markdown-1.2.0-t3.16-29ddd424.tgz([email protected])([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])+ version: file:apps/mobile/deps/react-native-enriched-markdown-1.2.0-t3.16-29ddd424.tgz(patch_hash=542e6932066897941f54b902fa7823b2a1861c4a741b5369d5c2d49bd18485a2)([email protected])([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])react-native-gesture-handler:specifier: 3.3.0version: 3.3.0(patch_hash=abe20e1d1c23549297cbbc818e14fe479b41455930d9663135d6b40fe359b626)([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])@@ -21248,7 +21249,7 @@ snapshots:react: 19.3.0react-native: 0.88.0(@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected])- react-native-enriched-markdown@file:apps/mobile/deps/react-native-enriched-markdown-1.2.0-t3.16-29ddd424.tgz([email protected])([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected]):+ react-native-enriched-markdown@file:apps/mobile/deps/react-native-enriched-markdown-1.2.0-t3.16-29ddd424.tgz(patch_hash=542e6932066897941f54b902fa7823b2a1861c4a741b5369d5c2d49bd18485a2)([email protected])([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected]):dependencies:react: 19.3.0react-native: 0.88.0(@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected])diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yamlindex 95d8b2d57e0..d64fc1a679f 100644--- a/pnpm-workspace.yaml+++ b/pnpm-workspace.yaml@@ -262,6 +262,8 @@ patchedDependencies:# Observe the owned host exit independently of descendants retaining its stdio."@muse-code/[email protected]": patches/@[email protected][email protected]: patches/[email protected]+ # A newline in a copied table cell becomes <br>, so Copy as Markdown keeps the row whole.+ [email protected]: patches/[email protected]# A failed Artifacts.cached effect hangs every later caller: https://github.com/alchemy-run/alchemy/issues/2981[email protected]: patches/[email protected]