From 03f798d71508cf0fe1856fce6e107229065c06cc Mon Sep 17 00:00:00 2001 From: Gaurav Date: Tue, 2 Aug 2022 23:07:36 -0400 Subject: Replace `markdown` by `comrak`; Render support for nested dirs * README.md will be rendered at currently visiting directory instead of just in the root. * Rendering is now done by comrak, which seems heavy but has a lot more features. --- src/listing.rs | 8 ++++++-- src/renderer.rs | 13 +++++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/listing.rs b/src/listing.rs index 458744c..436add9 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -232,7 +232,7 @@ pub fn directory_listing( } let mut entries: Vec = Vec::new(); - let mut readme: Option = None; + let mut readme: Option = None; for entry in dir.path.read_dir()? { if dir.is_visible(&entry) || conf.show_hidden { @@ -285,7 +285,11 @@ pub fn directory_listing( )); // TODO: Pattern match? if conf.readme && file_name.to_lowercase() == "readme.md"{ - readme = Some(file_name); + let file_path = conf.path.canonicalize().unwrap() + .join(base.as_os_str().to_str().unwrap() + .strip_prefix("/").unwrap()) + .join(&file_name); + readme = Some(file_path); } } } else { diff --git a/src/renderer.rs b/src/renderer.rs index 775d7c8..5fdd2be 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -3,9 +3,10 @@ use chrono::{DateTime, Utc}; use chrono_humanize::Humanize; use clap::{crate_name, crate_version}; use maud::{html, Markup, PreEscaped, DOCTYPE}; +use std::path::PathBuf; use std::time::SystemTime; use strum::IntoEnumIterator; -use std::path::Path; +use comrak::{markdown_to_html, ComrakOptions}; use crate::auth::CurrentUser; use crate::listing::{Breadcrumb, Entry, QueryParameters, SortingMethod, SortingOrder}; @@ -14,7 +15,7 @@ use crate::{archive::ArchiveMethod, MiniserveConfig}; /// Renders the file listing pub fn page( entries: Vec, - readme: Option, + readme: Option, is_root: bool, query_params: QueryParameters, breadcrumbs: Vec, @@ -169,9 +170,13 @@ pub fn page( } @if readme.is_some() { div { - h3 { (readme.as_ref().unwrap()) } + h3 { (readme.as_ref().unwrap().file_name().unwrap() + .to_string_lossy().to_string()) } (PreEscaped - (markdown::file_to_html(Path::new(&readme.unwrap())).unwrap())); + (markdown_to_html( + &std::fs::read_to_string(readme.unwrap()) + .unwrap_or_else(|_| "Cannot read File.".to_string()), + &ComrakOptions::default()))); } } a.back href="#top" { -- cgit v1.2.3