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:
1 parent
7848180454
commit
31a4b69bbd
5 files changed
+101
-6
No files matched your search
Generated
+47
@@ -94,6 +94,12 @@ version = "1.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.13.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b588b76d00fde79687d7646a9b5bdf3cc0f655e0bbd080335a95d7e96f3587da"
|
||||
|
||||
[[package]]
|
||||
name = "bstr"
|
||||
version = "1.13.0"
|
||||
@@ -178,6 +184,15 @@ dependencies = [
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getopts"
|
||||
version = "0.2.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cfe4fbac503b8d1f88e6676011885f34b7174f46e59956bba534ba83abded4df"
|
||||
dependencies = [
|
||||
"unicode-width",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.4.1"
|
||||
@@ -216,6 +231,7 @@ dependencies = [
|
||||
"assert_cmd",
|
||||
"clap",
|
||||
"predicates",
|
||||
"pulldown-cmark",
|
||||
"thiserror",
|
||||
"tiny_http",
|
||||
]
|
||||
@@ -286,6 +302,25 @@ dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pulldown-cmark"
|
||||
version = "0.12.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f86ba2052aebccc42cbbb3ed234b8b13ce76f75c3551a303cb2bcffcff12bb14"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"getopts",
|
||||
"memchr",
|
||||
"pulldown-cmark-escape",
|
||||
"unicase",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pulldown-cmark-escape"
|
||||
version = "0.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "007d8adb5ddab6f8e3f491ac63566a7d5002cc7ed73901f72057943fa71ae1ae"
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.47"
|
||||
@@ -410,12 +445,24 @@ dependencies = [
|
||||
"log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicase"
|
||||
version = "2.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dbc4bc3a9f746d862c45cb89d705aa10f187bb96c76001afab07a0d35ce60142"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-width"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254"
|
||||
|
||||
[[package]]
|
||||
name = "utf8parse"
|
||||
version = "0.2.2"
|
||||
|
||||
@@ -14,6 +14,7 @@ path = "src/main.rs"
|
||||
[dependencies]
|
||||
anyhow = "1"
|
||||
clap = { version = "=4.4.18", features = ["derive"] }
|
||||
pulldown-cmark = "0.12"
|
||||
thiserror = "1"
|
||||
tiny_http = "0.12"
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ description: 用户文档首页 — 代码即文档
|
||||
|
||||
> 问候 marqdo
|
||||
|
||||
如果你是一个敏锐的程序员应该会察觉到一个小问题,这个问题的解决方案是像下面这样的,我其实并不知道这样解决是否正确,但是我暂且放在这里,欢迎你,敏锐的程序元朋友。
|
||||
如果你是一个敏锐的程序员应该会察觉到一个小问题,这个问题的解决方案是像下面这样的,目前来看这样的解决方案没有问题,我并不能很有远见的判断后续一定不会出错,所以我暂且放在这里,欢迎你,敏锐的程序员朋友。
|
||||
|
||||
> 问候 谁=marqdo user
|
||||
|
||||
|
||||
+21
-1
@@ -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
@@ -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)"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user