Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mobile): 群聊里强调包着的链接不加下划线

Dash1d18723ae99a
diff --git a/apps/mobile/src/__tests__/BotGroupMarkdownText.test.tsx b/apps/mobile/src/__tests__/BotGroupMarkdownText.test.tsx
new file mode 100644
index 0000000000..a89be05718
--- /dev/null
+++ b/apps/mobile/src/__tests__/BotGroupMarkdownText.test.tsx
@@ -0,0 +1,58 @@
+// @vitest-environment jsdom
+import { act, createElement } from 'react';
+import { createRoot, type Root } from 'react-dom/client';
+import { afterEach, beforeEach, expect, it, vi } from 'vitest';
+
+// 把 style 展平进 data-style,断言每个 Text 实际叠了哪些样式。
+vi.mock('react-native', async () => {
+ const React = await import('react');
+ const flat = (style: unknown): Record<string, unknown> =>
+ Object.assign({}, ...[style].flat(Infinity).filter(Boolean));
+ const host = (tag: string) => ({ children, style }: any) =>
+ React.createElement(tag, { 'data-style': JSON.stringify(flat(style)) }, children);
+ return {
+ View: host('div'),
+ Text: host('span'),
+ StyleSheet: { create: (s: any) => s, flatten: (s: any) => s, hairlineWidth: 1 },
+ Platform: { OS: 'ios', select: (s: any) => s.ios ?? s.default },
+ };
+});
+vi.mock('@/components/AppText', async () => ({ Text: (await import('react-native')).Text }));
+vi.mock('@/theme', () => ({
+ fontWeight: { semibold: '600', bold: '700' },
+ monoFont: 'Menlo',
+ useThemedStyles: (make: (colors: any) => any) => make(new Proxy({}, { get: () => '#000' })),
+}));
+
+import { BotGroupMarkdownText } from '@/session/BotGroupMessageText';
+
+let root: Root;
+let node: HTMLDivElement;
+beforeEach(() => {
+ (globalThis as any).IS_REACT_ACT_ENVIRONMENT = true;
+ node = document.createElement('div');
+ document.body.append(node);
+ root = createRoot(node);
+});
+afterEach(() => {
+ act(() => root.unmount());
+ node.remove();
+});
+
+const styleOf = (text: string): Record<string, unknown> => {
+ // 取最内层的匹配(强调外层 Text 与内层代码 Text 文本相同)。
+ const span = [...node.querySelectorAll('span')].filter((el) => el.textContent === text).at(-1);
+ expect(span, `未找到文本 ${text}`).toBeDefined();
+ return JSON.parse(span!.getAttribute('data-style')!);
+};
+
+it('群聊不打开链接:强调里的链接只保留强调样式,不加下划线', async () => {
+ await act(async () => root.render(createElement(BotGroupMarkdownText, {
+ content: '**https://a.example/x** ~~https://b.example/y~~ **`a.ts`**',
+ })));
+ expect(styleOf('https://a.example/x')).toMatchObject({ fontWeight: '600' });
+ expect(styleOf('https://a.example/x').textDecorationLine).toBeUndefined();
+ expect(styleOf('https://b.example/y').textDecorationLine).toBe('line-through');
+ // 强调里的行内代码照常套代码样式。
+ expect(styleOf('a.ts')).toMatchObject({ fontFamily: 'Menlo' });
+});
diff --git a/apps/mobile/src/session/BotGroupMessageText.tsx b/apps/mobile/src/session/BotGroupMessageText.tsx
index 431903e0f2..9ed9e52641 100644
--- a/apps/mobile/src/session/BotGroupMessageText.tsx
+++ b/apps/mobile/src/session/BotGroupMessageText.tsx
@@ -15,15 +15,13 @@ type Styles = ReturnType<typeof makeStyles>;
function renderInlines(inlines: readonly MobileMarkdownInline[], styles: Styles): ReactNode[] {
return inlines.map((inline, index) => {
- // 强调里的链接 / 代码等:外层套一层带强调样式的 Text,嵌套 Text 继承样式。
- // 删除线里的链接:内层 underline 会盖掉外层 line-through(单值属性),改用合并值。
+ // 强调里的代码等:外层套一层带强调样式的 Text,嵌套 Text 继承样式。强调里的链接按强调
+ // 纯文本显示:群聊不打开链接,不加下划线,不做出「看着能点却点不动」的样子(DESIGN.md §14.5)。
if (inline.marks?.length) {
const markStyles = { strong: styles.strong, emphasis: styles.emphasis, strikethrough: styles.strike };
return (
<Text key={index} style={inline.marks.map((mark) => markStyles[mark])}>
- {inline.type === 'link' && inline.marks.includes('strikethrough')
- ? <Text style={styles.linkStrike}>{inline.text}</Text>
- : renderInlines([{ ...inline, marks: undefined }], styles)}
+ {inline.type === 'link' ? inline.text : renderInlines([{ ...inline, marks: undefined }], styles)}
</Text>
);
}
@@ -100,7 +98,6 @@ const makeStyles = (colors: ThemeColors) => StyleSheet.create({
emphasis: { fontStyle: 'italic' },
strike: { textDecorationLine: 'line-through' },
link: { textDecorationLine: 'underline' },
- linkStrike: { textDecorationLine: 'underline line-through' },
inlineCode: { fontFamily: monoFont, fontSize: typeScale.bodySmall, lineHeight: lineHeight.bodyLarge, backgroundColor: colors.surfaceChip },
quote: { borderLeftWidth: 2, borderLeftColor: colors.border, paddingLeft: spacing.md },
quoteText: { color: colors.textSecondary },