diff --git a/Cargo.lock b/Cargo.lock index 5fad90e..ed1ce6c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1120,6 +1120,16 @@ dependencies = [ "encoding", ] +[[package]] +name = "gh-emoji" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30ad64b43d48c1745c0059e5ba223816eb599eec8956c668fc0a31f6b9cd799e" +dependencies = [ + "phf", + "regex", +] + [[package]] name = "ghash" version = "0.5.0" @@ -3276,6 +3286,7 @@ dependencies = [ "derive_more", "dotenv", "gettext", + "gh-emoji", "poise", "r2d2", "r2d2_sqlite", diff --git a/Cargo.toml b/Cargo.toml index 3349ba5..856106b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ comrak = "0.18.0" derive_more = "0.99.17" dotenv = "0.15.0" gettext = "0.4.0" +gh-emoji = "1.0.7" poise = "0.5.5" r2d2 = "0.8.10" r2d2_sqlite = "0.22.0" diff --git a/src/models/challenge.rs b/src/models/challenge.rs index b5756dd..7afbaca 100644 --- a/src/models/challenge.rs +++ b/src/models/challenge.rs @@ -1,13 +1,15 @@ use core::panic; use std::str::FromStr; +use comrak::format_html; use serde::{Deserialize, Deserializer, Serialize}; use serde_yaml::Value; -use crate::{prelude::*}; +use crate::{prelude::*, utils::furigana_to_html}; #[derive(Serialize, Deserialize)] pub struct Challenge { + pub text: Option, pub japanese: Option>>>, pub english: Option>, pub song: Option, @@ -29,10 +31,11 @@ impl Challenge { options }; let arena = Arena::new(); + let challenge_text = fs::read_to_string(format!("content/challenges/{number}.md")) + .expect("Couldn't find challenge file"); let root = parse_document( &arena, - & (fs::read_to_string(format!("content/challenges/{number}.md")) - .expect("Couldn't find challenge file") + &(challenge_text.clone() // comrak can't find frontmatter if there's only frontmatter and no newline at end // TODO: Open issue in comrak + "\n"), @@ -45,7 +48,13 @@ impl Challenge { let lines: Vec<&str> = frontmatter.trim().lines().collect(); lines[1..lines.len() - 1].join("\n") }; - let challenge: Challenge = serde_yaml::from_str(&frontmatter).unwrap(); + let mut challenge: Challenge = serde_yaml::from_str(&frontmatter).unwrap(); + //challenge.text = Some(challenge_text.replace(&frontmatter, "").trim().to_owned()); + let mut html = vec![]; + format_html(root, &ComrakOptions::default(), &mut html) + .expect("Failed to format HTML"); + challenge.text = Some(furigana_to_html(&gh_emoji::Replacer::new() + .replace_all(&String::from_utf8(html).unwrap()))); challenge } else { panic!("No frontmatter!") diff --git a/templates/index.html.tera b/templates/index.html.tera index 2730895..ee38826 100644 --- a/templates/index.html.tera +++ b/templates/index.html.tera @@ -100,6 +100,7 @@ {% endfor %} {% endif %} + {{ content.text | safe }} {% if content.translation %}

Translation