-
-
Notifications
You must be signed in to change notification settings - Fork 294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rendering markdown files? #1355
Comments
It might be too much to ask for in miniserve, but there is the https://github.com/xyproto/algernon web server which can render markdown. |
Thank you for the alternative suggestion. Although I'd prefer miniserve
simplicity and CLI nature.
It might be too much to ask for in miniserve,
What would be the approximate difference in terms of source code
complexity and runtime resource usage between rendering Readme.md and
rendering an arbitrary markdown file named differently?
|
The code might be simple to write and integrate, but there is an added maintenance burden for every new feature, aka feature creep. Tomorrow the next person would want a word/powerpoint renderer. |
I actually think this would be fine for miniserve. A markdown renderer seems reasonable. We shouldn't add any other specific renderers, though. |
Since I had this actually opened in another tab... /// Load a markdown file, render to HTML, and return the response.
async fn md_path_to_html(path: &Path) -> Result<Response<Body>> {
// Render Markdown like GitHub
let mut options = ComrakOptions::default();
options.ext_autolink = true;
options.ext_header_ids = None;
options.ext_table = true;
options.ext_strikethrough = true;
options.ext_tagfilter = true;
options.ext_tasklist = true;
options.github_pre_lang = true;
options.ext_header_ids = Some("user-content-".to_string());
let buf = tokio::fs::read(path).await?;
let s = String::from_utf8(buf).map_err(|_| Error::MarkdownUtf8)?;
let html = comrak::markdown_to_html(&s, &options);
let cfg = HtmlCfg {
title: String::new(),
body: html,
};
let html = super::render_html(cfg)?;
Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_LENGTH, html.len() as u64)
.header(header::CONTENT_TYPE, mime::TEXT_HTML.as_ref())
.body(Body::from(html))
.map_err(Error::from)
} That's the implementation of the basic-http-server |
miniserve already renders Markdown using Comrak so this should be rather easy if there are any takers. :) |
About this. Just blindly converting any markdown file to rendered HTML would be easy with the mechanism we have for readme.md. But I think there needs to be a way to get the raw file contents as well. So I don't think we can convert Considering someone would want to add links to other markdown files to make a website/wiki of markdown file, I think either the normal file paths have to be converted to HTML and have a separate way to get raw, or we'd have to modify the links to other All that complication makes me think, miniserve might not be a good tool for that. Although having that feature would be nice, so if we agree on a simple flag (e.g. |
|
Modifying the URL like that seems simple. We can keep the directory listing pure, and modify the URLs in modified src/listing.rs @@ -237,6 +237,7 @@ pub fn directory_listing(
let mut entries: Vec<Entry> = Vec::new();
let mut readme: Option<(String, String)> = None;
let readme_rx: Regex = Regex::new("^readme([.](md|txt))?$").unwrap();
+ let md_link: Regex = Regex::new("href=\"(?<url>[.]/.*[.]md)\"").unwrap();
for entry in dir.path.read_dir()? {
if dir.is_visible(&entry) || conf.show_hidden {
@@ -292,10 +293,11 @@ pub fn directory_listing(
readme = Some((
file_name.to_string(),
if ext == "md" {
- markdown_to_html(
- &std::fs::read_to_string(entry.path())?,
- &ComrakOptions::default(),
- )
+ md_link.replace_all(
+ &markdown_to_html(
+ &std::fs::read_to_string(entry.path())?,
+ &ComrakOptions::default(),
+ ), "href=\"$url?render=true\"").to_string()
} else {
format!("<pre>{}</pre>", &std::fs::read_to_string(entry.path())?)
}, But I can't figure out how to make it serve the modified contents for each |
Well, the best thing would probably to register ad-hoc middle ware, since the files hook won't work. I intended to start on that and forgot it 😅 |
Sooo... |
Why not, just put up a PR and I'll have a look. |
I use
miniserve
to browse/usr/share/doc
and for this rendering the README.md file github style is great. Many times the whole documentation of a package is in markdown (.md) files.Since the capability is there I was wondering if an option to allow automatic rendering of markdown files would be a possible/useful feature to add?
The text was updated successfully, but these errors were encountered: