diff --git a/Cargo.lock b/Cargo.lock index c20dd3e..968bd71 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -202,6 +202,15 @@ version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "325918d6fe32f23b19878fe4b34794ae41fc19ddbe53b10571a4874d44ffd39b" +[[package]] +name = "caseless" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b6fd507454086c8edfd769ca6ada439193cdb209c7681712ef6275cccbfe5d8" +dependencies = [ + "unicode-normalization", +] + [[package]] name = "cc" version = "1.2.4" @@ -231,6 +240,19 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "comrak" +version = "0.33.0" +source = "git+https://github.com/kivikakk/comrak#428ad9aff440e9b76050794b07234ff258548eac" +dependencies = [ + "caseless", + "entities", + "memchr", + "slug", + "typed-arena", + "unicode_categories", +] + [[package]] name = "core-foundation-sys" version = "0.8.7" @@ -371,6 +393,12 @@ dependencies = [ "syn", ] +[[package]] +name = "deunicode" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "339544cc9e2c4dc3fc7149fd630c5f22263a4fdf18a98afd0075784968b5cf00" + [[package]] name = "digest" version = "0.10.7" @@ -405,6 +433,12 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "entities" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5320ae4c3782150d900b79807611a59a99fc9a1d61d686faafc24b93fc8d7ca" + [[package]] name = "estimated_read_time" version = "1.0.0" @@ -665,10 +699,10 @@ dependencies = [ "atom_syndication", "axum", "chrono", + "comrak", "dotenv", "estimated_read_time", "lazy_static", - "markdown", "mime_guess", "nom", "rust-embed", @@ -736,15 +770,6 @@ version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" -[[package]] -name = "markdown" -version = "1.0.0-alpha.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6491e6c702bf7e3b24e769d800746d5f2c06a6c6a2db7992612e0f429029e81" -dependencies = [ - "unicode-id", -] - [[package]] name = "matchit" version = "0.7.3" @@ -1099,6 +1124,16 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "slug" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "882a80f72ee45de3cc9a5afeb2da0331d58df69e4e7d8eeb5d3c7784ae67e724" +dependencies = [ + "deunicode", + "wasm-bindgen", +] + [[package]] name = "smallvec" version = "1.13.2" @@ -1174,6 +1209,21 @@ dependencies = [ "syn", ] +[[package]] +name = "tinyvec" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "022db8904dfa342efe721985167e9fcd16c29b226db4397ed752a761cfce81e8" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + [[package]] name = "tokio" version = "1.42.0" @@ -1248,6 +1298,12 @@ dependencies = [ "once_cell", ] +[[package]] +name = "typed-arena" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6af6ae20167a9ece4bcb41af5b80f8a1f1df981f6391189ce00fd257af04126a" + [[package]] name = "typenum" version = "1.17.0" @@ -1316,18 +1372,27 @@ version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7e51b68083f157f853b6379db119d1c1be0e6e4dec98101079dec41f6f5cf6df" -[[package]] -name = "unicode-id" -version = "0.3.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10103c57044730945224467c09f71a4db0071c123a0648cc3e818913bde6b561" - [[package]] name = "unicode-ident" version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" +[[package]] +name = "unicode-normalization" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5033c97c4262335cded6d6fc3e5c18ab755e1a3dc96376350f3d8e9f009ad956" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "unicode_categories" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39ec24b3121d976906ece63c9daad25b85969647682eee313cb5779fdd69e14e" + [[package]] name = "version_check" version = "0.9.5" diff --git a/Cargo.toml b/Cargo.toml index b471102..3fdaa56 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,9 +9,9 @@ edition = "2021" [dependencies] axum = "0.7.7" +comrak = { git = "https://github.com/kivikakk/comrak", default-features = false } dotenv = "0.15.0" lazy_static = "1.5.0" -markdown = "1.0.0-alpha.21" mime_guess = "2.0.5" nom = "7.1.3" atom_syndication = "0.12.5" diff --git a/justfile b/justfile index c9d0536..df3d9cc 100644 --- a/justfile +++ b/justfile @@ -15,3 +15,6 @@ run: nix: nix build -L + +test: + cargo test diff --git a/src/data/blog.rs b/src/data/blog.rs index 8ec244c..9a9d7f1 100644 --- a/src/data/blog.rs +++ b/src/data/blog.rs @@ -2,6 +2,7 @@ use atom_syndication::{ ContentBuilder, EntryBuilder, FeedBuilder, LinkBuilder, PersonBuilder, TextBuilder, }; use chrono::TimeZone; +use comrak::{markdown_to_html, Options as ComrakOptions}; use lazy_static::lazy_static; use nom::{branch::permutation, IResult}; use rust_embed::{Embed, EmbeddedFile}; @@ -71,32 +72,21 @@ impl Post { let content = Self::parse_content(raw_content); - match content { - Ok(content) => { - let read_time_seconds = estimated_read_time::text(&content, &ERT_OPTIONS).seconds(); - let read_time = read_time_seconds / 60; - - let ret = Self { - title: meta.title, - date: meta.date, - description: meta.description, - tags: meta.tags, - slug: String::new(), - content, - read_time, - id: None, - }; - - Ok(ret) - } - Err(e) => { - eprintln!("{e:?}"); - Err(Box::new(std::io::Error::new( - std::io::ErrorKind::Other, - e.to_string(), - ))) - } - } + let read_time_seconds = estimated_read_time::text(&content, &ERT_OPTIONS).seconds(); + let read_time = read_time_seconds / 60; + + let ret = Self { + title: meta.title, + date: meta.date, + description: meta.description, + tags: meta.tags, + slug: String::new(), + content, + read_time, + id: None, + }; + + Ok(ret) } fn parse_field<'a>(input: &'a str, field: &'a str) -> IResult<&'a str, String> { @@ -137,8 +127,15 @@ impl Post { Ok((input, tags.iter().map(|s| (*s).to_string()).collect())) } - fn parse_content(input: &str) -> Result { - markdown::to_html_with_options(input, &markdown::Options::gfm()) + fn parse_content(input: &str) -> String { + let mut opts = ComrakOptions::default(); + opts.extension.strikethrough = true; + opts.extension.table = true; + opts.extension.header_ids = Some("#".to_string()); + opts.extension.underline = true; + opts.extension.alerts = true; + + markdown_to_html(input, &opts) } } @@ -309,7 +306,7 @@ mod tests { assert_eq!(post.tags, vec!["test", "post"]); assert_eq!( post.content, - "

Hello, World!

\n

This is a test post.

\n" + "

Hello, World!

\n

This is a test post.

\n" ); } diff --git a/templates/pages/post.tera b/templates/pages/post.tera index 914320e..7091f70 100644 --- a/templates/pages/post.tera +++ b/templates/pages/post.tera @@ -1,6 +1,6 @@ {% extends "layouts/base.tera" %} -{% block title %}{% set title = "Blog > " ~ post.title %}{% endblock %} +{% block title %}{% set title = post.title %}{% endblock %} {% block description %}{% set description = post.description %}{% endblock %} {% block additionalfoot %}