Skip to content

Commit ffb875d

Browse files
fix(mobile): keep source-offset and index markdown keys apart
A node with beg: 1 and an offset-free sibling at index 1 produced the same React key, and every offset-free selectable group shared one key. Offsets and fallback indices now live in separate namespaces, and an offset-free selectable group is keyed by its starting child index.
1 parent 8e10335 commit ffb875d

3 files changed

Lines changed: 112 additions & 11 deletions

File tree

‎apps/mobile/modules/t3-markdown-text/src/NativeMarkdownBlock.tsx‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import type { MarkdownNode } from "react-native-nitro-markdown/headless";
44

55
import { CopyTextButton } from "./CopyTextButton";
66
import { MarkdownTextPrimitive } from "./MarkdownTextPrimitive";
7-
import { nativeMarkdownDocumentRuns, nativeMarkdownListItemBlocks } from "./nativeMarkdownText";
7+
import {
8+
nativeMarkdownDocumentRuns,
9+
nativeMarkdownListItemBlocks,
10+
nativeMarkdownNodePosition,
11+
} from "./nativeMarkdownText";
812
import { pendingCodeHighlight } from "./pendingCodeHighlight";
913
import { NativeMarkdownSelectableText } from "./NativeMarkdownSelectableText";
1014
import type {
@@ -30,7 +34,7 @@ const MONO_FONT_FAMILY = Platform.select({
3034
});
3135

3236
function nodeKey(node: MarkdownNode, index: number): string {
33-
return `${node.type}:${node.beg ?? index}`;
37+
return `${node.type}:${nativeMarkdownNodePosition(node, index)}`;
3438
}
3539

3640
/** Code inside markdown scales with the base text size (12pt at the default 15pt body). */

‎apps/mobile/modules/t3-markdown-text/src/nativeMarkdownText.ts‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -694,20 +694,31 @@ function containsRichBlock(node: MarkdownNode): boolean {
694694
return (node.children ?? []).some(containsRichBlock);
695695
}
696696

697+
/**
698+
* Sibling identity for React keys. A source offset survives appends while the
699+
* document streams; the child index is the fallback for offset-free nodes. The
700+
* two never share a namespace, so a positioned node cannot collide with an
701+
* offset-free sibling whose index happens to equal its offset.
702+
*/
703+
export function nativeMarkdownNodePosition(node: MarkdownNode, index: number): string {
704+
return node.beg === undefined ? `index:${index}` : `offset:${node.beg}`;
705+
}
706+
697707
export function nativeMarkdownDocumentChunks(
698708
document: MarkdownNode,
699709
): ReadonlyArray<NativeMarkdownDocumentChunk> {
700710
const chunks: NativeMarkdownDocumentChunk[] = [];
701711
let selectableNodes: MarkdownNode[] = [];
712+
let selectableStart = 0;
702713

703714
const flushSelectable = () => {
704-
if (selectableNodes.length === 0) {
715+
const first = selectableNodes[0];
716+
if (!first) {
705717
return;
706718
}
707-
const first = selectableNodes[0];
708719
chunks.push({
709720
kind: "selectable",
710-
key: `selectable:${first?.beg ?? "start"}`,
721+
key: `selectable:${nativeMarkdownNodePosition(first, selectableStart)}`,
711722
node: {
712723
type: "document",
713724
children: selectableNodes,
@@ -718,14 +729,17 @@ export function nativeMarkdownDocumentChunks(
718729

719730
for (const [index, child] of (document.children ?? []).entries()) {
720731
if (!containsRichBlock(child)) {
732+
if (selectableNodes.length === 0) {
733+
selectableStart = index;
734+
}
721735
selectableNodes.push(child);
722736
continue;
723737
}
724738

725739
flushSelectable();
726740
chunks.push({
727741
kind: "rich",
728-
key: `rich:${child.type}:${child.beg ?? index}`,
742+
key: `rich:${child.type}:${nativeMarkdownNodePosition(child, index)}`,
729743
node: child,
730744
});
731745
}

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

Lines changed: 88 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ describe("nativeMarkdownDocumentChunks", () => {
536536
).toEqual([
537537
{
538538
kind: "rich",
539-
key: "rich:blockquote:0",
539+
key: "rich:blockquote:offset:0",
540540
node: blockquote,
541541
},
542542
]);
@@ -663,7 +663,7 @@ describe("nativeMarkdownDocumentChunks", () => {
663663
expect(chunks[0]).toMatchObject({ kind: "selectable" });
664664
expect(chunks[1]).toEqual({
665665
kind: "rich",
666-
key: "rich:code_block:11",
666+
key: "rich:code_block:offset:11",
667667
node: document.children?.[1],
668668
});
669669
expect(chunks[2]).toMatchObject({ kind: "selectable" });
@@ -700,7 +700,7 @@ describe("nativeMarkdownDocumentChunks", () => {
700700
expect(nativeMarkdownDocumentChunks(document)).toEqual([
701701
{
702702
kind: "rich",
703-
key: "rich:list:0",
703+
key: "rich:list:offset:0",
704704
node: document.children?.[0],
705705
},
706706
]);
@@ -728,7 +728,7 @@ describe("nativeMarkdownDocumentChunks", () => {
728728
expect(chunks[0]).toMatchObject({ kind: "selectable" });
729729
expect(chunks[1]).toEqual({
730730
kind: "rich",
731-
key: "rich:horizontal_rule:1",
731+
key: "rich:horizontal_rule:index:1",
732732
node: document.children?.[1],
733733
});
734734
expect(chunks[2]).toMatchObject({ kind: "selectable" });
@@ -774,7 +774,7 @@ describe("nativeMarkdownDocumentChunks", () => {
774774
expect(chunks[0]).toMatchObject({ kind: "selectable" });
775775
expect(chunks[1]).toEqual({
776776
kind: "rich",
777-
key: "rich:list:1",
777+
key: "rich:list:index:1",
778778
node: document.children?.[1],
779779
});
780780
expect(chunks[2]).toMatchObject({ kind: "selectable" });
@@ -824,6 +824,89 @@ describe("nativeMarkdownDocumentChunks", () => {
824824
});
825825
});
826826

827+
it("keys positioned and offset-free siblings in separate namespaces", () => {
828+
const positioned: MarkdownNode = {
829+
type: "blockquote",
830+
beg: 1,
831+
end: 10,
832+
children: [{ type: "paragraph", children: [{ type: "text", content: "Positioned" }] }],
833+
};
834+
const offsetFree: MarkdownNode = {
835+
type: "blockquote",
836+
children: [{ type: "paragraph", children: [{ type: "text", content: "Generated" }] }],
837+
};
838+
const chunks = nativeMarkdownDocumentChunks({
839+
type: "document",
840+
children: [
841+
{ type: "paragraph", beg: 0, end: 0, children: [] },
842+
offsetFree,
843+
positioned,
844+
{ type: "paragraph", children: [{ type: "text", content: "Tail" }] },
845+
],
846+
});
847+
848+
expect(chunks.map((chunk) => chunk.key)).toEqual([
849+
"selectable:offset:0",
850+
"rich:blockquote:index:1",
851+
"rich:blockquote:offset:1",
852+
"selectable:index:3",
853+
]);
854+
});
855+
856+
it("gives every offset-free selectable group its own key", () => {
857+
const chunks = nativeMarkdownDocumentChunks({
858+
type: "document",
859+
children: [
860+
{ type: "paragraph", children: [{ type: "text", content: "One" }] },
861+
{ type: "horizontal_rule" },
862+
{ type: "paragraph", children: [{ type: "text", content: "Two" }] },
863+
{ type: "horizontal_rule" },
864+
{ type: "paragraph", children: [{ type: "text", content: "Three" }] },
865+
],
866+
});
867+
868+
expect(chunks.map((chunk) => chunk.key)).toEqual([
869+
"selectable:index:0",
870+
"rich:horizontal_rule:index:1",
871+
"selectable:index:2",
872+
"rich:horizontal_rule:index:3",
873+
"selectable:index:4",
874+
]);
875+
});
876+
877+
it("keeps positioned chunk keys stable while text streams in", () => {
878+
const before: MarkdownNode = {
879+
type: "document",
880+
children: [
881+
{ type: "paragraph", beg: 0, end: 5, children: [{ type: "text", content: "Intro" }] },
882+
{
883+
type: "code_block",
884+
language: "ts",
885+
beg: 7,
886+
end: 20,
887+
children: [{ type: "text", content: "const a" }],
888+
},
889+
],
890+
};
891+
const after: MarkdownNode = {
892+
type: "document",
893+
children: [
894+
{ type: "paragraph", beg: 0, end: 5, children: [{ type: "text", content: "Intro" }] },
895+
{
896+
type: "code_block",
897+
language: "ts",
898+
beg: 7,
899+
end: 40,
900+
children: [{ type: "text", content: "const a = 1;\nconst b" }],
901+
},
902+
],
903+
};
904+
905+
expect(nativeMarkdownDocumentChunks(after).map((chunk) => chunk.key)).toEqual(
906+
nativeMarkdownDocumentChunks(before).map((chunk) => chunk.key),
907+
);
908+
});
909+
827910
it("keeps a plain list in one selectable native text container", () => {
828911
const list: MarkdownNode = {
829912
type: "list",

0 commit comments

Comments
 (0)