Skip to content

Commit e00b2ad

Browse files
committed
fix(mobile): chips use their full accent and line refs render at body size
1 parent c91d880 commit e00b2ad

6 files changed

Lines changed: 72 additions & 24 deletions

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,3 @@ describe("context chip kinds", () => {
102102
expect(presentation.icon).toBe("circle-dashed");
103103
});
104104
});
105-
106-
describe("contextChipForeground", () => {
107-
it("mixes 22% of the accent into the text color, as web does", async () => {
108-
const { contextChipForeground } = await import("./composerChipPresentation");
109-
expect(contextChipForeground("#009f6e", "#ffffff")).toBe("#c7eadf");
110-
});
111-
112-
it("replaces an alpha byte the accent already carries", async () => {
113-
const { contextChipForeground } = await import("./composerChipPresentation");
114-
expect(contextChipForeground("#ffffffff", "#000000")).toBe("#383838");
115-
});
116-
});

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { formatAttachmentSize } from "@t3tools/client-runtime/state/attachments"
22
import { imageMimeType } from "@t3tools/shared/image";
33
import { videoMimeType } from "@t3tools/shared/video";
44

5-
import { flattenThemeColor } from "./mobileTheme";
65
import type { CONTEXT_CHIP_ICON_SOURCES } from "./contextChipIcons.generated";
76

87
export type ContextChipIcon = keyof typeof CONTEXT_CHIP_ICON_SOURCES;
@@ -48,10 +47,6 @@ export const UNAVAILABLE_CONTEXT_CHIP_PRESENTATION: ContextChipPresentation = {
4847
icon: "circle-dashed",
4948
};
5049

51-
export function contextChipForeground(accent: string, foreground: string) {
52-
return flattenThemeColor(`${accent.slice(0, 7)}38`, foreground);
53-
}
54-
5550
export function composerChipSizeSuffix(record?: {
5651
readonly kind?: string;
5752
readonly sizeBytes?: number;

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,36 @@ export function markdownWithHtml(markdown: string): string {
140140
return markdown;
141141
}
142142

143+
const INLINE_CODE_SPAN = /`+([^`\r\n]+)`+/g;
144+
145+
export function markdownWithInlineFileLinks(markdown: string, fileLink: RegExp): string {
146+
if (![...markdown.matchAll(INLINE_CODE_SPAN)].some(([, code]) => fileLink.test(code!.trim())))
147+
return markdown;
148+
const edits: Array<{ start: number; end: number; content: string }> = [];
149+
const visit = (node: MarkdownNode) => {
150+
if (node.type === "inlineCode") {
151+
const start = node.position?.start.offset;
152+
const end = node.position?.end.offset;
153+
if (
154+
start !== undefined &&
155+
end !== undefined &&
156+
fileLink.test(node.value) &&
157+
!/[<>]/.test(node.value)
158+
)
159+
edits.push({
160+
start,
161+
end,
162+
content: `[${node.value.replace(/[[\]\\]/g, "\\$&")}](<${node.value}>)`,
163+
});
164+
} else if (node.type !== "link" && node.type !== "linkReference" && "children" in node)
165+
node.children.forEach(visit);
166+
};
167+
visit(parser.parse(markdown));
168+
for (const edit of edits.reverse())
169+
markdown = markdown.slice(0, edit.start) + edit.content + markdown.slice(edit.end);
170+
return markdown;
171+
}
172+
143173
// A delimiter row such as `|---|:--:|`, possibly inside a blockquote or list item.
144174
function isTableDelimiterRow(line: string): boolean {
145175
if (!line.includes("-")) return false;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
import { ENRICHED_INLINE_FILE_LINK_REGEX } from "./enrichedLinkPresentation";
3+
import { markdownWithInlineFileLinks } from "./markdownDocument";
4+
5+
const rewrite = (markdown: string) =>
6+
markdownWithInlineFileLinks(markdown, ENRICHED_INLINE_FILE_LINK_REGEX);
7+
8+
describe("inline code that names a file", () => {
9+
it("becomes a link to the same path", () => {
10+
expect(rewrite("See `src/math.ts:2` and `./README.md`.")).toBe(
11+
"See [src/math.ts:2](<src/math.ts:2>) and [./README.md](<./README.md>).",
12+
);
13+
});
14+
15+
it("keeps commands, identifiers and URLs as code", () => {
16+
const markdown = "Run `vp test run`, call `useMemo`, open `https://example.com/a.ts`.";
17+
expect(rewrite(markdown)).toBe(markdown);
18+
});
19+
20+
it("leaves fenced code blocks and code already inside a link alone", () => {
21+
const markdown = "```\nsrc/math.ts:2\n```\n\n[`src/math.ts`](src/math.ts)";
22+
expect(rewrite(markdown)).toBe(markdown);
23+
});
24+
25+
it("handles double-backtick spans", () => {
26+
expect(rewrite("Open ``src/math.ts`` now.")).toBe("Open [src/math.ts](<src/math.ts>) now.");
27+
});
28+
});

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import {
44
formatComposerContextHref,
55
formatComposerContextReference,
66
} from "@t3tools/shared/composerContextReferences";
7-
import { markdownWithHtml, markdownWithTableBreaks } from "../lib/markdownDocument";
7+
import {
8+
markdownWithHtml,
9+
markdownWithInlineFileLinks,
10+
markdownWithTableBreaks,
11+
} from "../lib/markdownDocument";
812
import { useCallback, useMemo, useState, type ReactNode } from "react";
913
import { Platform, Share, StyleSheet, View } from "react-native";
1014
import {
@@ -151,7 +155,13 @@ export function MobileEnrichedMarkdownText(props: SelectableMarkdownTextProps) {
151155
const { renderImage, resolveImageSource } = props;
152156
const { themeAppearance, mathRenderingEnabled } = useAppearancePreferences();
153157
const markdown = useMemo(
154-
() => markdownWithTableBreaks(markdownWithHtml(props.markdown)),
158+
() =>
159+
markdownWithTableBreaks(
160+
markdownWithInlineFileLinks(
161+
markdownWithHtml(props.markdown),
162+
ENRICHED_INLINE_FILE_LINK_REGEX,
163+
),
164+
),
155165
[props.markdown],
156166
);
157167
const [documentAssets, setDocumentAssets] = useState<DocumentAssetsEvent["assets"]>([]);
@@ -178,7 +188,6 @@ export function MobileEnrichedMarkdownText(props: SelectableMarkdownTextProps) {
178188
const linkRecognition = useMemo(
179189
() => ({
180190
text: enrichedSkillLinkRegex(props.skills) ?? undefined,
181-
inlineCode: ENRICHED_INLINE_FILE_LINK_REGEX,
182191
}),
183192
[props.skills],
184193
);

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
MarkdownStyle,
77
} from "react-native-enriched-markdown";
88
import type { ComposerContextRecord } from "@t3tools/contracts";
9-
import { contextChipForeground, contextChipPresentation } from "../lib/composerChipPresentation";
9+
import { contextChipPresentation } from "../lib/composerChipPresentation";
1010
import { CONTEXT_CHIP_ICON_SOURCES } from "../lib/contextChipIcons.generated";
1111
import { FONT_FAMILIES } from "../lib/fontFamilies";
1212
import { markdownFileIconSource } from "../lib/markdownFileIcons";
@@ -94,9 +94,7 @@ export function useEnrichedLinkVariants(
9494
new Map(
9595
[...links].map(([url, link]) => [
9696
url,
97-
link.chip
98-
? contextChipForeground(link.chip.accent ?? textStyle.color, textStyle.color)
99-
: undefined,
97+
link.chip ? (link.chip.accent ?? textStyle.color) : undefined,
10098
]),
10199
),
102100
[links, textStyle],

0 commit comments

Comments
 (0)