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): 加粗/斜体/删除线里的链接也能点击

Dash8d5f6baa3c60
diff --git a/apps/mobile/src/__tests__/messageMarkdown.test.ts b/apps/mobile/src/__tests__/messageMarkdown.test.ts
index 36be6ff291..f7b848f7e0 100644
--- a/apps/mobile/src/__tests__/messageMarkdown.test.ts
+++ b/apps/mobile/src/__tests__/messageMarkdown.test.ts
@@ -123,7 +123,7 @@ describe('messageMarkdown', () => {
const blocks = parseMobileMarkdown(PR_WATCH_EXPANDED_BLANK_FIXTURE);
expect(blocks.length).toBeGreaterThan(1);
- // badge Markdown 位于 strong 标记内,当前解析为文本而非图片;空白不是图片尺寸撑高导致。
+ // badge Markdown 位于 strong 标记内,强调里的图片按原文显示(不成图片);空白不是图片尺寸撑高导致。
expect(collectMobileMarkdownImages(PR_WATCH_EXPANDED_BLANK_FIXTURE)).toEqual([]);
expect(blocks.some((block) => block.type === 'code')).toBe(true);
});
@@ -540,6 +540,45 @@ describe('messageMarkdown', () => {
]);
});
+ it('keeps links and other inline tokens inside emphasis, flattened with marks', () => {
+ expect(parseMobileMarkdownInlines('打开 **https://example.com/abc/** 安装')).toEqual([
+ { type: 'text', text: '打开 ' },
+ { type: 'link', text: 'https://example.com/abc/', url: 'https://example.com/abc/', marks: ['strong'] },
+ { type: 'text', text: ' 安装' },
+ ]);
+ expect(parseMobileMarkdownInlines('**看 [文档](https://example.com/docs) 和 `a.ts`**')).toEqual([
+ { type: 'strong', text: '看 ' },
+ { type: 'link', text: '文档', url: 'https://example.com/docs', marks: ['strong'] },
+ { type: 'strong', text: ' 和 ' },
+ { type: 'code', text: 'a.ts', marks: ['strong'] },
+ ]);
+ expect(parseMobileMarkdownInlines('*见 https://example.com/x*')).toEqual([
+ { type: 'emphasis', text: '见 ' },
+ { type: 'link', text: 'https://example.com/x', url: 'https://example.com/x', marks: ['emphasis'] },
+ ]);
+ expect(parseMobileMarkdownInlines('**~~https://example.com/old~~**')).toEqual([
+ { type: 'link', text: 'https://example.com/old', url: 'https://example.com/old', marks: ['strong', 'strikethrough'] },
+ ]);
+ });
+
+ it('keeps images inside emphasis as literal text and does not linkify their [alt](url)', () => {
+ expect(parseMobileMarkdownInlines(
+ '**<sub>![P2 Badge](https://img.shields.io/badge/P2-yellow)</sub> 标题 https://example.com/y**',
+ )).toEqual([
+ { type: 'strong', text: '<sub>![P2 Badge](https://img.shields.io/badge/P2-yellow)</sub> 标题 ' },
+ { type: 'link', text: 'https://example.com/y', url: 'https://example.com/y', marks: ['strong'] },
+ ]);
+ });
+
+ it('keeps bare paths inside emphasis within an HTML comment as plain text', () => {
+ expect(parseMobileMarkdownInlines('<!-- **src/a.ts** --> **src/b.ts**')).toEqual([
+ { type: 'text', text: '<!-- ' },
+ { type: 'strong', text: 'src/a.ts' },
+ { type: 'text', text: ' --> ' },
+ { type: 'link', text: 'src/b.ts', url: 'src/b.ts', bare: true, marks: ['strong'] },
+ ]);
+ });
+
it('parses emphasis at the beginning of a line', () => {
expect(parseMobileMarkdownInlines('*em* first')).toEqual([
{ type: 'emphasis', text: 'em' },
@@ -1553,11 +1592,9 @@ describe('bare file paths(正文纯文本形态)', () => {
]);
});
- it('已知取舍:被强调包裹的路径不成 chip(手机 inline 模型扁平,不支持嵌套)', () => {
- // 桌面 remark 能在 strong 的子 text 里继续 linkify;手机端 inline 无嵌套,
- // 强调整段吃掉。属既有架构限制,本次不扩,先把现状钉住。
+ it('被强调包裹的路径同样识别,带外层强调标记(与桌面 remark 在 strong 子节点里 linkify 同口径)', () => {
expect(parseMobileMarkdownInlines('**src/App.tsx**')).toEqual([
- { type: 'strong', text: 'src/App.tsx' },
+ { type: 'link', text: 'src/App.tsx', url: 'src/App.tsx', bare: true, marks: ['strong'] },
]);
});
diff --git a/apps/mobile/src/__tests__/selectableMarkdownHtml.test.ts b/apps/mobile/src/__tests__/selectableMarkdownHtml.test.ts
index 0036cb1615..18264f1d2f 100644
--- a/apps/mobile/src/__tests__/selectableMarkdownHtml.test.ts
+++ b/apps/mobile/src/__tests__/selectableMarkdownHtml.test.ts
@@ -165,6 +165,15 @@ describe('buildSelectableMarkdownHtml 本地路径不出死链', () => {
expect(html).toContain('<a href="https://example.com/a.ts">站点</a>');
});
+ it('加粗 / 删除线里的 http(s) 链接仍是可点 <a>,外层套强调标签', () => {
+ expect(buildSelectableMarkdownHtml('打开 **https://example.com/a** 看')).toContain(
+ '<strong><a href="https://example.com/a">https://example.com/a</a></strong>',
+ );
+ expect(buildSelectableMarkdownHtml('**~~https://example.com/b~~**')).toContain(
+ '<strong><del><a href="https://example.com/b">https://example.com/b</a></del></strong>',
+ );
+ });
+
it('会话深链渲染成不可点的 chip:保留 chip 观感,但不是 <a>、也不带 href', () => {
const html = buildSelectableMarkdownHtml('[某会话](cindy://session/abc123)');
expect(html).toContain('xdt-session-chip');
diff --git a/apps/mobile/src/session/BotGroupMessageText.tsx b/apps/mobile/src/session/BotGroupMessageText.tsx
index 39f59f94bd..dfa1f92af8 100644
--- a/apps/mobile/src/session/BotGroupMessageText.tsx
+++ b/apps/mobile/src/session/BotGroupMessageText.tsx
@@ -15,6 +15,15 @@ type Styles = ReturnType<typeof makeStyles>;
function renderInlines(inlines: readonly MobileMarkdownInline[], styles: Styles): ReactNode[] {
return inlines.map((inline, index) => {
+ // 强调里的链接 / 代码等:外层套一层带强调样式的 Text,嵌套 Text 继承样式。
+ 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])}>
+ {renderInlines([{ ...inline, marks: undefined }], styles)}
+ </Text>
+ );
+ }
switch (inline.type) {
case 'strong': return <Text key={index} style={styles.strong}>{inline.text}</Text>;
case 'emphasis': return <Text key={index} style={styles.emphasis}>{inline.text}</Text>;
diff --git a/apps/mobile/src/session/MessageRenderer.tsx b/apps/mobile/src/session/MessageRenderer.tsx
index 14f69a5802..bef79c0f28 100644
--- a/apps/mobile/src/session/MessageRenderer.tsx
+++ b/apps/mobile/src/session/MessageRenderer.tsx
@@ -219,6 +219,7 @@ import {
type MobileMarkdownBlock,
type MobileMarkdownBlockGroup,
type MobileMarkdownInline,
+ type MobileMarkdownInlineMark,
type MobileMarkdownTextRunGroupingOptions,
} from '@/session/messageMarkdown';
import { MarkdownBlockContent } from '@/session/MarkdownBlockContent';
@@ -6124,6 +6125,15 @@ function clickableInlineStyle(
return [base, onPress ? styles.markdownLink : undefined, extra];
}
+function markdownMarkStyle(
+ styles: ReturnType<typeof makeStyles>,
+ mark: MobileMarkdownInlineMark,
+): StyleProp<TextStyle> {
+ if (mark === 'strong') return styles.markdownStrong;
+ if (mark === 'emphasis') return styles.markdownEmphasis;
+ return styles.markdownStrike;
+}
+
/** 本地路径链接形态的路径 chip 包装:candidate 按 url memo,保证引用稳定——
* renderInline 是普通函数,若在其内直接 classify 会每次 render 产新对象,
* 击穿 ChatPathChipSpan 的 memo/effect 依赖,unknown verdict(不落缓存)的
@@ -6224,6 +6234,13 @@ function renderInline(
streaming?: boolean;
} = {},
): ReactNode {
+ // 强调里的链接 / 代码等:外层强调样式叠进 baseStyle,其余照常渲染(span 平铺,不嵌套)。
+ if (inline.marks?.length) {
+ return renderInline({ ...inline, marks: undefined }, index, styles, {
+ ...ctx,
+ baseStyle: [ctx.baseStyle, ...inline.marks.map((mark) => markdownMarkStyle(styles, mark))],
+ });
+ }
const SpanText = ctx.SpanText ?? MessageBodyText;
const openImage = ctx.onOpenImage ?? (ctx.onOpenPayload
? (url: string, alt?: string) => {
diff --git a/apps/mobile/src/session/messageMarkdown.ts b/apps/mobile/src/session/messageMarkdown.ts
index 6cf5e0e57a..754f710953 100644
--- a/apps/mobile/src/session/messageMarkdown.ts
+++ b/apps/mobile/src/session/messageMarkdown.ts
@@ -13,7 +13,14 @@ import {
import { DEEP_LINK_SCHEME_GROUP } from '@/session/sessionLinks';
import { i18n } from '@/i18n';
-export type MobileMarkdownInline =
+// 加粗 / 斜体 / 删除线里的内容会继续按行内语法解析(`**https://…**` 的链接照样可点,
+// 与桌面 remark 同口径),结果拍平进同一层 inline 列表:里面的纯文本就是该强调类型本身,
+// 其余 token(链接、路径、代码、公式;图片除外,见 parseMarkedInlines)带 marks 标出外层
+// 强调,由渲染层叠加样式。拍平而不是做成树,分段、图集收集、长度估算等按单层列表遍历的
+// 逻辑都不用改。
+export type MobileMarkdownInlineMark = 'strong' | 'emphasis' | 'strikethrough';
+
+export type MobileMarkdownInline = (
| { type: 'text'; text: string }
// bare:这条 link 是从正文纯文本里切出来的裸路径(matchBareFilePathLink),不是作者
// 手写的 `[label](url)`。渲染层据此决定点亮后是否套等宽 chip —— 裸路径的未点亮态是
@@ -29,7 +36,8 @@ export type MobileMarkdownInline =
// 原生 Text 流渲染时经 latexToUnicodeApproximation 近似;WebView 面(文件
// 阅读器)可用 KaTeX 精确渲染。
| { type: 'math'; text: string }
- | MobileMarkdownImageInline;
+ | MobileMarkdownImageInline
+) & { marks?: MobileMarkdownInlineMark[] };
// 正文图片:来自 ![alt](url) 或模型常用的 raw HTML <img src="..." width="150">(见桌面端 remarkHtmlImages)。
// Markdown 图片还可保留桌面本地/相对路径;会话渲染时再结合被控端 workdir
@@ -1070,6 +1078,14 @@ function stripCodeFenceIndent(line: string, indent: string): string {
export function parseMobileMarkdownInlines(
input: string,
startsInsideHtmlComment = false,
+): MobileMarkdownInline[] {
+ return parseInlines(input, startsInsideHtmlComment, false);
+}
+
+function parseInlines(
+ input: string,
+ startsInsideHtmlComment: boolean,
+ imagesAsText: boolean,
): MobileMarkdownInline[] {
const out: MobileMarkdownInline[] = [];
let cursor = 0;
@@ -1080,12 +1096,44 @@ export function parseMobileMarkdownInlines(
break;
}
if (token.index > cursor) pushText(out, input.slice(cursor, token.index));
- out.push(token.inline);
+ if (isMarkedInline(token.inline)) {
+ out.push(...parseMarkedInlines(token.inline.type, token.inline.text, input, token.index, startsInsideHtmlComment));
+ } else if (imagesAsText && token.inline.type === 'image') {
+ pushText(out, input.slice(token.index, token.end));
+ } else {
+ out.push(token.inline);
+ }
cursor = token.end;
}
return out.length > 0 ? out : [{ type: 'text', text: input }];
}
+function isMarkedInline(
+ inline: MobileMarkdownInline,
+): inline is Extract<MobileMarkdownInline, { type: MobileMarkdownInlineMark }> {
+ return inline.type === 'strong' || inline.type === 'emphasis' || inline.type === 'strikethrough';
+}
+
+// 强调内容再解析一遍并拍平(见 MobileMarkdownInlineMark 的说明)。内层文本严格短于外层,
+// 递归必然收敛;强调起点在 HTML 注释里时内层也按注释内处理,注释里的裸路径不会被点亮。
+// 强调里的图片保持原文:GitHub review 常见 `**<sub>![P2 Badge](…)</sub> 标题**`,渲染成
+// 图片预览会把小徽章撑成大图并打断可选中的正文;原文仍整段占位,`[alt](url)` 不会被当成链接。
+function parseMarkedInlines(
+ mark: MobileMarkdownInlineMark,
+ text: string,
+ input: string,
+ index: number,
+ startsInsideHtmlComment: boolean,
+): MobileMarkdownInline[] {
+ const innerStartsInsideHtmlComment = (startsInsideHtmlComment || input.includes('<!--'))
+ && isInsideHtmlComment(blankEscapedAngles(blankCodeSpans(input)), index, startsInsideHtmlComment);
+ return parseInlines(text, innerStartsInsideHtmlComment, true).map((inline) => (
+ inline.type === 'text'
+ ? { type: mark, text: inline.text }
+ : { ...inline, marks: [mark, ...(inline.marks ?? [])] }
+ ));
+}
+
/** Mobile 只把 `~~` 当删除线,单个 `~` 是普通字符,不能当 URL 包裹标记。 */
function mobileMarkdownWrapMarker(prefix: string): string | null {
const marker = markdownWrapMarkerFromPrefix(prefix);
diff --git a/apps/mobile/src/session/selectableMarkdownHtml.ts b/apps/mobile/src/session/selectableMarkdownHtml.ts
index 747f3a1703..59666f2d73 100644
--- a/apps/mobile/src/session/selectableMarkdownHtml.ts
+++ b/apps/mobile/src/session/selectableMarkdownHtml.ts
@@ -3,6 +3,7 @@ import {
parseMobileMarkdown,
type MobileMarkdownBlock,
type MobileMarkdownInline,
+ type MobileMarkdownInlineMark,
} from '@/session/messageMarkdown';
import { tokenizeCode } from '@/session/codeHighlight';
import { parseSessionDeepLinkUrl, shortSessionId } from '@/session/sessionLinks';
@@ -499,7 +500,20 @@ function renderInlines(inlines: readonly MobileMarkdownInline[], ctx: RenderCont
return inlines.map((inline) => renderInline(inline, ctx)).join('');
}
+const MARK_TAGS: Record<MobileMarkdownInlineMark, string> = {
+ strong: 'strong',
+ emphasis: 'em',
+ strikethrough: 'del',
+};
+
function renderInline(inline: MobileMarkdownInline, ctx: RenderContext = {}): string {
+ // 强调里的链接 / 代码等:先按原类型渲染,再由内到外套上外层强调标签。
+ if (inline.marks?.length) {
+ return inline.marks.reduceRight(
+ (html, mark) => `<${MARK_TAGS[mark]}>${html}</${MARK_TAGS[mark]}>`,
+ renderInline({ ...inline, marks: undefined }, ctx),
+ );
+ }
switch (inline.type) {
case 'text':
return escapeHtml(inline.text);