Function tpnote_lib::workflow::render_erroneous_content_html
source · pub fn render_erroneous_content_html<T: Content>(
context: Context,
note_erroneous_content: T
) -> Result<String, NoteError>Expand description
When the header can not be deserialized, the file located in
context.path is rendered as “Error HTML page”.
The erroneous content is rendered to html with
parse_hyperlinks::renderer::text_rawlinks2html and inserted in
the TMPL_HTML_VIEWER_ERROR template (can be replace at runtime).
This template expects the template variables TMPL_VAR_PATH
and TMPL_VAR_NOTE_JS and TMPL_VAR_NOTE_ERROR in context to be set.
NB: The value of TMPL_VAR_PATH equals context.path.
use tpnote_lib::config::LIB_CFG;
use tpnote_lib::config::TMPL_VAR_NOTE_ERROR;
use tpnote_lib::config::TMPL_VAR_NOTE_JS;
use tpnote_lib::content::Content;
use tpnote_lib::content::ContentString;
use tpnote_lib::context::Context;
use tpnote_lib::error::NoteError;
use tpnote_lib::workflow::render_erroneous_content_html;
use std::env::temp_dir;
use std::fs;
// Prepare test: create existing errorneous note file.
let raw_error = r#"---
title: "My day"
subtitle: "Note"
--
Body text
"#;
let notefile = temp_dir().join("20221030-My day--Note.md");
fs::write(¬efile, raw_error.as_bytes()).unwrap();
let mut context = Context::from(¬efile);
let e = NoteError::MissingFrontMatterField { field_name: "title".to_string() };
// Start test
let mut context = Context::from(¬efile);
// We do not inject any JavaScript.
context.insert(TMPL_VAR_NOTE_JS, &e.to_string());
// We simulate an error;
context.insert(TMPL_VAR_NOTE_ERROR, &e.to_string());
// Render.
// Read from file.
// You can plug in your own type (must impl. `Content`).
let content = ContentString::open(&context.path).unwrap();
let html = render_erroneous_content_html(
context, content).unwrap();
// Check the HTML rendition.
assert!(html.starts_with("<!DOCTYPE html>\n<html"))