Function string_to_html

Source
pub fn string_to_html(str: &str) -> Result<String>
Expand description

Converts an Editor.js JSON string into an HTML representation.

This function takes a JSON-formatted string generated by Editor.js and converts it into HTML. It is useful for rendering rich text content stored in Editor.js format in web applications.

§Arguments

  • str - A string slice containing the Editor.js JSON.

§Returns

  • Ok(String) - The generated HTML string if the conversion is successful.
  • Err(anyhow::Error) - An error if the JSON is invalid or cannot be parsed.

§Example

let editor_json = serde_json::json!({
    "time": 1612345678901i64,
    "blocks": [
        { "type": "paragraph", "data": { "text": "Hello, world!" } },
        { "type": "header", "data": { "text": "Welcome", "level": 2 } }
    ],
    "version": "2.22.2"
});
let html_content = editorjs2html::string_to_html(&editor_json.to_string()).unwrap();
assert_eq!(html_content, "<div class=\"js-para\"><p>Hello, world!</p></div><div class=\"js-head\"><h2>Welcome</h2></div>");

§Errors

This function may return an error if:

  • The input is not valid JSON.
  • The JSON structure does not conform to Editor.js format.
  • The function encounters an unsupported block type.

§Notes

  • The function assumes that the input follows the standard Editor.js JSON format.
  • Not all Editor.js block types may be supported in the implementation.