use serde::Serialize;
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct CommentBlock {
pub text: String,
#[serde(skip_serializing_if = "CommentAttributes::is_empty")]
pub attributes: CommentAttributes,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize)]
pub struct CommentAttributes {
#[serde(skip_serializing_if = "is_false")]
pub bold: bool,
#[serde(skip_serializing_if = "is_false")]
pub code: bool,
#[serde(rename = "code-block", skip_serializing_if = "Option::is_none")]
pub code_block: Option<CodeBlockAttr>,
#[serde(skip_serializing_if = "Option::is_none")]
pub list: Option<ListAttr>,
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct CodeBlockAttr {
#[serde(rename = "code-block")]
pub code_block: String,
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct ListAttr {
pub list: String,
}
impl CommentAttributes {
fn is_empty(&self) -> bool {
!self.bold && !self.code && self.code_block.is_none() && self.list.is_none()
}
}
#[allow(clippy::trivially_copy_pass_by_ref)] fn is_false(b: &bool) -> bool {
!*b
}
fn newline(attributes: CommentAttributes) -> CommentBlock {
CommentBlock {
text: "\n".to_string(),
attributes,
}
}
pub fn markdown_to_comment_blocks(body: &str) -> Vec<CommentBlock> {
if body.is_empty() {
return Vec::new();
}
let mut blocks: Vec<CommentBlock> = Vec::new();
let mut in_code_fence = false;
for line in body.split('\n') {
if line.trim_start().starts_with("```") {
in_code_fence = !in_code_fence;
continue;
}
if in_code_fence {
if !line.is_empty() {
blocks.push(CommentBlock {
text: line.to_string(),
attributes: CommentAttributes::default(),
});
}
blocks.push(newline(CommentAttributes {
code_block: Some(CodeBlockAttr {
code_block: "plain".to_string(),
}),
..Default::default()
}));
continue;
}
if let Some(rest) = strip_bullet(line) {
push_inline_runs(&mut blocks, rest);
blocks.push(newline(CommentAttributes {
list: Some(ListAttr {
list: "bullet".to_string(),
}),
..Default::default()
}));
continue;
}
if let Some(rest) = strip_ordered(line) {
push_inline_runs(&mut blocks, rest);
blocks.push(newline(CommentAttributes {
list: Some(ListAttr {
list: "ordered".to_string(),
}),
..Default::default()
}));
continue;
}
if let Some(rest) = strip_heading(line) {
push_bold_run(&mut blocks, rest);
blocks.push(newline(CommentAttributes::default()));
continue;
}
push_inline_runs(&mut blocks, line);
blocks.push(newline(CommentAttributes::default()));
}
while blocks.len() > 1 {
let last = &blocks[blocks.len() - 1];
if last.text == "\n" && last.attributes.is_empty() {
blocks.pop();
} else {
break;
}
}
blocks
}
fn strip_bullet(line: &str) -> Option<&str> {
let trimmed = line.trim_start();
for marker in ['-', '*', '+'] {
if let Some(rest) = trimmed.strip_prefix(marker) {
if let Some(rest) = rest.strip_prefix(' ') {
return Some(rest);
}
}
}
None
}
fn strip_ordered(line: &str) -> Option<&str> {
let trimmed = line.trim_start();
let digits_end = trimmed.find(|c: char| !c.is_ascii_digit())?;
if digits_end == 0 {
return None;
}
let after = &trimmed[digits_end..];
for sep in ['.', ')'] {
if let Some(rest) = after.strip_prefix(sep) {
if let Some(rest) = rest.strip_prefix(' ') {
return Some(rest);
}
}
}
None
}
fn strip_heading(line: &str) -> Option<&str> {
let hashes = line.chars().take_while(|&c| c == '#').count();
if (1..=6).contains(&hashes) {
let rest = &line[hashes..];
if let Some(rest) = rest.strip_prefix(' ') {
return Some(rest);
}
}
None
}
fn push_bold_run(blocks: &mut Vec<CommentBlock>, text: &str) {
if text.is_empty() {
return;
}
blocks.push(CommentBlock {
text: text.to_string(),
attributes: CommentAttributes {
bold: true,
..Default::default()
},
});
}
fn push_inline_runs(blocks: &mut Vec<CommentBlock>, line: &str) {
for run in parse_inline(line) {
blocks.push(run);
}
}
fn parse_inline(line: &str) -> Vec<CommentBlock> {
let mut runs: Vec<CommentBlock> = Vec::new();
let chars: Vec<char> = line.chars().collect();
let mut i = 0;
let mut plain = String::new();
let flush_plain = |plain: &mut String, runs: &mut Vec<CommentBlock>| {
if !plain.is_empty() {
runs.push(CommentBlock {
text: std::mem::take(plain),
attributes: CommentAttributes::default(),
});
}
};
while i < chars.len() {
let c = chars[i];
if c == '`' {
if let Some(close) = find_char(&chars, i + 1, '`') {
flush_plain(&mut plain, &mut runs);
let text: String = chars[i + 1..close].iter().collect();
runs.push(CommentBlock {
text,
attributes: CommentAttributes {
code: true,
..Default::default()
},
});
i = close + 1;
continue;
}
}
if c == '*' && i + 1 < chars.len() && chars[i + 1] == '*' {
if let Some(close) = find_double_star(&chars, i + 2) {
flush_plain(&mut plain, &mut runs);
let inner: String = chars[i + 2..close].iter().collect();
for mut run in parse_inline(&inner) {
run.attributes.bold = true;
runs.push(run);
}
i = close + 2;
continue;
}
}
plain.push(c);
i += 1;
}
flush_plain(&mut plain, &mut runs);
runs
}
fn find_char(chars: &[char], from: usize, needle: char) -> Option<usize> {
(from..chars.len()).find(|&j| chars[j] == needle)
}
fn find_double_star(chars: &[char], from: usize) -> Option<usize> {
let mut j = from;
while j + 1 < chars.len() {
if chars[j] == '*' && chars[j + 1] == '*' {
return Some(j);
}
j += 1;
}
None
}
#[cfg(test)]
mod tests {
use super::*;
fn plain(text: &str) -> CommentBlock {
CommentBlock {
text: text.to_string(),
attributes: CommentAttributes::default(),
}
}
#[test]
fn plain_paragraph() {
let blocks = markdown_to_comment_blocks("hello world");
assert_eq!(blocks, vec![plain("hello world")]);
}
#[test]
fn inline_code_splits_runs() {
let blocks = markdown_to_comment_blocks("the `SecretBackend` trait");
assert_eq!(
blocks,
vec![
plain("the "),
CommentBlock {
text: "SecretBackend".to_string(),
attributes: CommentAttributes {
code: true,
..Default::default()
},
},
plain(" trait"),
]
);
}
#[test]
fn inline_code_does_not_fragment_surrounding_prose() {
let blocks = markdown_to_comment_blocks("a `x` b `y` c");
let texts: Vec<&str> = blocks.iter().map(|b| b.text.as_str()).collect();
assert_eq!(texts, vec!["a ", "x", " b ", "y", " c"]);
assert!(blocks[1].attributes.code);
assert!(blocks[3].attributes.code);
}
#[test]
fn bold_run() {
let blocks = markdown_to_comment_blocks("a **bold** b");
assert_eq!(blocks[1].text, "bold");
assert!(blocks[1].attributes.bold);
}
#[test]
fn bold_with_nested_inline_code() {
let blocks = markdown_to_comment_blocks("**`SecretBackend`**");
assert_eq!(blocks.len(), 1);
assert_eq!(blocks[0].text, "SecretBackend");
assert!(blocks[0].attributes.bold);
assert!(blocks[0].attributes.code);
}
#[test]
fn bold_with_mixed_inner_content() {
let blocks = markdown_to_comment_blocks("**`backend.rs`** (new)");
assert_eq!(blocks[0].text, "backend.rs");
assert!(blocks[0].attributes.bold && blocks[0].attributes.code);
assert_eq!(blocks[1].text, " (new)");
assert!(blocks[1].attributes.is_empty());
}
#[test]
fn fenced_code_block() {
let body = "```rust\nlet x = 1;\nlet y = 2;\n```";
let blocks = markdown_to_comment_blocks(body);
assert_eq!(blocks.len(), 4);
assert_eq!(blocks[0].text, "let x = 1;");
assert!(blocks[1].attributes.code_block.is_some());
assert_eq!(blocks[1].text, "\n");
assert_eq!(blocks[2].text, "let y = 2;");
assert!(blocks[3].attributes.code_block.is_some());
}
#[test]
fn fenced_code_block_does_not_parse_inline() {
let body = "```\na `b` **c**\n```";
let blocks = markdown_to_comment_blocks(body);
assert_eq!(blocks[0].text, "a `b` **c**");
assert!(blocks[0].attributes.is_empty());
}
#[test]
fn bullet_list() {
let body = "- one\n- two";
let blocks = markdown_to_comment_blocks(body);
assert_eq!(blocks[0].text, "one");
assert_eq!(
blocks[1].attributes.list.as_ref().map(|l| l.list.as_str()),
Some("bullet")
);
assert_eq!(blocks[2].text, "two");
assert_eq!(
blocks[3].attributes.list.as_ref().map(|l| l.list.as_str()),
Some("bullet")
);
}
#[test]
fn bullet_list_item_keeps_inline_code() {
let blocks = markdown_to_comment_blocks("- first with `code`");
assert_eq!(blocks[0].text, "first with ");
assert!(blocks[1].attributes.code);
assert_eq!(blocks[1].text, "code");
assert!(blocks[2].attributes.list.is_some());
}
#[test]
fn ordered_list() {
let body = "1. one\n2. two";
let blocks = markdown_to_comment_blocks(body);
assert_eq!(blocks[0].text, "one");
assert_eq!(
blocks[1].attributes.list.as_ref().map(|l| l.list.as_str()),
Some("ordered")
);
}
#[test]
fn heading_becomes_bold() {
let blocks = markdown_to_comment_blocks("## Done");
assert_eq!(blocks[0].text, "Done");
assert!(blocks[0].attributes.bold);
}
#[test]
fn unterminated_inline_code_is_literal() {
let blocks = markdown_to_comment_blocks("a `b c");
assert_eq!(blocks, vec![plain("a `b c")]);
}
#[test]
fn serializes_attributes_with_clickup_shape() {
let body = "- item\n```\ncode\n```\n`x`";
let blocks = markdown_to_comment_blocks(body);
let json = serde_json::to_string(&blocks).unwrap();
assert!(json.contains(r#""list":{"list":"bullet"}"#));
assert!(json.contains(r#""code-block":{"code-block":"plain"}"#));
assert!(json.contains(r#""code":true"#));
assert!(json.contains(r#"{"text":"item"}"#));
}
#[test]
fn trailing_blank_lines_are_trimmed() {
for body in ["a", "a\n", "a\n\n", "a\n\n\n"] {
let blocks = markdown_to_comment_blocks(body);
assert_eq!(
blocks,
vec![plain("a")],
"body {body:?} should trim every trailing plain newline"
);
}
}
#[test]
fn trailing_block_separator_is_preserved() {
let blocks = markdown_to_comment_blocks("- item\n");
let last = blocks.last().unwrap();
assert!(last.attributes.list.is_some());
}
#[test]
fn empty_body_yields_no_blocks() {
assert!(markdown_to_comment_blocks("").is_empty());
}
}