Render view comments as Markdown with pulldown-cmark.

Comment cards now turn links, code, and emphasis into real HTML instead of escaped source text.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-08-04 16:14:34 +08:00
1 parent 7848180454
commit 31a4b69bbd
5 files changed
+101 -6

No files matched your search

+21 -1
View File
@@ -308,10 +308,30 @@ section.block > h2 {
font-size: 0.92rem;
display: block;
margin-top: 0.35rem;
white-space: pre-line;
line-height: 1.55;
max-width: 42em;
}
.comment-text > :first-child { margin-top: 0; }
.comment-text > :last-child { margin-bottom: 0; }
.comment-text p { margin: 0.45em 0; }
.comment-text a {
color: var(--ink);
text-decoration: underline;
text-underline-offset: 0.12em;
border-bottom: none;
}
.comment-text a:hover { opacity: 0.72; }
.comment-text code {
font-family: var(--mono);
font-size: 0.86em;
background: var(--fill);
padding: 0.08em 0.28em;
border-radius: 4px;
}
.comment-text ul, .comment-text ol {
margin: 0.4em 0;
padding-left: 1.35em;
}
code.expr {
font-family: var(--mono);
font-size: 0.84rem;
+31 -4
View File
@@ -8,6 +8,7 @@ use crate::ast::{
};
use crate::lex::{classify_source, ClassifiedLine, LineKind};
use crate::view::html::escape;
use pulldown_cmark::{html, Options, Parser};
pub struct FileViewModel {
#[allow(dead_code)]
@@ -83,11 +84,12 @@ fn emit_comments(lines: &[ClassifiedLine], from_line: u32, before_line: u32) ->
if paragraph.is_empty() {
return;
}
let text = paragraph.join("\n");
// Soft-wrapped source lines → one Markdown paragraph (spaces between).
let text = paragraph.join(" ");
paragraph.clear();
s.push_str(&format!(
"<div class=\"card comment-card\"><span class=\"badge\">comment</span><span class=\"comment-text\">{}</span></div>",
escape(&text)
"<div class=\"card comment-card\"><span class=\"badge\">comment</span><div class=\"comment-text\">{}</div></div>",
comment_markdown_to_html(&text)
));
};
@@ -113,6 +115,18 @@ fn emit_comments(lines: &[ClassifiedLine], from_line: u32, before_line: u32) ->
s
}
/// Render narrative comments as Markdown (links, emphasis, code, lists, …).
fn comment_markdown_to_html(md: &str) -> String {
let mut opts = Options::empty();
opts.insert(Options::ENABLE_STRIKETHROUGH);
opts.insert(Options::ENABLE_TABLES);
opts.insert(Options::ENABLE_TASKLISTS);
let parser = Parser::new_ext(md, opts);
let mut out = String::new();
html::push_html(&mut out, parser);
out
}
fn render_fun(fun: &Function, lines: &[ClassifiedLine], depth: usize) -> (String, u32) {
let mut s = String::new();
let nest = if depth > 0 { " nested" } else { "" };
@@ -444,7 +458,20 @@ mod tests {
let html = render_module_structure(&module, src);
let cards = html.matches("comment-card").count();
assert_eq!(cards, 2, "blank line should split comment paragraphs: {html}");
assert!(html.contains("段首说明\n续行仍属同段"));
assert!(html.contains("段首说明 续行仍属同段") || html.contains("段首说明</p>"));
assert!(html.contains("下一段"));
}
#[test]
fn comments_render_markdown_links() {
let src = "见[仓库](https://github.com/cflmy/marqdo)与 `code`\n\n# main\n\n> print text=x\n";
let module = crate::parse::parse_source(src).unwrap();
let html = render_module_structure(&module, src);
assert!(
html.contains("<a href=\"https://github.com/cflmy/marqdo\">仓库</a>"),
"{html}"
);
assert!(html.contains("<code>code</code>"), "{html}");
assert!(!html.contains("[仓库](https://github.com/cflmy/marqdo)"));
}
}