1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//! Markdown/HTML (de)serialization between text and [`tdoc::Document`].
//!
//! rutle works on `tdoc::Document` directly and leaves (de)serialization to
//! `tdoc`. These thin wrappers are the entry points piki-gui needs for the
//! clipboard and note load/save.
use std::io::Cursor;
use tdoc::{Document, html, markdown};
/// Parse markdown text into a [`tdoc::Document`]. Empty document on error.
pub fn markdown_to_document(src: &str) -> Document {
markdown::parse(Cursor::new(src.as_bytes())).unwrap_or_else(|_| Document::new())
}
/// Serialize a [`tdoc::Document`] into markdown text.
pub fn document_to_markdown(doc: &Document) -> String {
let mut buffer: Vec<u8> = Vec::new();
if let Err(err) = markdown::write(&mut buffer, doc) {
eprintln!("Failed to serialize document to markdown: {}", err);
return String::new();
}
let markdown = String::from_utf8(buffer).unwrap_or_default();
// An empty note is represented in the editor by a single empty paragraph
// (see `StructuredRichUI::set_content_from_markdown`), which serializes to a
// lone newline. Normalize that — and any whitespace-only document — back to
// the empty string so a freshly created, untouched note stays empty on disk
// and autosave does not persist a file for it.
if markdown.trim().is_empty() {
return String::new();
}
markdown
}
/// Serialize a [`tdoc::Document`] into an HTML fragment.
pub fn document_to_html(doc: &Document) -> String {
let mut buffer: Vec<u8> = Vec::new();
if let Err(err) = html::write(&mut buffer, doc) {
eprintln!("Failed to serialize document to HTML: {}", err);
return String::new();
}
String::from_utf8(buffer).unwrap_or_default()
}
#[cfg(test)]
mod tests {
use super::*;
/// The seeded empty paragraph a fresh note carries (so formatting commands
/// work immediately) must serialize back to an empty string — otherwise an
/// untouched new note would be persisted to disk as a lone newline.
#[test]
fn seeded_empty_paragraph_serializes_to_empty_string() {
let mut doc = Document::new();
doc.add_paragraph(tdoc::Paragraph::new_text());
assert_eq!(document_to_markdown(&doc), "");
}
#[test]
fn empty_document_serializes_to_empty_string() {
assert_eq!(document_to_markdown(&Document::new()), "");
}
/// A note with real content is untouched — in particular its trailing
/// newline is preserved, so existing notes are not spuriously rewritten.
#[test]
fn real_content_round_trips_unchanged() {
let doc = markdown_to_document("# Title\n\nBody text\n");
assert_eq!(document_to_markdown(&doc), "# Title\n\nBody text\n");
}
}