Skip to content

Commit ffc403d

Browse files
committed
render markdown body only on request
Signed-off-by: karthik2804 <[email protected]>
1 parent 57401ce commit ffc403d

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/content.rs

+1
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ fn translate_relative_links(event: markdown::Event, path_info: String) -> markdo
300300

301301
/// The envelope for a page's content
302302
/// The head contains front matter, while the body is the markdown body of the document.
303+
#[derive(Serialize, Deserialize)]
303304
pub struct Content {
304305
/// The front matter for this content.
305306
pub head: Head,

src/template.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use {
77
use crate::rhai_engine::custom_rhai_engine_init;
88

99
use super::content::{Content, Head};
10+
use handlebars::handlebars_helper;
1011
use serde::{Deserialize, Serialize};
1112

1213
/// The name of the default template.
@@ -81,9 +82,9 @@ pub struct PageValues {
8182
}
8283

8384
impl From<Content> for PageValues {
84-
fn from(mut c: Content) -> Self {
85+
fn from(c: Content) -> Self {
8586
PageValues {
86-
body: c.render_markdown(&None),
87+
body: c.body,
8788
head: c.head,
8889
published: c.published,
8990
}
@@ -102,6 +103,11 @@ pub struct Renderer<'a> {
102103
handlebars: handlebars::Handlebars<'a>,
103104
}
104105

106+
handlebars_helper!(render_markdown: |content: Content|{
107+
let mut content = content;
108+
content.render_markdown(&None)
109+
});
110+
105111
#[cfg(feature = "server")]
106112
impl<'a> Renderer<'a> {
107113
/// Create a new renderer with the necessary directories attached.
@@ -136,6 +142,7 @@ impl<'a> Renderer<'a> {
136142
pub fn load_template_dir(&mut self) -> Result<(), anyhow::Error> {
137143
#[cfg(feature = "server")]
138144
self.register_helpers();
145+
self.handlebars.register_helper("render_markdown", Box::new(render_markdown));
139146

140147
// If there is a theme, load the templates provided by it first
141148
// Allows for user defined templates to take precedence
@@ -150,6 +157,7 @@ impl<'a> Renderer<'a> {
150157
Ok(())
151158
}
152159

160+
153161
/// Load the scripts directory
154162
pub fn load_script_dir(&mut self) -> anyhow::Result<()> {
155163
let mut theme_scripts: Vec<PathBuf> = Vec::new();

0 commit comments

Comments
 (0)