{
let mut out = Vec::new();
if let Some((href, title)) = &m.link {
let mut s = Spec::new("a");
s.attrs.push(("href", href.clone()));
if let Some(t) = title {
s.attrs.push(("title", t.clone()));
}
out.push(s);
}
if m.em {
out.push(Spec::new("em"));
}
if m.strong {
out.push(Spec::new("strong"));
}
if m.code {
out.push(Spec::new("code"));
}
out
}
/// The top of the note as the editor shows it before it starts: at most `blocks`
/// blocks and about `chars` characters of text, in one page element. Reads only the
/// head of the body.
pub fn preview_html(doc: &LoroDoc, blocks: usize, chars: usize) -> String {
let text = flat::body(doc);
let n = text.len_unicode().min(chars + blocks + 1);
let delta = text.slice_delta(0, n, PosType::Unicode).unwrap_or_default();
let mut flat = flat::read_delta(delta);
flat.truncate(blocks);
let mut out = String::from("");
let mut budget = chars;
blocks_html(&flat::nest(&flat), &mut out, &mut budget);
out.push_str("
");
out
}
/// Blocks as the editor's markup, with at most `budget` characters of text.
pub fn blocks_html(blocks: &[Block], out: &mut String, budget: &mut usize) {
for b in blocks {
if *budget == 0 {
return;
}
block_html(b, out, budget);
}
}
fn block_html(b: &Block, out: &mut String, budget: &mut usize) {
let (spec, inl): (Spec, &[Inline]) = match b {
Block::Paragraph(inl) => (leaf_spec(Kind::Para, None), inl),
Block::Heading(n, inl) => (leaf_spec(Kind::Heading((*n).clamp(1, 6) as u8), None), inl),
Block::Rule => {
open(&leaf_spec(Kind::Rule, None), out);
return;
}
Block::Code { lang, text } => {
let spec = leaf_spec(Kind::Code, lang.as_deref());
open(&spec, out);
out.push_str("");
let s = cut(text, budget);
escape(s, out);
if s.is_empty() || s.ends_with('\n') {
trailing_break(out);
}
out.push_str("");
return;
}
Block::Quote(inner) => {
open(&wrap_spec(Wrap::Quote, 1), out);
blocks_html(inner, out, budget);
close("blockquote", out);
return;
}
Block::List { ordered, start, items } => {
let spec = wrap_spec(if *ordered { Wrap::Ordered } else { Wrap::Bullet }, *start);
open(&spec, out);
for item in items {
out.push_str("");
blocks_html(item, out, budget);
out.push_str("");
}
close(spec.tag, out);
return;
}
};
open(&spec, out);
inline_html(inl, out, budget);
close(spec.tag, out);
}
/// A textblock's content. An empty one, or one ending in a break, gets a trailing
/// `
` to show its last line.
pub fn inline_html(inl: &[Inline], out: &mut String, budget: &mut usize) {
let mut last_break = true;
for i in inl {
match i {
Inline::Text(s, m) => {
let s = cut(s, budget);
if s.is_empty() {
continue;
}
let wraps = mark_specs(m);
for w in &wraps {
open(w, out);
}
escape(s, out);
for w in wraps.iter().rev() {
close(w.tag, out);
}
last_break = false;
}
Inline::HardBreak => {
out.push_str("
");
last_break = true;
}
Inline::Image { src, alt, title } => {
let spec = image_spec(src, alt, title.as_deref());
open(&spec, out);
if !is_void(spec.tag) {
close(spec.tag, out);
}
last_break = false;
}
}
}
if last_break {
trailing_break(out);
}
}
fn trailing_break(out: &mut String) {
out.push_str("
");
}
/// The head of `s` that fits the budget, which it takes from.
fn cut<'a>(s: &'a str, budget: &mut usize) -> &'a str {
let s = match s.char_indices().nth(*budget) {
Some((at, _)) => &s[..at],
None => s,
};
*budget -= s.chars().count().min(*budget);
s
}
fn open(spec: &Spec, out: &mut String) {
out.push('<');
out.push_str(spec.tag);
for (k, v) in &spec.attrs {
out.push(' ');
out.push_str(k);
out.push_str("=\"");
escape(v, out);
out.push('"');
}
out.push('>');
}
fn close(tag: &str, out: &mut String) {
out.push_str("");
out.push_str(tag);
out.push('>');
}
pub fn escape(s: &str, out: &mut String) {
for c in s.chars() {
match c {
'&' => out.push_str("&"),
'<' => out.push_str("<"),
'>' => out.push_str(">"),
'"' => out.push_str("""),
c => out.push(c),
}
}
}