index : livejrnl-rs.git

ascending towards madness

author berrysweet <berrysweet@noreply.codeberg.org> 2023-07-06 20:13:29.0 +00:00:00
committer berrysweet <berrysweet@noreply.codeberg.org> 2023-07-06 20:13:29.0 +00:00:00
commit
808e429c10f71d0b06ce92dd46fcaabef78769b8 [patch]
tree
153a2e2e75d517bc491658dca23395f639896112
parent
29bd3d3e645a3cdaa5d80fdac59645eed60f0026
download
808e429c10f71d0b06ce92dd46fcaabef78769b8.tar.gz

feat(filters): add markdown filter



Diff

 Cargo.toml             |  3 ++-
 src/main.rs            |  1 +
 src/util/filters.rs    | 17 +++++++++++++++++
 src/util/templating.rs |  6 +++++-
 4 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index b92992c..67b69d7 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -13,4 +13,5 @@ serde_json = "1.0.100"
walkdir = "2.3.3"
read_pipe = "0.1.2"
minijinja = "1.0.3"
gumdrop = "0.8.1"
\ No newline at end of file
gumdrop = "0.8.1"
markdown = "1.0.0-alpha.10"
\ No newline at end of file
diff --git a/src/main.rs b/src/main.rs
index 40cdecb..a80aaa8 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -9,6 +9,7 @@ mod data {
}
mod util {
	pub mod templating;
	pub mod filters;
	pub mod journal;
}

diff --git a/src/util/filters.rs b/src/util/filters.rs
new file mode 100644
index 0000000..7657126
--- /dev/null
+++ b/src/util/filters.rs
@@ -0,0 +1,17 @@
use markdown::CompileOptions;

pub fn markdown(value: String) -> String {
	let result = markdown::to_html_with_options(&value, &markdown::Options {
		compile: CompileOptions {
			allow_dangerous_html: true,
			allow_dangerous_protocol: true,
			..CompileOptions::default()
		},
		..markdown::Options::gfm()
	});
	result.unwrap_or_default()
}

pub fn register_filters(env: &mut minijinja::Environment<'_>) {
	env.add_filter("markdown", markdown);
}
\ No newline at end of file
diff --git a/src/util/templating.rs b/src/util/templating.rs
index 4eb6293..d17185e 100644
--- a/src/util/templating.rs
+++ b/src/util/templating.rs
@@ -8,6 +8,8 @@ use minijinja::{Environment, context};
use crate::data::formats;
use crate::util::journal;

use super::filters;

pub fn get_templates(template: String) -> HashMap<String, String> {
	let template_path: PathBuf = PathBuf::from_str(&template).unwrap();
	let mut templates: HashMap<String, String> = HashMap::new();
@@ -25,12 +27,14 @@ pub fn get_templates(template: String) -> HashMap<String, String> {
}

pub fn render_html(config: formats::RenderConfig, journal: journal::Journal, templates: &HashMap<String, String>) -> String {
	let mut env: Environment<'_> = Environment::new();
	let env: &mut Environment<'_> = &mut Environment::new();

	for (name, source) in templates {
		let _ = env.add_template(&name, &source).unwrap();
	}

	filters::register_filters(env);

	let tmpl: minijinja::Template<'_, '_> = env.get_template(&config.template_index).unwrap();
	tmpl.render(context!(config => config, journal => journal)).unwrap_or_default()
}
\ No newline at end of file