forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits

marks.rs39 lines, 1.2 KiBHistoryRawDownload
1//! Inline marks of a LoroText run, shared by the readers and the writer.
2
3use std::collections::HashMap;
4use std::hash::BuildHasher;
5
6use loro::LoroValue;
7
8use crate::tree::value_str;
9
10/// The marks on one run of text. `link` holds `(href, title)`.
11#[derive(PartialEq, Clone, Default, Debug)]
12pub struct Marks {
13 pub strong: bool,
14 pub em: bool,
15 pub code: bool,
16 pub link: Option<(String, Option<String>)>,
17}
18
19impl Marks {
20 /// Reads the marks from the attributes of a rich-text delta. A mark counts as on
21 /// unless its value is null or absent.
22 pub fn from_attrs<S: BuildHasher>(attrs: &HashMap<String, LoroValue, S>) -> Marks {
23 let on = |k: &str| attrs.get(k).is_some_and(|v| !matches!(v, LoroValue::Null));
24 let link = attrs.get("link").map(|v| {
25 let href = v.get_by_key("href").and_then(value_str).unwrap_or_default();
26 let title = v
27 .get_by_key("title")
28 .and_then(value_str)
29 .filter(|t| !t.is_empty());
30 (href, title)
31 });
32 Marks {
33 strong: on("strong"),
34 em: on("em"),
35 code: on("code"),
36 link,
37 }
38 }
39}