Skip to content

Commit 340cab4

Browse files
Merge main into fix/issue-24498
2 parents 1e2fe07 + bf5c483 commit 340cab4

11 files changed

Lines changed: 156 additions & 72 deletions

‎apps/mobile/deps/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
The Enriched Markdown tarball comes from [`feat/t3chat-markdown-parity-v2`](https://github.com/pingdotgg/enriched-markdown/tree/feat/t3chat-markdown-parity-v2), based on 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. File/skill link recognition, back navigation from horizontal blocks, table-image presses, and code-block headers use upstream implementations. The fork retains clipboard metadata, parsed asset manifests, signed image source resolution, React media slots, and development-host autolinks. The fork retains native math parsing, scrolling and copying, table layout and gestures, and inline-code layout. Character-reference decoding, iOS intrinsic-width measurement, and HTML clipboard round trips now use the upstream fixes included in Julius's base. RaTeX uses the upstream-released 0.1.15 dependency.
1+
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 only 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)), and development-host autolinks ([#961](https://github.com/software-mansion/enriched-markdown/pull/961)).
22

3-
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.15-fee84114.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.
3+
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.
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
@@ -127,7 +127,7 @@
127127
"react-dom": "catalog:",
128128
"react-native": "0.88.0",
129129
"react-native-blob-util": "0.25.1",
130-
"react-native-enriched-markdown": "file:deps/react-native-enriched-markdown-1.2.0-t3.15-fee84114.tgz",
130+
"react-native-enriched-markdown": "file:deps/react-native-enriched-markdown-1.2.0-t3.16-8d2f84a8.tgz",
131131
"react-native-gesture-handler": "~3.3.0",
132132
"react-native-image-viewing": "^0.2.2",
133133
"react-native-keyboard-controller": "1.22.7",

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

Lines changed: 48 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,20 @@ import {
99
markdownWithInlineFileLinks,
1010
markdownWithTableBreaks,
1111
} from "../lib/markdownDocument";
12-
import { useCallback, useMemo, useState, type ReactNode } from "react";
12+
import { isValidElement, useCallback, useMemo, useState, type ReactNode } from "react";
1313
import { Platform, Share, StyleSheet, View } from "react-native";
1414
import {
1515
EnrichedMarkdownText,
16-
type DocumentAssetsEvent,
16+
type MarkdownImageBlock,
17+
type MarkdownLinkOccurrence,
1718
type MarkdownStyle,
18-
type MarkdownMediaAsset,
1919
} from "react-native-enriched-markdown";
2020
import type { SelectableMarkdownTextProps } from "./SelectableMarkdownText.types";
2121

2222
import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
2323
import { Menu } from "../components/ui/Menu";
2424
import { useEnrichedLinkVariants } from "./useEnrichedLinkVariants";
25+
import { useRetryingImageSource } from "./useRetryingImageSource";
2526
import { copyTextWithHaptic } from "../lib/copyTextWithHaptic";
2627
import {
2728
ENRICHED_INLINE_FILE_LINK_REGEX,
@@ -164,33 +165,34 @@ export function MobileEnrichedMarkdownText(props: SelectableMarkdownTextProps) {
164165
),
165166
[props.markdown],
166167
);
167-
const [documentAssets, setDocumentAssets] = useState<DocumentAssetsEvent["assets"]>([]);
168-
const onDocumentAssets = useCallback(({ assets }: DocumentAssetsEvent) => {
169-
setDocumentAssets((previous) =>
170-
previous.length === assets.length &&
171-
previous.every((asset, index) => {
172-
const next = assets[index]!;
173-
return (
174-
asset.id === next.id &&
175-
asset.kind === next.kind &&
176-
asset.url === next.url &&
177-
asset.altText === next.altText &&
178-
asset.title === next.title &&
179-
asset.placement === next.placement &&
180-
asset.eligible === next.eligible
181-
);
182-
})
168+
const [parsedLinkUrls, setParsedLinkUrls] = useState<ReadonlyArray<string>>([]);
169+
const onLinksChange = useCallback((links: MarkdownLinkOccurrence[]) => {
170+
const urls = [...new Set(links.map((link) => link.url))];
171+
setParsedLinkUrls((previous) =>
172+
previous.length === urls.length && previous.every((url, index) => url === urls[index])
183173
? previous
184-
: assets,
174+
: urls,
185175
);
186176
}, []);
187-
const { linkVariants, linkPillContent } = useEnrichedLinkVariants(documentAssets, props);
188-
const linkRecognition = useMemo(
189-
() => ({
190-
text: enrichedSkillLinkRegex(props.skills) ?? undefined,
191-
}),
192-
[props.skills],
177+
const skillRegex = useMemo(() => enrichedSkillLinkRegex(props.skills), [props.skills]);
178+
// Skill links come from linkRecognition, which runs as the text is drawn, so
179+
// onLinksChange never reports them. Joined so a streaming tick keeps the value.
180+
const skillLinkKey = useMemo(
181+
() =>
182+
skillRegex
183+
? [...new Set(markdown.match(new RegExp(skillRegex.source, "gu")) ?? [])].join("\n")
184+
: "",
185+
[markdown, skillRegex],
186+
);
187+
const linkUrls = useMemo(
188+
() =>
189+
skillLinkKey
190+
? [...new Set([...parsedLinkUrls, ...skillLinkKey.split("\n")])]
191+
: parsedLinkUrls,
192+
[parsedLinkUrls, skillLinkKey],
193193
);
194+
const { linkVariants, linkPillContent } = useEnrichedLinkVariants(linkUrls, props);
195+
const linkRecognition = useMemo(() => ({ text: skillRegex ?? undefined }), [skillRegex]);
194196
const [activeMenu, setActiveMenu] = useState<{
195197
url: string;
196198
menu: NonNullable<ReturnType<NonNullable<typeof props.fileContextMenu>>>;
@@ -209,11 +211,7 @@ export function MobileEnrichedMarkdownText(props: SelectableMarkdownTextProps) {
209211
() =>
210212
Platform.OS === "ios" && (fileContextMenu || onLinkPress)
211213
? Object.fromEntries(
212-
[
213-
...new Set(
214-
documentAssets.filter((asset) => asset.kind === "link").map((asset) => asset.url),
215-
),
216-
].map((url) => {
214+
linkUrls.map((url) => {
217215
const menu = fileContextMenu?.(url) ?? {
218216
title: url,
219217
actions: [
@@ -233,7 +231,7 @@ export function MobileEnrichedMarkdownText(props: SelectableMarkdownTextProps) {
233231
}),
234232
)
235233
: undefined,
236-
[documentAssets, fileContextMenu, onLinkPress, performMenuAction],
234+
[linkUrls, fileContextMenu, onLinkPress, performMenuAction],
237235
);
238236
const selectionClipboard = useMemo(() => {
239237
const fragment = decodeComposerContextFragment(props.contextClipboardFragment);
@@ -264,24 +262,23 @@ export function MobileEnrichedMarkdownText(props: SelectableMarkdownTextProps) {
264262
() => ({ ...enrichedStyle(props.textStyle, themeAppearance === "dark"), linkVariants }),
265263
[props.textStyle, themeAppearance, linkVariants],
266264
);
267-
const renderMedia = useCallback(
268-
(asset: MarkdownMediaAsset) =>
269-
renderImage?.({
270-
href: asset.url,
271-
alt: asset.altText || null,
272-
title: asset.title || null,
273-
}) ?? null,
265+
const renderers = useMemo(
266+
() =>
267+
renderImage
268+
? {
269+
image: (block: MarkdownImageBlock) => {
270+
const element = renderImage({
271+
href: block.url,
272+
alt: block.altText || null,
273+
title: block.title || null,
274+
});
275+
return isValidElement(element) ? element : undefined;
276+
},
277+
}
278+
: undefined,
274279
[renderImage],
275280
);
276-
const resolveNativeImageSource = useCallback(
277-
(asset: MarkdownMediaAsset) =>
278-
resolveImageSource?.({
279-
href: asset.url,
280-
alt: asset.altText || null,
281-
title: asset.title || null,
282-
}) ?? null,
283-
[resolveImageSource],
284-
);
281+
const resolveNativeImageSource = useRetryingImageSource(resolveImageSource);
285282
const renderMarkdown = (openAndroidMenu?: () => void) => {
286283
const onLinkLongPress =
287284
Platform.OS === "android" && props.fileContextMenu
@@ -310,9 +307,9 @@ export function MobileEnrichedMarkdownText(props: SelectableMarkdownTextProps) {
310307
<EnrichedMarkdownText
311308
markdown={markdown}
312309
markdownStyle={markdownStyle}
313-
onDocumentAssets={onDocumentAssets}
314-
renderMedia={renderImage ? renderMedia : undefined}
315-
resolveImageSource={resolveImageSource ? resolveNativeImageSource : undefined}
310+
onLinksChange={onLinksChange}
311+
renderers={renderers}
312+
resolveImageSource={resolveNativeImageSource}
316313
onImagePress={
317314
props.onImagePress || props.onLinkPress
318315
? ({ url }) => (props.onImagePress ?? props.onLinkPress)?.(url)

‎apps/mobile/src/native/useEnrichedLinkVariants.ts‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { useMemo } from "react";
22
import { Image } from "react-native";
3-
import type {
4-
LinkPillContent,
5-
MarkdownDocumentAsset,
6-
MarkdownStyle,
7-
} from "react-native-enriched-markdown";
3+
import type { LinkPillContent, MarkdownStyle } from "react-native-enriched-markdown";
84
import type { ComposerContextRecord } from "@t3tools/contracts";
95
import { contextChipAccent, contextChipPresentation } from "../lib/composerChipPresentation";
106
import { CONTEXT_CHIP_ICON_SOURCES } from "../lib/contextChipIcons.generated";
@@ -94,20 +90,20 @@ function presentation(
9490
}
9591

9692
export function useEnrichedLinkVariants(
97-
assets: ReadonlyArray<MarkdownDocumentAsset>,
93+
urls: ReadonlyArray<string>,
9894
props: SelectableMarkdownTextProps,
9995
) {
10096
const { linkCustomization, skills, textStyle } = props;
10197
const theme = useUniwindTheme();
10298
const links = useMemo(() => {
10399
const byUrl = new Map<string, MarkdownLinkCustomization>();
104-
for (const asset of assets) {
105-
if (asset.kind !== "link" || byUrl.has(asset.url)) continue;
106-
const value = presentation(asset.url, { linkCustomization, skills, textStyle });
107-
if (value) byUrl.set(asset.url, value);
100+
for (const url of urls) {
101+
if (byUrl.has(url)) continue;
102+
const value = presentation(url, { linkCustomization, skills, textStyle });
103+
if (value) byUrl.set(url, value);
108104
}
109105
return byUrl;
110-
}, [assets, linkCustomization, skills, textStyle]);
106+
}, [urls, linkCustomization, skills, textStyle]);
111107
const chipForegrounds = useMemo(
112108
() =>
113109
new Map(
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { act, useLayoutEffect } from "react";
2+
import { createRoot } from "react-dom/client";
3+
import { expect, it } from "vite-plus/test";
4+
import type { MarkdownImageSourceResolver } from "./SelectableMarkdownText.types";
5+
import { useRetryingImageSource } from "./useRetryingImageSource";
6+
7+
let resolveImage: ReturnType<typeof useRetryingImageSource>;
8+
9+
function Probe(props: { readonly resolve: MarkdownImageSourceResolver }) {
10+
const resolver = useRetryingImageSource(props.resolve);
11+
useLayoutEffect(() => {
12+
resolveImage = resolver;
13+
});
14+
return null;
15+
}
16+
17+
it("asks the next resolver for an image the previous one could not resolve", async () => {
18+
const root = createRoot(document.createElement("div"));
19+
const offline: MarkdownImageSourceResolver = async () => {
20+
throw new Error("Markdown image is unavailable");
21+
};
22+
await act(async () => root.render(<Probe resolve={offline} />));
23+
const askedOnce = resolveImage!;
24+
25+
let source: unknown = "pending";
26+
const answered = askedOnce({
27+
id: "img:0:0",
28+
url: "assets/shot.png",
29+
altText: "",
30+
title: "",
31+
placement: "inline",
32+
}).then((value) => {
33+
source = value;
34+
});
35+
await act(async () => {});
36+
expect(source).toBe("pending");
37+
38+
const online: MarkdownImageSourceResolver = async (image) => ({
39+
uri: `https://host/${image.href}`,
40+
});
41+
await act(async () => root.render(<Probe resolve={online} />));
42+
await answered;
43+
44+
expect(source).toEqual({ uri: "https://host/assets/shot.png" });
45+
expect(resolveImage).toBe(askedOnce);
46+
act(() => root.unmount());
47+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { useLayoutEffect, useMemo, useRef } from "react";
2+
import type { MarkdownImageOccurrence, MarkdownImageSource } from "react-native-enriched-markdown";
3+
import type { MarkdownImageSourceResolver } from "./SelectableMarkdownText.types";
4+
5+
/**
6+
* Enriched asks about each image once and reads `resolveImageSource` like an
7+
* event handler, so a failed image would keep its placeholder until remount.
8+
* Our resolver fails while the environment is unreachable and is replaced when
9+
* that changes, so a failed image stays pending and asks the next resolver.
10+
*/
11+
export function useRetryingImageSource(resolve: MarkdownImageSourceResolver | undefined) {
12+
const latestRef = useRef(resolve);
13+
const retriesRef = useRef(new Set<() => void>());
14+
useLayoutEffect(() => {
15+
latestRef.current = resolve;
16+
const retries = [...retriesRef.current];
17+
retriesRef.current.clear();
18+
for (const retry of retries) retry();
19+
}, [resolve]);
20+
21+
const enabled = resolve !== undefined;
22+
return useMemo(() => {
23+
if (!enabled) return undefined;
24+
return (image: MarkdownImageOccurrence) =>
25+
new Promise<MarkdownImageSource | undefined>((settle) => {
26+
const request = { href: image.url, alt: image.altText || null, title: image.title || null };
27+
const attempt = () => {
28+
const current = latestRef.current;
29+
if (!current) return;
30+
Promise.resolve()
31+
.then(() => current(request))
32+
.then(
33+
(source) => settle(source ?? undefined),
34+
() => {
35+
if (latestRef.current !== current) attempt();
36+
else retriesRef.current.add(attempt);
37+
},
38+
);
39+
};
40+
attempt();
41+
});
42+
}, [enabled]);
43+
}

‎apps/mobile/vite.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const reactTests = [
77
"src/features/settings/components/BranchNamingSettings.test.tsx",
88
"src/native/NativeHeaderToolbar.test.tsx",
99
"src/native/useRestingTopOffset.test.tsx",
10+
"src/native/useRetryingImageSource.test.tsx",
1011
"src/state/use-selected-thread-git-actions.test.tsx",
1112
];
1213

‎pnpm-lock.yaml‎

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)