//! The plain block and inline tree of a note: what the markdown import produces, //! what `flat::nest` reads back, and what `flat::flatten` writes. use std::collections::HashMap; use loro::{LoroResult, LoroText, LoroValue}; use crate::marks::Marks; use crate::schema::{MARK_CODE, MARK_EM, MARK_LINK, MARK_STRONG}; #[derive(Clone, PartialEq, Debug)] pub enum Inline { Text(String, Marks), HardBreak, Image { src: String, alt: String, title: Option, }, } #[derive(Clone, PartialEq, Debug)] pub enum Block { Paragraph(Vec), Heading(i64, Vec), Quote(Vec), Code { lang: Option, text: String, }, Rule, List { ordered: bool, start: i64, items: Vec>, }, } /// The peer id of docs the server builds: above every client's, which are random. /// Loro looks up the change store entry at or below a commit's id on every local /// commit, and decodes it: below a big import, that is the import, megabytes for each /// keystroke (loro 1.17). pub const BUILDER_PEER: u64 = u64::MAX - 1; pub fn apply_marks( text: &LoroText, range: std::ops::Range, marks: &Marks, ) -> LoroResult<()> { if marks.strong { text.mark(range.clone(), MARK_STRONG, empty_map())?; } if marks.em { text.mark(range.clone(), MARK_EM, empty_map())?; } if marks.code { text.mark(range.clone(), MARK_CODE, empty_map())?; } if let Some((href, title)) = &marks.link { let mut map: HashMap = HashMap::new(); map.insert("href".to_owned(), LoroValue::from(href.as_str())); let title_value = match title { Some(t) => LoroValue::from(t.as_str()), None => LoroValue::Null, }; map.insert("title".to_owned(), title_value); text.mark(range, MARK_LINK, LoroValue::from(map))?; } Ok(()) } fn empty_map() -> LoroValue { LoroValue::from(HashMap::::new()) }