From ecd945b309a7704632611569a657359c00721021 Mon Sep 17 00:00:00 2001 From: oudecheng <13802883547@139.com> Date: Wed, 20 May 2026 15:04:34 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E6=A0=BC=E5=BC=8F=E5=8C=96=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=BC=BA=E5=AF=B9=E7=89=B9=E6=AE=8A=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E5=92=8C=E6=8D=A2=E8=A1=8C=E7=AC=A6=E7=9A=84=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 ++ src/command/handlers/save_session.rs | 54 +++++++++++++++++++++++++--- 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 0244034..37c4580 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ PicoBot.code-workspace .picobot .claude output +.python-version +pyproject.toml diff --git a/src/command/handlers/save_session.rs b/src/command/handlers/save_session.rs index 23e2155..8236ee4 100644 --- a/src/command/handlers/save_session.rs +++ b/src/command/handlers/save_session.rs @@ -257,10 +257,24 @@ pub fn generate_markdown( /// 格式化消息内容 /// /// 如果内容包含特殊字符,使用代码块包装 +/// 使用比内容中最大连续反引号数量多1的反引号来包裹,避免嵌套冲突 pub fn format_message_content(content: &str) -> String { - // 如果内容包含代码块标记或表格标记,使用原始格式 - if content.contains("```") || content.contains("| ") { - format!("```\n{}\n```", content) + // 如果内容包含表格标记或换行符,使用代码块包裹以保留格式 + if content.contains("| ") || content.contains('\n') { + // 计算内容中连续反引号的最大数量 + let max_backticks = content + .chars() + .fold((0, 0), |(max_count, current_count), c| { + if c == '`' { + (max_count, current_count + 1) + } else { + (max_count.max(current_count), 0) + } + }) + .0; + // 使用比最大数量多1的反引号来包裹(至少3个) + let fence = "`".repeat(max_backticks.max(3) + 1); + format!("{}\n{}\n{}", fence, content, fence) } else { content.to_string() } @@ -553,10 +567,40 @@ mod tests { #[test] fn test_format_message_content() { + // 普通单行文本 - 原样返回 assert_eq!(format_message_content("hello"), "hello"); + + // 单行包含反引号 - 原样返回(单行不需要包裹) + assert_eq!(format_message_content("`code`"), "`code`"); + + // 包含换行符 - 使用4个反引号包裹(最小) assert_eq!( - format_message_content("```code```"), - "```\n```code```\n```" + format_message_content("line1\nline2\nline3"), + "````\nline1\nline2\nline3\n````" + ); + + // 包含表格标记 - 使用4个反引号包裹 + assert_eq!( + format_message_content("| col1 | col2 |"), + "````\n| col1 | col2 |\n````" + ); + + // 多行内容包含3个反引号(代码块标记)- 使用4个反引号包裹 + assert_eq!( + format_message_content("```code```\nmore"), + "````\n```code```\nmore\n````" + ); + + // 多行内容包含多行代码块 + assert_eq!( + format_message_content("```\ncode\n```\nmore"), + "````\n```\ncode\n```\nmore\n````" + ); + + // 多行内容包含4个反引号 - 使用5个反引号包裹 + assert_eq!( + format_message_content("````code````\nmore"), + "`````\n````code````\nmore\n`````" ); }