We are using the new incarnation of the markdown crate that has not replaced the old one yet. The source code explains it and referres us to the version number of the most recent release of the new crate.
examples/markdow-to-html/Cargo.toml
[package]
name = "markdow-to-html"
version = "0.1.0"
edition = "2021"
[dependencies]
markdown = "1.0.0-alpha.17"
The code
In the code I wrapped the parameters in a new function call. This is how I use the crate in the Code Maven SSG the code that generates the Rust Maven web site.
Setting the allow_dangerous_html
means we can embed HTML tags in the Markdown and they will be passed on without escaping the HTML tags.
examples/markdow-to-html/src/main.rs
fn main() -> Result<(), Box<dyn std::error::Error>> {
let markdown = std::fs::read_to_string("content.md")?;
match markdown2html(&markdown) {
Ok(html) => println!("{html}"),
Err(err) => return Err(Box::<dyn std::error::Error>::from(format!("{err}"))),
}
Ok(())
}
fn markdown2html(content: &str) -> Result<String, markdown::message::Message> {
let html = markdown::to_html_with_options(
content,
&markdown::Options {
compile: markdown::CompileOptions {
allow_dangerous_html: true,
..markdown::CompileOptions::default()
},
..markdown::Options::gfm()
},
)?;
Ok(html)
}
Some simple markdown file
The following file was used for the example
examples/markdow-to-html/content.md
# Main title
This text is under the main title
## First subtitle
* apple
* banana
* peach
A [link to the markdown crate](https://crates.io/crates/markdown) that we use.
## Code snippet:
```rust
fn main() {
}
Command
There is a command in here: cargo run
to run the crate.
HTML
Some bold and italic and anything else.