pub fn render_viewer_html<T: Content>(
    context: Context,
    content: T
) -> Result<String, NoteError>
Expand description

Returns the HTML rendition of the note file located in context.path with the template TMPL_HTML_VIEWER (can be replaced at runtime).

use tpnote_lib::config::TMPL_HTML_VAR_NOTE_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_NOTE_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_NOTE_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 day"
subtitle: "Note"
---
Body text
"#;
let notefile = temp_dir().join("20221030-My day--Note.md");
fs::write(&notefile, raw.as_bytes()).unwrap();

// Start test
let mut context = Context::from(&notefile);
// We do not inject any JavaScript.
context.insert(TMPL_HTML_VAR_NOTE_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"))