From 3128abe3c61b7e2077663bcff989be6d5dafdc69 Mon Sep 17 00:00:00 2001 From: oudecheng <13802883547@139.com> Date: Thu, 21 May 2026 17:17:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=20parse=5Fpost=5Fcon?= =?UTF-8?q?tent=20=E5=87=BD=E6=95=B0=EF=BC=8C=E6=94=AF=E6=8C=81=E5=A4=84?= =?UTF-8?q?=E7=90=86=E5=8C=85=E5=90=AB=E5=86=85=E5=AE=B9=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=A0=81=E5=9D=97=EF=BC=8C=E5=B9=B6=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E7=9B=B8=E5=85=B3=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E4=BB=A5=E7=A1=AE=E4=BF=9D=E5=90=91=E5=90=8E=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/channels/feishu.rs | 49 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/src/channels/feishu.rs b/src/channels/feishu.rs index 9f68f9e..351d25c 100644 --- a/src/channels/feishu.rs +++ b/src/channels/feishu.rs @@ -1384,7 +1384,16 @@ fn parse_post_content(content: &str) -> String { } "code_block" => { let lang = el.get("language").and_then(|l| l.as_str()).unwrap_or(""); - let code_text = el.get("text").and_then(|t| t.as_str()).unwrap_or(""); + let code_text = if let Some(content_arr) = el.get("content").and_then(|c| c.as_array()) { + content_arr + .iter() + .filter_map(|item| item.get("text").and_then(|t| t.as_str())) + .collect::>() + .join("") + } else { + // Fallback to text field for backwards compatibility + el.get("text").and_then(|t| t.as_str()).unwrap_or("").to_string() + }; out.push(format!("\n```{}\n{}\n```\n", lang, code_text)); } _ => { @@ -2190,7 +2199,7 @@ fn sanitize_download_file_name(file_name: &str) -> String { mod tests { use super::{ FeishuChannel, MsgFormat, extract_file_name_from_content_disposition, - infer_download_filename, sanitize_download_file_name, + infer_download_filename, parse_post_content, sanitize_download_file_name, }; #[test] @@ -2279,6 +2288,42 @@ mod tests { let file_name = extract_file_name_from_content_disposition(&headers); assert_eq!(file_name.as_deref(), Some("archive.zip")); } + + #[test] + fn parse_post_content_handles_code_block_with_content_array() { + // Test parsing code_block with content array (standard Feishu format) + let post_json = r#"{"post":{"zh_cn":{"content":[[{"tag":"code_block","language":"python","content":[{"tag":"text","text":"def hello():"},{"tag":"text","text":" print('world')"}]}]]}}}"#; + let result = parse_post_content(post_json); + assert!(result.contains("```python")); + assert!(result.contains("def hello():")); + assert!(result.contains("print('world')")); + } + + #[test] + fn parse_post_content_handles_code_block_with_fallback_text() { + // Backwards compatibility: some formats might use text field directly + let post_json = r#"{"post":{"zh_cn":{"content":[[{"tag":"code_block","language":"rust","text":"fn main() {}"}]]}}}"#; + let result = parse_post_content(post_json); + assert!(result.contains("```rust")); + assert!(result.contains("fn main() {}")); + } + + #[test] + fn parse_post_content_handles_code_block_without_language() { + // Test code_block without language field + let post_json = r#"{"post":{"zh_cn":{"content":[[{"tag":"code_block","content":[{"tag":"text","text":"plain text"}]}]]}}}"#; + let result = parse_post_content(post_json); + assert!(result.contains("```")); + assert!(result.contains("plain text")); + } + + #[test] + fn parse_post_content_handles_empty_code_block() { + // Test code_block with empty content + let post_json = r#"{"post":{"zh_cn":{"content":[[{"tag":"code_block","language":"go"}]]}}}"#; + let result = parse_post_content(post_json); + assert!(result.contains("```go")); + } } #[async_trait]