Add markdown support

This commit is contained in:
2024-11-10 01:57:18 +01:00
parent eea2472f6b
commit cc99eecedd
6 changed files with 32 additions and 0 deletions

16
Cargo.lock generated
View File

@@ -1667,6 +1667,7 @@ dependencies = [
"include_dir", "include_dir",
"insta", "insta",
"loco-rs", "loco-rs",
"markdown",
"migration", "migration",
"rstest", "rstest",
"sea-orm", "sea-orm",
@@ -2433,6 +2434,15 @@ dependencies = [
"value-bag", "value-bag",
] ]
[[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]] [[package]]
name = "matchers" name = "matchers"
version = "0.1.0" version = "0.1.0"
@@ -5012,6 +5022,12 @@ version = "0.3.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08f95100a766bf4f8f28f90d77e0a5461bbdb219042e7679bebe79004fed8d75" checksum = "08f95100a766bf4f8f28f90d77e0a5461bbdb219042e7679bebe79004fed8d75"
[[package]]
name = "unicode-id"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10103c57044730945224467c09f71a4db0071c123a0648cc3e818913bde6b561"
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.12" version = "1.0.12"

View File

@@ -37,6 +37,7 @@ axum = { version = "0.7.5", features = ["multipart"] }
include_dir = "0.7" include_dir = "0.7"
uuid = { version = "1.6.0", features = ["v4"] } uuid = { version = "1.6.0", features = ["v4"] }
tracing-subscriber = { version = "0.3.17", features = ["env-filter", "json"] } tracing-subscriber = { version = "0.3.17", features = ["env-filter", "json"] }
markdown = "1.0.0-alpha.21"
# view engine i18n # view engine i18n
fluent-templates = { version = "0.8.0", features = ["tera"] } fluent-templates = { version = "0.8.0", features = ["tera"] }
@@ -45,6 +46,7 @@ axum-range = "0.4.0"
axum-extra = { version = "0.9.4", features = ["multipart", "typed-header", "cookie"] } axum-extra = { version = "0.9.4", features = ["multipart", "typed-header", "cookie"] }
bytes = "1.8.0" bytes = "1.8.0"
tera = "1.20.0" tera = "1.20.0"
# /view engine # /view engine
[[bin]] [[bin]]

7
src/filters/markdown.rs Normal file
View File

@@ -0,0 +1,7 @@
use std::collections::HashMap;
pub fn markdown_filter(value: &tera::Value, _: &HashMap<String, tera::Value>) -> tera::Result<tera::Value> {
let value = tera::from_value::<String>(value.clone())?;
let md = markdown::to_html(&value);
Ok(tera::to_value(md)?)
}

1
src/filters/mod.rs Normal file
View File

@@ -0,0 +1 @@
pub mod markdown;

View File

@@ -7,6 +7,8 @@ use loco_rs::{
}; };
use tracing::info; use tracing::info;
use crate::filters;
const I18N_DIR: &str = "assets/i18n"; const I18N_DIR: &str = "assets/i18n";
const I18N_SHARED: &str = "assets/i18n/shared.ftl"; const I18N_SHARED: &str = "assets/i18n/shared.ftl";
@@ -28,6 +30,9 @@ impl Initializer for ViewEngineInitializer {
tera_engine tera_engine
.tera .tera
.register_function("t", FluentLoader::new(arc)); .register_function("t", FluentLoader::new(arc));
tera_engine
.tera
.register_filter("markdown", filters::markdown::markdown_filter);
info!("locales loaded"); info!("locales loaded");
} }

View File

@@ -7,3 +7,4 @@ pub mod services;
pub mod tasks; pub mod tasks;
pub mod views; pub mod views;
pub mod workers; pub mod workers;
pub mod filters;