From 406eb57031fba945e44d8465e668fa29f7bdc32c Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 17 Jan 2025 22:52:35 +0100 Subject: [PATCH] Fix crate-level docs not being rendered correctly if there is no readme: * Markdown was not rendered * Highlighting wasn't run as expected * Lines start were completely trimmed whereas they shouldn't --- src/db/add_package.rs | 5 ++++- src/web/highlight.rs | 3 +++ templates/crate/details.html | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/db/add_package.rs b/src/db/add_package.rs index 0080b15c9..eb00f9b7b 100644 --- a/src/db/add_package.rs +++ b/src/db/add_package.rs @@ -469,7 +469,10 @@ fn read_rust_doc(file_path: &Path) -> Result> { let line = line?; if line.starts_with("//!") { // some lines may or may not have a space between the `//!` and the start of the text - let line = line.trim_start_matches("//!").trim_start(); + let mut line = line.trim_start_matches("//!"); + if line.starts_with(' ') { + line = &line[1..]; + } if !line.is_empty() { rustdoc.push_str(line); } diff --git a/src/web/highlight.rs b/src/web/highlight.rs index 7b91a1234..d20b44b72 100644 --- a/src/web/highlight.rs +++ b/src/web/highlight.rs @@ -51,6 +51,9 @@ fn try_with_syntax(syntax: &SyntaxReference, code: &str) -> Result { fn select_syntax(name: Option<&str>, code: &str) -> &'static SyntaxReference { name.and_then(|name| { + if name.is_empty() { + return SYNTAXES.find_syntax_by_token("rust"); + } SYNTAXES.find_syntax_by_token(name).or_else(|| { name.rsplit_once('.') .and_then(|(_, ext)| SYNTAXES.find_syntax_by_token(ext)) diff --git a/templates/crate/details.html b/templates/crate/details.html index e2ae588cb..6e4318941 100644 --- a/templates/crate/details.html +++ b/templates/crate/details.html @@ -198,7 +198,7 @@ {# If there's not a readme then attempt to display the long description #} {%- elif let Some(rustdoc) = rustdoc -%} - {{ rustdoc|safe }} + {{ crate::web::markdown::render(rustdoc)|safe }} {%- endif -%}