feat: markdown implementation #18

Merged
Anri merged 9 commits from feat/md into main 2023-04-11 01:25:45 +02:00
3 changed files with 38 additions and 11 deletions
Showing only changes of commit 2215f3e4e8 - Show all commits

20
Cargo.lock generated
View file

@ -468,6 +468,7 @@ dependencies = [
"minify-html",
"ramhorns",
"serde",
"serde_yaml",
"toml",
]
@ -1143,6 +1144,19 @@ dependencies = [
"serde",
]
[[package]]
name = "serde_yaml"
version = "0.9.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9d684e3ec7de3bf5466b32bd75303ac16f0736426e5a4e0d6e489559ce1249c"
dependencies = [
"indexmap",
"itoa",
"ryu",
"serde",
"unsafe-libyaml",
]
[[package]]
name = "sha1"
version = "0.10.5"
@ -1386,6 +1400,12 @@ version = "1.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36"
[[package]]
name = "unsafe-libyaml"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1865806a559042e51ab5414598446a5871b561d21b6764f2eabb0dd481d880a6"
[[package]]
name = "url"
version = "2.3.1"

View file

@ -15,6 +15,7 @@ actix-files = "0.6"
ramhorns = "0.14.0"
toml = "0.7.3"
serde = { version = "1.0.159", features = ["derive"] }
serde_yaml = "0.9"
minify-html = "0.10.8"
glob = "0.3.1"
markdown = "1.0.0-alpha.7"

View file

@ -1,4 +1,5 @@
use ramhorns::{Content, Ramhorns};
use serde::Deserialize;
#[derive(Clone)]
pub struct Template {
@ -21,16 +22,21 @@ enum FrontMatter<'a> {
}
impl FrontMatter<'_> {
fn parse(&self) -> String {
fn parse(&self) -> Option<Metadata> {
match self {
Self::Yaml(_val) => todo!(),
Self::Yaml(val) => serde_yaml::from_str(val).unwrap_or_default(),
Self::Toml(_val) => todo!(),
Self::Json(_val) => todo!(),
}
}
}
pub fn read_md(filename: &str) -> String {
#[derive(Deserialize)]
pub struct Metadata {
pub title: Option<String>,
}
pub fn read_md(filename: &str) -> (Option<Metadata>, String) {
// Read markdown file
let mut text = std::fs::read_to_string(filename).unwrap();
@ -53,22 +59,22 @@ pub fn read_md(filename: &str) -> String {
};
let md_tree = markdown::to_mdast(&text, &parse_option).unwrap();
let frontmatter = match &md_tree.children().unwrap()[0] {
markdown::mdast::Node::Yaml(v) => Some(FrontMatter::Yaml(&v.value).parse()),
markdown::mdast::Node::Toml(v) => Some(FrontMatter::Toml(&v.value).parse()),
markdown::mdast::Node::MdxjsEsm(v) => Some(FrontMatter::Json(&v.value).parse()),
let metadata = match &md_tree.children().unwrap()[0] {
markdown::mdast::Node::Yaml(v) => FrontMatter::Yaml(&v.value).parse(),
markdown::mdast::Node::Toml(v) => FrontMatter::Toml(&v.value).parse(),
markdown::mdast::Node::MdxjsEsm(v) => FrontMatter::Json(&v.value).parse(),
_ => None,
};
println!("{:#?}\nVS", &md_tree.children().unwrap()[0]);
println!("{:#?}", frontmatter);
// Convert to HTML
markdown::to_html_with_options(
let html = markdown::to_html_with_options(
&text,
&markdown::Options {
parse: parse_option,
compile: compile_option,
},
)
.unwrap()
.unwrap();
(metadata, html)
}