Function tpnote_lib::workflow::render_viewer_html
source · pub fn render_viewer_html<T: Content>(
context: Context,
content: T
) -> Result<String, NoteError>Expand description
Returns the HTML rendition of a ContentString. The markup rendition
engine is determined, by the file extension of the variable context.path.
The resulting HTML and other HTML template variables originating from
context are inserted into the TMPL_HTML_VIEWER template (which can be
replaced at runtime) before being returned. This function is stateless.
use tpnote_lib::config::TMPL_HTML_VAR_VIEWER_DOC_JS;
use tpnote_lib::content::Content;
use tpnote_lib::content::ContentString;
use tpnote_lib::context::Context;
use tpnote_lib::workflow::render_viewer_html;
use std::env::temp_dir;
use std::fs;
use std::path::Path;
// Prepare test: create existing note file.
let raw = String::from(r#"---
title: "My day"
subtitle: "Note"
---
Body text
"#);
// Start test
let mut context = Context::from(Path::new("/path/to/note.md"));
// We do not inject any JavaScript.
context.insert(TMPL_HTML_VAR_VIEWER_DOC_JS, &"".to_string());
// Render.
let html = render_viewer_html::<ContentString>(context, raw.into())
.unwrap();
// Check the HTML rendition.
assert!(html.starts_with("<!DOCTYPE html>\n<html"))A more elaborated example that reads from disk:
use tpnote_lib::config::LIB_CFG;
use tpnote_lib::config::TMPL_HTML_VAR_VIEWER_DOC_JS;
use tpnote_lib::content::Content;
use tpnote_lib::content::ContentString;
use tpnote_lib::context::Context;
use tpnote_lib::workflow::render_viewer_html;
use std::env::temp_dir;
use std::fs;
// Prepare test: create existing note file.
let raw = r#"---
title: "My day2"
subtitle: "Note"
---
Body text
"#;
let notefile = temp_dir().join("20221030-My day2--Note.md");
fs::write(¬efile, raw.as_bytes()).unwrap();
// Start test
let mut context = Context::from(¬efile);
// We do not inject any JavaScript.
context.insert(TMPL_HTML_VAR_VIEWER_DOC_JS, &"".to_string());
// Render.
let content = ContentString::open(&context.path).unwrap();
// You can plug in your own type (must impl. `Content`).
let html = render_viewer_html(context, content).unwrap();
// Check the HTML rendition.
assert!(html.starts_with("<!DOCTYPE html>\n<html"))