~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/hud

A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts

0
0
1863 lines · 71.6 KB Raw History
//! Text with structure, read into blocks: markdown for `RichText`
//! (CommonMark, or GFM with its tables, task lists, strikethrough, bare
//! links and footnotes), and a long-form document's content for `Document`
//! (Leaflet's pages of blocks, markpub's markdown, else its plain text as
//! paragraphs, else the content itself as JSON), as the web renderer's
//! Markdown and document/ components read them. Markdown's HTML stays text
//! but for `<details>` and its `<summary>`, its links open only pages (http
//! or https; an app's own page's its app's paths and mail too, `page`), and
//! its pictures show their alt text and load nothing, but
//! those the text's `pictures` name (`Picture`), drawn as blocks of their
//! own where a block may be (not in a heading, a table, a link or a
//! summary).
//!
//! A block is `{ kind, … }`, any of them with `align` (center, right or
//! justify, a Leaflet block's own):
//!
//! - `paragraph { runs }`; `heading { level, runs }`, 1 to 6 (a Leaflet
//!   header 2 to 4, as it was drawn).
//! - `list { ordered, start?, tight?, items: [{ check?, blocks }] }`:
//!   `start` where an ordered list starts elsewhere than 1, `tight` where its
//!   items' paragraphs are drawn without their space, `check` a task's.
//! - `code { text, lang? }`, `quote { blocks }`, `rule`, `table { head:
//!   [runs], rows: [[runs]] }`, `footnotes { items: [{ blocks }] }`, in the
//!   order they are first referred to (a reference reads `[n]`).
//! - `details { runs, blocks, expanded? }`: markdown's `<details>`, `runs`
//!   its summary, `blocks` what it hides, `expanded` where it starts open.
//! - `image { src, alt, ratio, width? }`, `width` the most CSS pixels it is
//!   drawn across, where its picture says; `gallery { images: [{ src, alt,
//!   ratio }] }`.
//! - `card { kicker, title, description?, image, open? }`, a link to
//!   something outside the text; `button { label, open? }`.
//! - `bskyPost { uri, quote?, fallback }`: a Bluesky post, `quote` the
//!   post as an embed's quote card (filled in by the tool, which fetches
//!   it, see `fill_posts`), `fallback` the block drawn without it.
//! - `gap` (Leaflet's empty lines), `unsupported { text }`, `json { value }`.

use super::*;
use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag, TagEnd};
use unicase::UniCase;

/// The formatting that covers a run.
#[derive(Clone, Copy, Default, PartialEq, Eq)]
struct Marks {
    bold: bool,
    italic: bool,
    code: bool,
    strike: bool,
    underline: bool,
    highlight: bool,
    /// A picture's alt text, in its place.
    alt: bool,
}

/// A run with its link or account and its formatting, each flag only when set.
fn marked(text: &str, kind: Option<(&str, &str)>, m: Marks) -> Value {
    let mut r = run(text, kind);
    for (key, on) in [
        ("bold", m.bold),
        ("italic", m.italic),
        ("code", m.code),
        ("strike", m.strike),
        ("underline", m.underline),
        ("highlight", m.highlight),
        ("alt", m.alt),
    ] {
        if on {
            r[key] = true.into();
        }
    }
    r
}

fn paragraph(runs: Vec<Value>) -> Value {
    json!({ "kind": "paragraph", "runs": runs })
}

fn unsupported(text: String) -> Value {
    json!({ "kind": "unsupported", "text": text })
}

// Markdown.

/// A picture markdown's images may name, by its blob's CID (an image whose
/// URL is `com.atproto.sync.getBlob`'s with that `cid`, on whatever host)
/// or by its own URL: drawn from `src`, at `ratio`, no wider than `width`.
pub(super) struct Picture {
    cid: String,
    url: String,
    src: Value,
    ratio: f64,
    width: Option<u64>,
}

impl Picture {
    /// The pictures a value lists, each `{ cid, url, width?, height? }`
    /// (`width` and `height` in CSS pixels), those whose URL the view may
    /// load.
    pub(super) fn all(found: Option<&Value>, ctx: &Ctx) -> Vec<Picture> {
        let listed = found.and_then(Value::as_array).into_iter().flatten();
        listed
            .filter_map(|p| {
                let src = source_of(Some(&p["url"]), ctx, false);
                let cid = str_at(p, "cid").filter(|_| !src.is_null())?;
                Some(Picture {
                    cid: cid.to_string(),
                    url: p["url"].as_str().unwrap_or_default().to_string(),
                    src,
                    ratio: ratio_of(p).unwrap_or(4.0 / 3.0),
                    width: whole(&p["width"]),
                })
            })
            .collect()
    }
}

/// The CID a `com.atproto.sync.getBlob` URL names; none for another URL.
fn blob_cid(dest: &str) -> Option<String> {
    let url = url::Url::parse(dest)
        .ok()
        .filter(|u| u.path().ends_with("/xrpc/com.atproto.sync.getBlob"))?;
    url.query_pairs()
        .find(|(k, _)| k == "cid")
        .map(|(_, v)| v.into_owned())
}

/// Markdown as blocks: CommonMark, or with `gfm` GitHub's extensions; no
/// picture drawn.
pub(super) fn markdown(source: &str, gfm: bool) -> Vec<Value> {
    pictured(source, gfm, &[])
}

/// Markdown as blocks, its images that name one of `pictures` drawn.
pub(super) fn pictured(source: &str, gfm: bool, pictures: &[Picture]) -> Vec<Value> {
    read(source, gfm, pictures, None)
}

/// An app's own page's Markdown (an operator's app file's `pages`) as
/// blocks, GFM: its links open pages, its app's own paths (`/support/
/// privacy`) on the app's site (`https://{site}`), and mail (`mailto:`),
/// as the operator wrote them, who is trusted; no picture drawn.
pub(super) fn page(source: &str, site: &str) -> Vec<Value> {
    read(source, true, &[], Some(site))
}

/// Markdown as blocks, as `pictured` reads it, with `site` an app's own
/// page's (`page`).
fn read(source: &str, gfm: bool, pictures: &[Picture], site: Option<&str>) -> Vec<Value> {
    let options = if gfm {
        Options::ENABLE_TABLES
            | Options::ENABLE_STRIKETHROUGH
            | Options::ENABLE_TASKLISTS
            | Options::ENABLE_FOOTNOTES
    } else {
        Options::empty()
    };
    let mut reader = Reader {
        gfm,
        pictures,
        site,
        ..Reader::default()
    };
    for event in Parser::new_ext(source, options) {
        reader.event(event);
    }
    reader.finish()
}

/// A block markdown has opened and not yet closed: what it is, and what is
/// in it so far (a list's items, a details block's hidden blocks, anything
/// else's blocks).
struct Open {
    kind: Opened,
    blocks: Vec<Value>,
    /// A task's check: only an item has one.
    check: Option<bool>,
}

enum Opened {
    Quote,
    List {
        start: Option<u64>,
        tight: bool,
    },
    Item,
    Note {
        label: String,
    },
    /// A `<details>`: whether it starts open, its summary, and whether the
    /// summary is still being read.
    Details {
        expanded: bool,
        summary: Vec<Value>,
        in_summary: bool,
    },
}

#[derive(Default)]
struct Table {
    head: Vec<Value>,
    rows: Vec<Value>,
    row: Vec<Value>,
}

/// What covers the text being read: its formatting, the page its link
/// opens, and whether it is in a link at all (no bare link is found there).
#[derive(Clone, Default, PartialEq)]
struct Style {
    marks: Marks,
    link: Option<String>,
    in_link: bool,
}

/// Markdown's events read into blocks. Text is gathered while its style
/// holds, so a bare link split across events is found whole.
#[derive(Default)]
struct Reader<'p> {
    gfm: bool,
    pictures: &'p [Picture],
    /// An app's own page's site, its domain (`page`): its paths and mail
    /// are links too.
    site: Option<&'p str>,
    blocks: Vec<Value>,
    open: Vec<Open>,
    runs: Vec<Value>,
    pending: String,
    pending_style: Style,
    strong: u32,
    em: u32,
    strike: u32,
    /// The links the text is in, each the page it opens (None for one that opens none).
    links: Vec<Option<String>>,
    /// How many pictures the text is in, and the outermost's alt text so
    /// far and URL.
    images: u32,
    alt: Option<String>,
    image: String,
    /// Whether the text is a heading's or a table's, where no block goes.
    runs_only: bool,
    code: Option<(Option<String>, String)>,
    html: Option<String>,
    table: Table,
    notes: Vec<(String, Vec<Value>)>,
    /// Footnotes' labels, in the order they are first referred to.
    refs: Vec<String>,
}

impl<'p> Reader<'p> {
    /// What a link to `dest` opens: a page (http or https); on an app's own
    /// page, a path of its site's (`/x`, not `//x`) there, or mail; else
    /// nothing.
    fn opens(&self, dest: &str) -> Option<String> {
        if http_url(dest) {
            return Some(dest.to_string());
        }
        let site = self.site?;
        if dest.starts_with('/') && !dest.starts_with("//") {
            return Some(format!("https://{site}{dest}"));
        }
        let mail = dest
            .strip_prefix("mailto:")
            .is_some_and(|to| to.contains('@'));
        mail.then(|| dest.to_string())
    }

    fn style(&self) -> Style {
        Style {
            marks: Marks {
                bold: self.strong > 0,
                italic: self.em > 0,
                strike: self.strike > 0,
                ..Marks::default()
            },
            link: self.links.last().cloned().flatten(),
            in_link: !self.links.is_empty(),
        }
    }

    fn event(&mut self, event: Event) {
        match event {
            Event::Start(tag) => self.start(tag),
            Event::End(tag) => self.end(tag),
            Event::Text(t)
            | Event::Html(t)
            | Event::InlineHtml(t)
            | Event::InlineMath(t)
            | Event::DisplayMath(t) => self.text(&t),
            Event::Code(t) => self.piece(
                &t,
                Marks {
                    code: true,
                    ..Marks::default()
                },
            ),
            Event::FootnoteReference(label) => {
                let n = match self.refs.iter().position(|r| same_label(r, &label)) {
                    Some(i) => i + 1,
                    None => {
                        self.refs.push(label.to_string());
                        self.refs.len()
                    }
                };
                self.piece(&format!("[{n}]"), Marks::default());
            }
            Event::SoftBreak => self.text(" "),
            Event::HardBreak => self.text("\n"),
            Event::Rule => {
                self.settle();
                self.push(json!({ "kind": "rule" }));
            }
            Event::TaskListMarker(done) => {
                // pulldown-cmark puts a task's marker first in its item.
                let item = self
                    .open
                    .last_mut()
                    .expect("a task's marker is in its item");
                item.check = Some(done);
            }
        }
    }

    fn start(&mut self, tag: Tag) {
        match tag {
            Tag::Paragraph => {
                self.settle();
                // A paragraph of its own in an item: its list is loose.
                let n = self.open.len();
                if n >= 2
                    && matches!(self.open[n - 1].kind, Opened::Item)
                    && let Opened::List { tight, .. } = &mut self.open[n - 2].kind
                {
                    *tight = false;
                }
            }
            Tag::Heading { .. } | Tag::Table(_) => {
                self.settle();
                self.runs_only = true;
            }
            Tag::BlockQuote(_) => self.enter(Opened::Quote),
            Tag::CodeBlock(kind) => {
                self.settle();
                let lang = match kind {
                    CodeBlockKind::Fenced(info) => {
                        info.split_whitespace().next().map(str::to_string)
                    }
                    CodeBlockKind::Indented => None,
                };
                self.code = Some((lang, String::new()));
            }
            Tag::HtmlBlock => {
                self.settle();
                self.html = Some(String::new());
            }
            Tag::List(start) => self.enter(Opened::List { start, tight: true }),
            Tag::Item => self.enter(Opened::Item),
            Tag::FootnoteDefinition(label) => self.enter(Opened::Note {
                label: label.to_string(),
            }),
            Tag::Emphasis => self.em += 1,
            Tag::Strong => self.strong += 1,
            Tag::Strikethrough => self.strike += 1,
            Tag::Link { dest_url, .. } => {
                let opens = self.opens(&dest_url);
                self.links.push(opens);
            }
            Tag::Image { dest_url, .. } => {
                self.images += 1;
                if self.alt.is_none() {
                    self.alt = Some(String::new());
                    self.image = dest_url.to_string();
                }
            }
            _ => {}
        }
    }

    fn end(&mut self, tag: TagEnd) {
        match tag {
            TagEnd::Heading(level) => {
                self.runs_only = false;
                let runs = self.take_runs();
                self.push(json!({ "kind": "heading", "level": level as u8, "runs": runs }));
            }
            TagEnd::BlockQuote(_) | TagEnd::List(_) | TagEnd::Item | TagEnd::FootnoteDefinition => {
                self.close_details();
                let open = self
                    .open
                    .pop()
                    .expect("markdown's ends close what its starts opened");
                self.close(open);
            }
            TagEnd::CodeBlock => {
                let (lang, mut text) = self
                    .code
                    .take()
                    .expect("a code block's end follows its start");
                if text.ends_with('\n') {
                    text.pop();
                }
                let mut b = json!({ "kind": "code", "text": text });
                if let Some(lang) = lang {
                    b["lang"] = lang.into();
                }
                self.push(b);
            }
            TagEnd::HtmlBlock => {
                let html = self
                    .html
                    .take()
                    .expect("an HTML block's end follows its start");
                self.html_block(&html);
            }
            TagEnd::TableCell => {
                let runs = self.take_runs();
                self.table.row.push(Value::Array(runs));
            }
            TagEnd::TableHead => self.table.head = std::mem::take(&mut self.table.row),
            TagEnd::TableRow => {
                let row = std::mem::take(&mut self.table.row);
                self.table.rows.push(Value::Array(row));
            }
            TagEnd::Table => {
                self.runs_only = false;
                let t = std::mem::take(&mut self.table);
                self.push(json!({ "kind": "table", "head": t.head, "rows": t.rows }));
            }
            TagEnd::Emphasis => self.em = self.em.saturating_sub(1),
            TagEnd::Strong => self.strong = self.strong.saturating_sub(1),
            TagEnd::Strikethrough => self.strike = self.strike.saturating_sub(1),
            TagEnd::Link => {
                self.links.pop();
            }
            TagEnd::Image => {
                self.images = self.images.saturating_sub(1);
                if self.images == 0
                    && let Some(alt) = self.alt.take()
                {
                    match self.picture() {
                        Some(p) => self.draw(p, alt),
                        None if alt.is_empty() => {}
                        None => self.piece(
                            &format!("[{alt}]"),
                            Marks {
                                alt: true,
                                ..Marks::default()
                            },
                        ),
                    }
                }
            }
            // A paragraph's end: its text made one. No other ends come, as
            // the extensions whose would are not enabled.
            _ => self.settle(),
        }
    }

    /// The picture the image just read names, where a block of its own
    /// may go: not in a heading, a table, a link or a details' summary.
    fn picture(&self) -> Option<&'p Picture> {
        let summary = matches!(
            self.open.last(),
            Some(Open {
                kind: Opened::Details {
                    in_summary: true,
                    ..
                },
                ..
            })
        );
        if self.runs_only || !self.links.is_empty() || summary {
            return None;
        }
        let pictures = self.pictures;
        let cid = blob_cid(&self.image);
        pictures
            .iter()
            .find(|p| p.url == self.image || cid.as_deref() == Some(p.cid.as_str()))
    }

    /// A picture as a block of its own, the text before it a paragraph.
    fn draw(&mut self, p: &Picture, alt: String) {
        self.settle();
        let mut b = json!({ "kind": "image", "src": p.src, "alt": alt, "ratio": p.ratio });
        if let Some(w) = p.width {
            b["width"] = w.into();
        }
        self.push(b);
    }

    /// Text where it is: a code block's, an HTML block's, a picture's alt,
    /// or a run's, gathered while its style holds.
    fn text(&mut self, s: &str) {
        if let Some(alt) = &mut self.alt {
            alt.push_str(s);
            return;
        }
        if let Some((_, code)) = &mut self.code {
            code.push_str(s);
            return;
        }
        if let Some(html) = &mut self.html {
            html.push_str(s);
            return;
        }
        let style = self.style();
        if style != self.pending_style {
            self.flush();
            self.pending_style = style;
        }
        self.pending.push_str(s);
    }

    /// A run of its own (code, a picture's alt, a footnote's number) in the
    /// style around it, with `extra` formatting.
    fn piece(&mut self, s: &str, extra: Marks) {
        if let Some(alt) = &mut self.alt {
            alt.push_str(s);
            return;
        }
        self.flush();
        let style = self.style();
        let marks = Marks {
            code: extra.code,
            alt: extra.alt,
            ..style.marks
        };
        let kind = style.link.as_deref().map(|u| ("link", u));
        self.push_run(marked(s, kind, marks));
    }

    /// The text gathered, as runs: a link's, or with GFM cut at its bare links.
    fn flush(&mut self) {
        if self.pending.is_empty() {
            return;
        }
        let text = std::mem::take(&mut self.pending);
        let Style {
            marks,
            link,
            in_link,
        } = std::mem::take(&mut self.pending_style);
        if let Some(url) = &link {
            self.push_run(marked(&text, Some(("link", url)), marks));
        } else if self.gfm && !in_link {
            for (part, url) in autolinks(&text) {
                self.push_run(marked(part, url.as_deref().map(|u| ("link", u)), marks));
            }
        } else {
            self.push_run(marked(&text, None, marks));
        }
    }

    /// A run after the others, one with the same link and formatting as the
    /// last joined to it.
    fn push_run(&mut self, r: Value) {
        if let (Some(Value::Object(last)), Value::Object(next)) = (self.runs.last_mut(), &r)
            && last.len() == next.len()
            && next
                .iter()
                .all(|(k, v)| k == "text" || last.get(k) == Some(v))
            && let (Some(Value::String(joined)), Some(more)) =
                (last.get_mut("text"), next["text"].as_str())
        {
            joined.push_str(more);
            return;
        }
        self.runs.push(r);
    }

    fn take_runs(&mut self) -> Vec<Value> {
        self.flush();
        std::mem::take(&mut self.runs)
    }

    /// A block into what holds it: an item into its list; a paragraph or
    /// heading in a summary being read, its runs.
    fn push(&mut self, b: Value) {
        match self.open.last_mut() {
            Some(Open {
                kind:
                    Opened::Details {
                        in_summary: true,
                        summary,
                        ..
                    },
                ..
            }) => {
                if let Some(Value::Array(runs)) = b.get("runs") {
                    if !summary.is_empty() {
                        summary.push(run(" ", None));
                    }
                    summary.extend(runs.iter().cloned());
                }
            }
            Some(open) => open.blocks.push(b),
            None => self.blocks.push(b),
        }
    }

    /// A block opened, after the text before it.
    fn enter(&mut self, kind: Opened) {
        self.settle();
        self.open.push(Open {
            kind,
            blocks: Vec::new(),
            check: None,
        });
    }

    /// A block closed, into what holds it; a footnote's kept for the end.
    fn close(&mut self, open: Open) {
        let Open {
            kind,
            blocks,
            check,
        } = open;
        let b = match kind {
            Opened::Quote => json!({ "kind": "quote", "blocks": blocks }),
            Opened::List { start, tight } => {
                let mut b = json!({ "kind": "list", "ordered": start.is_some(), "items": blocks });
                if let Some(s) = start.filter(|s| *s != 1) {
                    b["start"] = s.into();
                }
                if tight {
                    b["tight"] = true.into();
                }
                b
            }
            Opened::Item => {
                let mut item = json!({ "blocks": blocks });
                if let Some(c) = check {
                    item["check"] = c.into();
                }
                item
            }
            Opened::Note { label } => {
                self.notes.push((label, blocks));
                return;
            }
            Opened::Details {
                expanded, summary, ..
            } => {
                let mut b = json!({ "kind": "details", "runs": summary, "blocks": blocks });
                if expanded {
                    b["expanded"] = true.into();
                }
                b
            }
        };
        self.push(b);
    }

    /// An HTML block: `<details>`, `<summary>` and their ends open and
    /// close a details block, whose summary may hold markdown read before
    /// `</summary>`; the rest is drawn as the text it is.
    fn html_block(&mut self, html: &str) {
        let mut rest = html;
        while let Some((at, len, tag)) = details_tag(rest) {
            self.loose_html(&rest[..at]);
            rest = &rest[at + len..];
            match tag {
                DetailsTag::Open { expanded } => self.enter(Opened::Details {
                    expanded,
                    summary: Vec::new(),
                    in_summary: false,
                }),
                DetailsTag::Summary | DetailsTag::EndSummary => {
                    self.settle();
                    if let Some(Open {
                        kind: Opened::Details { in_summary, .. },
                        ..
                    }) = self.open.last_mut()
                    {
                        *in_summary = matches!(tag, DetailsTag::Summary);
                    }
                }
                DetailsTag::Close => {
                    self.close_detail();
                }
            }
        }
        self.loose_html(rest);
    }

    /// HTML outside the tags a details block is made of: in a summary its
    /// text, else drawn as it is, its lines run together as a paragraph's.
    fn loose_html(&mut self, html: &str) {
        if let Some(Open {
            kind:
                Opened::Details {
                    in_summary: true,
                    summary,
                    ..
                },
            ..
        }) = self.open.last_mut()
        {
            let text = html_text(html);
            if !text.is_empty() {
                if !summary.is_empty() {
                    summary.push(run(" ", None));
                }
                summary.push(run(&text, None));
            }
            return;
        }
        let text = html
            .lines()
            .map(str::trim)
            .filter(|l| !l.is_empty())
            .collect::<Vec<_>>()
            .join(" ");
        if !text.is_empty() {
            self.push(paragraph(vec![run(&text, None)]));
        }
    }

    /// The details block open innermost closed into what holds it, if it
    /// is innermost: one markdown opened inside it never closes it.
    fn close_detail(&mut self) -> bool {
        self.settle();
        let Some(details) = self
            .open
            .pop_if(|o| matches!(o.kind, Opened::Details { .. }))
        else {
            return false;
        };
        self.close(details);
        true
    }

    /// Details blocks left open where what holds them ends, closed.
    fn close_details(&mut self) {
        while self.close_detail() {}
    }

    /// Text left outside a paragraph (a tight list item's, a picture's
    /// neighbours) made one, without the space at its ends: none if that
    /// is all it is.
    fn settle(&mut self) {
        let mut runs = self.take_runs();
        if let Some(Value::String(t)) = runs.first_mut().map(|r| &mut r["text"]) {
            *t = t.trim_start().to_string();
        }
        if let Some(Value::String(t)) = runs.last_mut().map(|r| &mut r["text"]) {
            *t = t.trim_end().to_string();
        }
        runs.retain(|r| r["text"] != "");
        if !runs.is_empty() {
            self.push(paragraph(runs));
        }
    }

    fn finish(mut self) -> Vec<Value> {
        self.close_details();
        let refs = std::mem::take(&mut self.refs);
        // pulldown-cmark refers only to notes it has, so each is found.
        let items: Vec<Value> = refs
            .iter()
            .filter_map(|label| {
                let i = self.notes.iter().position(|(l, _)| same_label(l, label))?;
                let (_, blocks) = self.notes.swap_remove(i);
                Some(json!({ "blocks": blocks }))
            })
            .collect();
        if !items.is_empty() {
            self.blocks
                .push(json!({ "kind": "footnotes", "items": items }));
        }
        self.blocks
    }
}

/// Whether two footnote labels name the same note, as pulldown-cmark
/// matches them: by Unicode case folding.
fn same_label(a: &str, b: &str) -> bool {
    UniCase::new(a) == UniCase::new(b)
}

/// A tag a details block is made of.
enum DetailsTag {
    Open { expanded: bool },
    Close,
    Summary,
    EndSummary,
}

/// The first `<details>`, `<summary>` or end of one in HTML, any case: where
/// it starts, how long it is, and which; `<details open>` starts expanded.
fn details_tag(html: &str) -> Option<(usize, usize, DetailsTag)> {
    let mut from = 0;
    while let Some(at) = html[from..].find('<').map(|i| from + i) {
        from = at + 1;
        let (end, named) = match html[from..].strip_prefix('/') {
            Some(t) => (true, t),
            None => (false, &html[from..]),
        };
        for name in ["details", "summary"] {
            let Some(after) = named
                .get(..name.len())
                .filter(|n| n.eq_ignore_ascii_case(name))
                .map(|_| &named[name.len()..])
            else {
                continue;
            };
            let Some(gt) = after.find('>') else { continue };
            let attrs = &after[..gt];
            // `<detailsx>` is no details; an end tag has no attributes.
            let named_alone = if end {
                attrs.trim().is_empty()
            } else {
                attrs.is_empty() || attrs.starts_with(char::is_whitespace)
            };
            if !named_alone {
                continue;
            }
            let len = html.len() - after.len() + gt + 1 - at;
            let tag = match (name, end) {
                ("details", false) => DetailsTag::Open {
                    expanded: attrs.split_whitespace().any(|a| {
                        let key = a.split('=').next().unwrap_or(a);
                        key.eq_ignore_ascii_case("open")
                    }),
                },
                ("details", true) => DetailsTag::Close,
                (_, false) => DetailsTag::Summary,
                (_, true) => DetailsTag::EndSummary,
            };
            return Some((at, len, tag));
        }
    }
    None
}

/// HTML's text: its tags left out, its whitespace one space, and its
/// character references (`&amp;`, `&#183;`, a few others by name) read.
fn html_text(html: &str) -> String {
    let mut text = String::with_capacity(html.len());
    let mut chars = html.chars().peekable();
    while let Some(c) = chars.next() {
        if c == '<'
            && chars
                .peek()
                .is_some_and(|n| n.is_ascii_alphabetic() || *n == '/')
        {
            chars.by_ref().find(|&c| c == '>');
        } else {
            text.push(c);
        }
    }
    let text = text.split_whitespace().collect::<Vec<_>>().join(" ");
    let mut out = String::with_capacity(text.len());
    let mut rest = text.as_str();
    while let Some(i) = rest.find('&') {
        out.push_str(&rest[..i]);
        rest = &rest[i..];
        let read = rest[1..].find(';').filter(|&e| e <= 8).and_then(|e| {
            let c = match &rest[1..=e] {
                "amp" => '&',
                "lt" => '<',
                "gt" => '>',
                "quot" => '"',
                "apos" => '\'',
                "nbsp" => ' ',
                "middot" => '·',
                n => {
                    let n = n.strip_prefix('#')?;
                    let code = match n.strip_prefix(['x', 'X']) {
                        Some(hex) => u32::from_str_radix(hex, 16).ok()?,
                        None => n.parse().ok()?,
                    };
                    char::from_u32(code).filter(|&c| c != '\0')?
                }
            };
            Some((c, e + 2))
        });
        match read {
            Some((c, len)) => {
                out.push(c);
                rest = &rest[len..];
            }
            None => {
                out.push('&');
                rest = &rest[1..];
            }
        }
    }
    out.push_str(rest);
    out
}

/// Plain text cut at the links GFM finds bare in it: an http(s) URL, or
/// one starting `www.` (an http page), after the start, a space or one of
/// `(*_~`, up to a space or a `<`; trailing `?!.,:*_~'"` and a `)` it never
/// opened left off. Each part with the page it opens, if it is a link.
fn autolinks(text: &str) -> Vec<(&str, Option<String>)> {
    let lower = text.to_ascii_lowercase();
    let mut parts = Vec::new();
    let mut at = 0;
    let mut from = 0;
    while let Some(found) = ["http://", "https://", "www."]
        .iter()
        .filter_map(|p| lower[from..].find(p).map(|i| i + from))
        .min()
    {
        let before = text[..found].chars().next_back();
        if !before.is_none_or(|c| c.is_whitespace() || "(*_~".contains(c)) {
            from = found + 1;
            continue;
        }
        let end = text[found..]
            .find(|c: char| c.is_whitespace() || c == '<')
            .map_or(text.len(), |i| i + found);
        let mut url = &text[found..end];
        // Trailing punctuation, and a `)` it never opened, are not the URL's.
        while url.ends_with(['?', '!', '.', ',', ':', '*', '_', '~', '\'', '"'])
            || (url.ends_with(')') && url.matches(')').count() > url.matches('(').count())
        {
            url = &url[..url.len() - 1];
        }
        let href = if lower[found..].starts_with("www.") {
            (url.len() > "www.".len()).then(|| format!("http://{url}"))
        } else {
            Some(url.to_string())
        };
        let Some(href) = href.filter(|h| http_url(h)) else {
            from = end.max(found + 1);
            continue;
        };
        if found > at {
            parts.push((&text[at..found], None));
        }
        parts.push((url, Some(href)));
        at = found + url.len();
        from = at;
    }
    if at < text.len() {
        parts.push((&text[at..], None));
    }
    parts
}

// Documents.

pub(super) fn document(p: &Value, scope: &Value, ctx: &Ctx) -> R<Option<Value>> {
    let content = value(&p["value"], scope, present)?;
    let fallback = text_of(&p["fallback"], scope)?;
    let mut doc = Doc { ctx };
    let leaflet = content.filter(|c| c["$type"] == "pub.leaflet.content" && c["pages"].is_array());
    let markpub = content
        .filter(|c| c["$type"] == "at.markpub.markdown")
        .and_then(|c| c["text"]["markdown"].as_str());
    let (look, blocks) = if let Some(c) = leaflet {
        ("leaflet", doc.pages(&c["pages"]))
    } else if let Some(source) = markpub {
        ("markdown", markdown(source, true))
    } else {
        let shown = doc.plain(fallback.as_deref().unwrap_or_default());
        if !shown.is_empty() {
            ("plain", shown)
        } else if let Some(c) = content {
            ("plain", vec![json!({ "kind": "json", "value": c })])
        } else {
            return Err(Fail::Missing);
        }
    };
    let mut o = Map::new();
    o.insert("look".into(), look.into());
    o.insert("blocks".into(), Value::Array(blocks));
    Ok(Some(make("Document", o, vec![])))
}

/// The last part of a `$type`, after its `#` or last `.`: `text` for
/// `pub.leaflet.blocks.text`; empty for a value with none.
fn kind(v: &Value) -> &str {
    let t = v["$type"].as_str().unwrap_or("");
    t.rfind(['#', '.']).map_or(t, |i| &t[i + 1..])
}

/// A wrapper's alignment, as its block is drawn.
fn align(wrapper: &Value) -> Option<&'static str> {
    let a = wrapper["alignment"].as_str()?.to_lowercase();
    ["center", "right", "justify"]
        .into_iter()
        .find(|w| a.ends_with(w))
}

/// A URL's host (and port), for a website's card; the URL itself if none.
fn host_of(src: &str) -> String {
    url::Url::parse(src)
        .ok()
        .and_then(|u| {
            u.host_str().map(|h| match u.port() {
                Some(port) => format!("{h}:{port}"),
                None => h.to_string(),
            })
        })
        .unwrap_or_else(|| src.to_string())
}

/// The at-uri of the Bluesky post a bsky.app post URL names
/// (`https://bsky.app/profile/<handle-or-did>/post/<rkey>`).
fn bsky_post_uri(url: &str) -> Option<String> {
    let rest = url.trim().strip_prefix("https://bsky.app/profile/")?;
    let (who, rest) = rest.split_once('/')?;
    let rkey = rest.strip_prefix("post/")?;
    let rkey = rkey.strip_suffix('/').unwrap_or(rkey);
    let made_of = |s: &str, extra: &str| {
        !s.is_empty()
            && s.chars()
                .all(|c| c.is_ascii_alphanumeric() || extra.contains(c))
    };
    (made_of(who, ".:%_-") && made_of(rkey, "._:~-"))
        .then(|| format!("at://{}/app.bsky.feed.post/{rkey}", unescape(who)))
}

/// `%XX` escapes decoded; one that is not stays as written.
fn unescape(s: &str) -> String {
    let b = s.as_bytes();
    let mut out = Vec::with_capacity(b.len());
    let mut i = 0;
    while i < b.len() {
        let hex = (b[i] == b'%')
            .then(|| s.get(i + 1..i + 3))
            .flatten()
            .filter(|h| h.bytes().all(|c| c.is_ascii_hexdigit()))
            .and_then(|h| u8::from_str_radix(h, 16).ok());
        match hex {
            Some(byte) => {
                out.push(byte);
                i += 3;
            }
            None => {
                out.push(b[i]);
                i += 1;
            }
        }
    }
    String::from_utf8_lossy(&out).into_owned()
}

/// Text cut into paragraphs at its blank lines, each trimmed, none empty.
fn paragraphs(text: &str) -> Vec<&str> {
    let mut out = Vec::new();
    let mut start: Option<usize> = None;
    let mut end = 0;
    let mut at = 0;
    for line in text.split('\n') {
        if !line.trim().is_empty() {
            start.get_or_insert(at);
            end = at + line.len();
        } else if let Some(s) = start.take() {
            out.push(text[s..end].trim());
        }
        at += line.len() + 1;
    }
    if let Some(s) = start {
        out.push(text[s..end].trim());
    }
    out
}

/// Leaflet's `plaintext` cut into runs by its facets (UTF-8 byte ranges,
/// which may overlap, a bold word inside a link): a run begins wherever a
/// facet starts or ends, with every feature of those covering it. A link
/// opens a page (http or https), or an account (a DID), as a mention does;
/// any other is text. A code run is code alone.
fn leaflet_runs(text: &str, facets: &Value) -> Vec<Value> {
    let bytes = text.as_bytes();
    let marks: Vec<(usize, usize, &Value)> = facets
        .as_array()
        .into_iter()
        .flatten()
        .filter_map(|f| {
            let s = f["index"]["byteStart"].as_u64()? as usize;
            let e = f["index"]["byteEnd"].as_u64()? as usize;
            (s < e && e <= bytes.len()).then_some((s, e, &f["features"]))
        })
        .collect();
    let mut cuts: Vec<usize> = marks
        .iter()
        .flat_map(|m| [m.0, m.1])
        .chain([0, bytes.len()])
        .collect();
    cuts.sort_unstable();
    cuts.dedup();
    let mut runs = Vec::new();
    for w in cuts.windows(2) {
        let (from, to) = (w[0], w[1]);
        let mut m = Marks::default();
        let mut link = None;
        let mut mention = None;
        for (_, _, features) in marks.iter().filter(|(s, e, _)| *s <= from && *e >= to) {
            for f in features.as_array().into_iter().flatten() {
                let t = f["$type"].as_str().unwrap_or("");
                match &t[t.rfind('#').map_or(0, |i| i + 1)..] {
                    "link" => link = f["uri"].as_str().or(link),
                    "didMention" => mention = f["did"].as_str().or(mention),
                    "code" => m.code = true,
                    "bold" => m.bold = true,
                    "italic" => m.italic = true,
                    "underline" => m.underline = true,
                    "highlight" => m.highlight = true,
                    "strikethrough" => m.strike = true,
                    _ => {}
                }
            }
        }
        let kind = link.or(mention).and_then(|t| {
            if http_url(t) {
                Some(("link", t))
            } else {
                t.starts_with("did:").then_some(("actor", t))
            }
        });
        let m = if m.code {
            Marks {
                code: true,
                ..Marks::default()
            }
        } else {
            m
        };
        runs.push(marked(&String::from_utf8_lossy(&bytes[from..to]), kind, m));
    }
    runs
}

/// A document being read: what its links open.
struct Doc<'c, 'a> {
    ctx: &'c Ctx<'a>,
}

impl Doc<'_, '_> {
    /// What a link's target opens: a record, an account, a page; nothing else.
    fn follow(&self, target: &str) -> Option<Value> {
        if target.starts_with("at://") {
            return Some(self.ctx.record_call(target));
        }
        if target.starts_with("did:") {
            return Some(self.ctx.actor_call(target));
        }
        http_url(target).then(|| json!({ "url": target }))
    }

    /// A blob's picture, drawn small or large; no other value loads.
    fn blob(&self, v: &Value, small: bool) -> Value {
        if v.is_object() {
            source_of(Some(v), self.ctx, small)
        } else {
            Value::Null
        }
    }

    /// Plain text's paragraphs, their http(s) URLs links; one that is only
    /// a Bluesky post's URL that post, drawn in place.
    fn plain(&mut self, text: &str) -> Vec<Value> {
        paragraphs(text)
            .into_iter()
            .map(|para| {
                let shown = paragraph(link_runs(para));
                match bsky_post_uri(para) {
                    Some(uri) => json!({ "kind": "bskyPost", "uri": uri, "fallback": shown }),
                    None => shown,
                }
            })
            .collect()
    }

    /// Every page's blocks in order; a page with none says so.
    fn pages(&mut self, pages: &Value) -> Vec<Value> {
        let mut out = Vec::new();
        for page in pages.as_array().into_iter().flatten() {
            match page["blocks"].as_array() {
                Some(blocks) => out.extend(self.page(blocks)),
                None => {
                    let k = Some(kind(page)).filter(|k| !k.is_empty());
                    out.push(unsupported(format!(
                        "Unsupported page: {}",
                        k.unwrap_or("unknown")
                    )));
                }
            }
        }
        out
    }

    /// A page's blocks, each in its wrapper's alignment. Empty text blocks,
    /// which Leaflet uses for space, become one gap however many come
    /// together, and none at the start or end.
    fn page(&mut self, wrappers: &[Value]) -> Vec<Value> {
        let mut out: Vec<Value> = Vec::new();
        for wrapper in wrappers {
            let b = if wrapper["block"].is_object() {
                &wrapper["block"]
            } else {
                wrapper
            };
            let empty =
                kind(b) == "text" && b["plaintext"].as_str().is_none_or(|t| t.trim().is_empty());
            if empty {
                if out.last().is_some_and(|l| l["kind"] != "gap") {
                    out.push(json!({ "kind": "gap" }));
                }
                continue;
            }
            let mut shown = self.block(b);
            if let Some(a) = align(wrapper) {
                shown["align"] = a.into();
            }
            out.push(shown);
        }
        if out.last().is_some_and(|l| l["kind"] == "gap") {
            out.pop();
        }
        out
    }

    /// One Leaflet block (`pub.leaflet.blocks.*`); one of a kind not drawn
    /// says what it is.
    fn block(&mut self, b: &Value) -> Value {
        let text = b["plaintext"].as_str().unwrap_or("");
        match kind(b) {
            "text" => paragraph(leaflet_runs(text, &b["facets"])),
            "header" => {
                // As it was drawn: h2 for 1, h3 for 2, h4 for anything else.
                let level = match (b["level"].as_f64().unwrap_or(1.0), b["level"].as_u64()) {
                    (l, _) if l <= 1.0 => 2,
                    (_, Some(2)) => 3,
                    _ => 4,
                };
                json!({ "kind": "heading", "level": level, "runs": leaflet_runs(text, &b["facets"]) })
            }
            "blockquote" => {
                json!({ "kind": "quote", "blocks": [paragraph(leaflet_runs(text, &b["facets"]))] })
            }
            "code" => {
                let mut c = json!({ "kind": "code", "text": text });
                if let Some(lang) = str_at(b, "language") {
                    c["lang"] = lang.into();
                }
                c
            }
            "image" => json!({
                "kind": "image",
                "src": self.blob(&b["image"], false),
                "alt": b["alt"].as_str().unwrap_or(""),
                "ratio": ratio_of(&b["aspectRatio"]).unwrap_or(4.0 / 3.0),
            }),
            "imageGallery" => {
                let images: Vec<Value> = b["images"]
                    .as_array()
                    .into_iter()
                    .flatten()
                    .filter(|i| i.is_object())
                    .map(|i| {
                        json!({
                            "src": self.blob(&i["image"], true),
                            "alt": i["alt"].as_str(),
                            "ratio": ratio_of(&i["aspectRatio"]),
                        })
                    })
                    .collect();
                json!({ "kind": "gallery", "images": images })
            }
            k @ ("unorderedList" | "orderedList") => {
                let ordered = k == "orderedList";
                let start = b["startIndex"].as_u64().filter(|_| ordered);
                self.list(&b["children"], ordered, start)
            }
            "horizontalRule" => json!({ "kind": "rule" }),
            "bskyPost" => {
                let uri = str_at(&b["postRef"], "uri");
                let mut card = json!({ "kind": "card", "kicker": "Bluesky post", "title": "Open the post", "image": null });
                if let Some(uri) = uri {
                    card["description"] = uri.into();
                    if let Some(open) = self.follow(uri) {
                        card["open"] = open;
                    }
                }
                match uri {
                    Some(uri) => json!({ "kind": "bskyPost", "uri": uri, "fallback": card }),
                    None => card,
                }
            }
            "website" => {
                let src = b["src"].as_str().unwrap_or("");
                let host = host_of(src);
                let title = str_at(b, "title").map_or_else(|| host.clone(), str::to_string);
                let mut card = json!({
                    "kind": "card",
                    "kicker": host,
                    "title": title,
                    "image": self.blob(&b["previewImage"], true),
                });
                if let Some(d) = str_at(b, "description") {
                    card["description"] = d.into();
                }
                if let Some(open) = self.follow(src) {
                    card["open"] = open;
                }
                card
            }
            "button" => {
                let url = b["url"].as_str().unwrap_or("");
                let mut button =
                    json!({ "kind": "button", "label": str_at(b, "text").unwrap_or(url) });
                if let Some(open) = self.follow(url) {
                    button["open"] = open;
                }
                button
            }
            _ => unsupported(format!(
                "Unsupported block: {}",
                b["$type"].as_str().unwrap_or("unknown")
            )),
        }
    }

    /// A Leaflet list: each item's content block, and its own items under
    /// it, nested, of the same kind.
    fn list(&mut self, children: &Value, ordered: bool, start: Option<u64>) -> Value {
        let mut items = Vec::new();
        for item in children
            .as_array()
            .into_iter()
            .flatten()
            .filter(|i| i.is_object())
        {
            let mut blocks = Vec::new();
            if let Some(content) = item.get("content") {
                blocks.push(self.block(content));
            }
            if item["children"].as_array().is_some_and(|k| !k.is_empty()) {
                blocks.push(self.list(&item["children"], ordered, None));
            }
            items.push(json!({ "blocks": blocks }));
        }
        let mut list = json!({ "kind": "list", "ordered": ordered, "items": items });
        if let Some(s) = start.filter(|s| *s != 1) {
            list["start"] = s.into();
        }
        list
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn ctx<'a>(root: &'a Value, origins: &'a [String]) -> Ctx<'a> {
        Ctx {
            origins,
            thumbnails: vec![],
            trusted: true,
            root,
            report: None,
            inline: None,
            lexicon: None,
            handles: None,
            paged: Vec::new(),
            more: More::default(),
            me: None,
            people: None,
        }
    }

    /// Markdown's blocks as a RichText node sends them, held to the lexicon.
    fn checked_markdown(blocks: &[Value]) {
        check::checked(&json!({ "c": "RichText", "p": { "blocks": blocks } }));
    }

    #[test]
    fn markdown_keeps_html_as_text_shows_pictures_alt_and_opens_only_pages() {
        let blocks = markdown(
            "**a** <b>x</b> [m](mailto:a@b.test) [j](javascript:alert(1)) [![logo](https://e.test/x.png)](https://w.test)\n\n<div>\nhi\n</div>",
            false,
        );
        checked_markdown(&blocks);
        assert_eq!(
            Value::Array(blocks),
            json!([
                { "kind": "paragraph", "runs": [
                    { "text": "a", "bold": true },
                    { "text": " <b>x</b> m j " },
                    { "text": "[logo]", "link": "https://w.test", "alt": true },
                ] },
                { "kind": "paragraph", "runs": [{ "text": "<div> hi </div>" }] },
            ])
        );
    }

    /// An app's own page's Markdown is GFM whose links open its app's
    /// paths on its site and mail too; a path of another host (`//x`), mail
    /// to no one or a relative path opens nothing still.
    #[test]
    fn an_app_page_opens_its_sites_paths_and_mail() {
        let blocks = page(
            "[t](/support/terms) [m](mailto:a@b.test) [n](mailto:x) [p](//e.test) [h](https://a.test) [r](rel) ~~s~~",
            "grain.social",
        );
        checked_markdown(&blocks);
        assert_eq!(
            Value::Array(blocks),
            json!([{ "kind": "paragraph", "runs": [
                { "text": "t", "link": "https://grain.social/support/terms" },
                { "text": " " },
                { "text": "m", "link": "mailto:a@b.test" },
                { "text": " n p " },
                { "text": "h", "link": "https://a.test" },
                { "text": " r " },
                { "text": "s", "strike": true },
            ] }])
        );
    }

    /// Markdown's images that name a picture it is given, by its blob's CID
    /// on whatever PDS or by its URL, are drawn, each a block of its own
    /// splitting the paragraph it was in, at its size; one it is not given,
    /// one whose URL the view may not load, or one where no block goes (a
    /// heading, a table, a link, a summary) is its alt text.
    #[test]
    fn markdown_draws_the_pictures_it_is_given_where_a_block_goes() {
        let root = json!({});
        let origins = vec!["https://k.test".to_string()];
        let c = ctx(&root, &origins);
        let given = json!([
            { "cid": "bafa", "url": "https://k.test/a", "width": 400, "height": 200 },
            { "cid": "bafb", "url": "https://k.test/b" },
            { "cid": "bafc", "url": "https://evil.test/c" },
            { "url": "https://k.test/d" },
        ]);
        let pictures = Picture::all(Some(&given), &c);
        let blob = |cid: &str| {
            format!("https://pds.test/xrpc/com.atproto.sync.getBlob?did=did:plc:a&cid={cid}")
        };
        let a = blob("bafa");
        let source = format!(
            "Before ![shot]({a})\n![](https://k.test/b) after\n\n![evil]({c}) ![rel](x.png) ![get](https://pds.test/xrpc/com.atproto.sync.getRecord?cid=bafa)\n\n# ![h]({a})\n\n[![l]({a})](https://w.test)\n\n| ![t]({a}) |\n|---|\n| x |\n\n<details><summary>\n\n![s]({a})\n\n</summary>\n\n- ![in]({a})\n\n</details>",
            c = blob("bafc"),
        );
        let blocks = pictured(&source, true, &pictures);
        checked_markdown(&blocks);
        let src = |u: &str| json!({ "src": u, "fallback": null });
        let alt = |t: &str| json!({ "text": format!("[{t}]"), "alt": true });
        let shot = |alt: &str| json!({ "kind": "image", "src": src("https://k.test/a"), "alt": alt, "ratio": 2.0, "width": 400 });
        assert_eq!(
            Value::Array(blocks),
            json!([
                paragraph(vec![run("Before", None)]),
                shot("shot"),
                { "kind": "image", "src": src("https://k.test/b"), "alt": "", "ratio": 4.0 / 3.0 },
                paragraph(vec![run("after", None)]),
                paragraph(vec![alt("evil"), run(" ", None), alt("rel"), run(" ", None), alt("get")]),
                { "kind": "heading", "level": 1, "runs": [alt("h")] },
                paragraph(vec![json!({ "text": "[l]", "link": "https://w.test", "alt": true })]),
                { "kind": "table", "head": [[alt("t")]], "rows": [[[{ "text": "x" }]]] },
                { "kind": "details", "runs": [alt("s")], "blocks": [
                    { "kind": "list", "ordered": false, "tight": true, "items": [{ "blocks": [shot("in")] }] },
                ] },
            ])
        );
        assert!(markdown(&source, true).iter().all(|b| b["kind"] != "image"));
    }

    #[test]
    fn gfm_finds_bare_links_tasks_and_tables_where_commonmark_does_not() {
        let source = "see www.a.test/x. and (https://b.test/y)\n\n- [x] done\n- todo\n\n| h | ~~s~~ |\n|---|---|\n| `c` | d |";
        let gfm = markdown(source, true);
        checked_markdown(&gfm);
        assert_eq!(
            gfm[0]["runs"],
            json!([
                { "text": "see " }, { "text": "www.a.test/x", "link": "http://www.a.test/x" },
                { "text": ". and (" }, { "text": "https://b.test/y", "link": "https://b.test/y" }, { "text": ")" },
            ])
        );
        assert_eq!(gfm[1]["tight"], true);
        assert_eq!(gfm[1]["items"][0]["check"], true);
        assert!(gfm[1]["items"][1].get("check").is_none());
        assert_eq!(gfm[2]["head"][1], json!([{ "text": "s", "strike": true }]));
        assert_eq!(gfm[2]["rows"][0][0], json!([{ "text": "c", "code": true }]));
        let plain = markdown(source, false);
        checked_markdown(&plain);
        assert_eq!(plain[0]["runs"].as_array().unwrap().len(), 1);
        assert_eq!(plain[1]["kind"], "list");
        assert_eq!(plain[2]["kind"], "paragraph", "no table in CommonMark");
    }

    #[test]
    fn loose_lists_and_where_ordered_ones_start() {
        let blocks = markdown("3. a\n\n4. b\n   - c\n", false);
        checked_markdown(&blocks);
        let list = &blocks[0];
        assert_eq!(list["start"], 3);
        assert!(list.get("tight").is_none());
        let second = &list["items"][1]["blocks"];
        assert_eq!(second[0]["kind"], "paragraph");
        assert_eq!(second[1]["tight"], true);
        assert_eq!(second[1]["items"][0]["blocks"][0]["runs"][0]["text"], "c");
    }

    #[test]
    fn footnotes_follow_in_the_order_they_are_referred_to() {
        let blocks = markdown("x[^b] y[^a] z[^b]\n\n[^a]: A\n\n[^b]: B\n", true);
        checked_markdown(&blocks);
        assert_eq!(blocks[0]["runs"], json!([{ "text": "x[1] y[2] z[1]" }]));
        assert_eq!(blocks[1]["items"][0]["blocks"][0]["runs"][0]["text"], "B");
        assert_eq!(blocks[1]["items"][1]["blocks"][0]["runs"][0]["text"], "A");
    }

    /// A summary's HTML is its text, character references read (but not
    /// one that names nothing, NUL, or runs too long); summaries given twice
    /// run on; a tag only named like one, or an end tag with attributes, is
    /// text.
    #[test]
    fn a_summarys_html_reads_its_references_and_near_tags_are_text() {
        let source = "<details><summary>A &amp; &lt;b&gt; &quot;q&quot; &apos;s&apos;&nbsp;x &#65;&#x42;&#X43; &#0; &#xZZ; &#q; &bogus; & &toolongentity;</summary><summary>more</summary>\n<detailsx>\n</details x>\n\n</details>";
        let blocks = markdown(source, true);
        checked_markdown(&blocks);
        assert_eq!(
            Value::Array(blocks),
            json!([{ "kind": "details", "runs": [
                { "text": "A & <b> \"q\" 's' x ABC &#0; &#xZZ; &#q; &bogus; & &toolongentity;" },
                { "text": " " }, { "text": "more" },
            ], "blocks": [{ "kind": "paragraph", "runs": [{ "text": "<detailsx> </details x>" }] }] }])
        );
    }

    /// A summary's paragraphs run on, a block with no text in it is left
    /// out, and a summary outside any details is text.
    #[test]
    fn a_summarys_paragraphs_run_on_and_one_outside_details_is_text() {
        let source = "<details><summary>\n\none\n\n---\n\ntwo\n\n</summary>\n\nbody\n\n</details>\n\n<summary>s</summary>";
        let blocks = markdown(source, true);
        checked_markdown(&blocks);
        assert_eq!(
            Value::Array(blocks),
            json!([
                { "kind": "details", "runs": [{ "text": "one" }, { "text": " " }, { "text": "two" }], "blocks": [
                    { "kind": "paragraph", "runs": [{ "text": "body" }] },
                ] },
                { "kind": "paragraph", "runs": [{ "text": "s" }] },
            ])
        );
    }

    #[test]
    fn details_hide_their_markdown_under_their_summary() {
        let source = "<details>\n<summary>Claude Opus 5.5 &middot; <b>high</b></summary>\n\n**a**\n\n- b\n\n</details>\n\n<DETAILS open><summary>\n\n*one*\n\n</summary>\nloose <i>html</i>\n</details>\n\n> <details><summary>q</summary>\n>\n> c\n\nafter";
        let blocks = markdown(source, true);
        checked_markdown(&blocks);
        assert_eq!(
            Value::Array(blocks),
            json!([
                { "kind": "details", "runs": [{ "text": "Claude Opus 5.5 · high" }], "blocks": [
                    { "kind": "paragraph", "runs": [{ "text": "a", "bold": true }] },
                    { "kind": "list", "ordered": false, "tight": true, "items": [
                        { "blocks": [{ "kind": "paragraph", "runs": [{ "text": "b" }] }] },
                    ] },
                ] },
                { "kind": "details", "expanded": true, "runs": [{ "text": "one", "italic": true }], "blocks": [
                    { "kind": "paragraph", "runs": [{ "text": "loose <i>html</i>" }] },
                ] },
                // One left open closes where its quote ends.
                { "kind": "quote", "blocks": [
                    { "kind": "details", "runs": [{ "text": "q" }], "blocks": [
                        { "kind": "paragraph", "runs": [{ "text": "c" }] },
                    ] },
                ] },
                { "kind": "paragraph", "runs": [{ "text": "after" }] },
            ])
        );
    }

    #[test]
    fn leaflet_facets_overlap_and_links_open_pages_or_accounts_alone() {
        let text = "héllo world";
        let at = "héllo ".len() as u64;
        let facets = json!([
            { "index": { "byteStart": 0, "byteEnd": at + 5 }, "features": [{ "$type": "pub.leaflet.richtext.facet#link", "uri": "https://a.test" }] },
            { "index": { "byteStart": at, "byteEnd": at + 5 }, "features": [{ "$type": "pub.leaflet.richtext.facet#bold" }, { "$type": "pub.leaflet.richtext.facet#code" }] },
        ]);
        assert_eq!(
            Value::Array(leaflet_runs(text, &facets)),
            json!([{ "text": "héllo ", "link": "https://a.test" }, { "text": "world", "link": "https://a.test", "code": true }])
        );
        let mail = json!([{ "index": { "byteStart": 0, "byteEnd": 2 }, "features": [{ "$type": "pub.leaflet.richtext.facet#link", "uri": "mailto:a@b.test" }] }]);
        assert_eq!(leaflet_runs("hi", &mail), vec![json!({ "text": "hi" })]);
    }

    #[test]
    fn a_leaflet_page_gaps_once_between_blocks_and_shows_its_posts() {
        let root = json!({});
        let origins = vec!["https://cdn.bsky.app".to_string()];
        let c = ctx(&root, &origins);
        let text =
            |t: &str| json!({ "block": { "$type": "pub.leaflet.blocks.text", "plaintext": t } });
        let content = json!({ "$type": "pub.leaflet.content", "pages": [{ "blocks": [
            text(""), text("a"), text(" "), text(""), text("b"), text(""),
            { "block": { "$type": "pub.leaflet.blocks.image", "image": "https://cdn.bsky.app/x.jpg" }, "alignment": "lex:pub.leaflet.pages.linearDocument#textAlignCenter" },
            { "block": { "$type": "pub.leaflet.blocks.bskyPost", "postRef": { "uri": "at://did:plc:a/app.bsky.feed.post/1" } } },
        ] }, { "$type": "pub.leaflet.pages.canvas" }] });
        let layout = json!({ "component": "Document", "props": { "value": { "$": "content" } } });
        let tree = resolve(&layout, &json!({ "content": content }), &c).unwrap();
        check::checked(&tree);
        let kinds: Vec<&str> = tree["p"]["blocks"]
            .as_array()
            .unwrap()
            .iter()
            .map(|b| b["kind"].as_str().unwrap())
            .collect();
        assert_eq!(
            kinds,
            [
                "paragraph",
                "gap",
                "paragraph",
                "gap",
                "image",
                "bskyPost",
                "unsupported"
            ]
        );
        let blocks = &tree["p"]["blocks"];
        assert_eq!(
            blocks[4]["src"],
            Value::Null,
            "a picture that is no blob never loads"
        );
        assert_eq!(blocks[4]["align"], "center");
        // A post opens as its thread, the tile its feed opens too.
        assert_eq!(
            blocks[5]["fallback"]["open"]["call"],
            json!({ "name": "query", "args": { "method": "app.bsky.feed.getPostThread",
                    "params": { "uri": "at://did:plc:a/app.bsky.feed.post/1" } } })
        );
        assert_eq!(blocks[6]["text"], "Unsupported page: canvas");
        assert!(tree["p"].get("posts").is_none());
    }

    #[test]
    fn plain_text_falls_back_in_paragraphs_a_post_url_its_post() {
        let root = json!({});
        let c = ctx(&root, &[]);
        let layout = json!({ "component": "Document", "props": { "value": { "$": "content" }, "fallback": { "$": "text" } } });
        let scope = json!({
            "content": { "$type": "com.example.other" },
            "text": "one\nline\n \n\nhttps://bsky.app/profile/did%3Aplc%3Aa/post/3k\n\n",
        });
        let tree = resolve(&layout, &scope, &c).unwrap();
        check::checked(&tree);
        assert_eq!(tree["p"]["look"], "plain");
        let blocks = &tree["p"]["blocks"];
        assert_eq!(blocks[0]["runs"][0]["text"], "one\nline");
        assert_eq!(blocks[1]["uri"], "at://did:plc:a/app.bsky.feed.post/3k");
        assert_eq!(
            blocks[1]["fallback"]["runs"][0]["link"],
            "https://bsky.app/profile/did%3Aplc%3Aa/post/3k"
        );
        let none = resolve(&layout, &json!({ "content": { "$type": "x" } }), &c).unwrap();
        check::checked(&none);
        assert_eq!(none["p"]["blocks"][0]["kind"], "json");
        assert!(resolve(&layout, &json!({}), &c).is_none());
    }

    #[test]
    fn markdown_reads_headings_quotes_code_rules_breaks_and_links() {
        let source = "# Head *em*\n\n> quoted\n>\n> ```rs extra\n> fn x() {}\n> ```\n\n    indented\n\n```\n```\n\n---\n\na  \nb [t **b**](https://a.test) ![](x.png) ![a `c` ![b](y)](x)\n\n![](alone.png)\n\n<p>\n\n</p>\n";
        let blocks = markdown(source, false);
        checked_markdown(&blocks);
        assert_eq!(
            Value::Array(blocks),
            json!([
                { "kind": "heading", "level": 1, "runs": [{ "text": "Head " }, { "text": "em", "italic": true }] },
                { "kind": "quote", "blocks": [
                    { "kind": "paragraph", "runs": [{ "text": "quoted" }] },
                    { "kind": "code", "text": "fn x() {}", "lang": "rs" },
                ] },
                { "kind": "code", "text": "indented" },
                { "kind": "code", "text": "" },
                { "kind": "rule" },
                { "kind": "paragraph", "runs": [
                    { "text": "a\nb " },
                    { "text": "t ", "link": "https://a.test" },
                    { "text": "b", "link": "https://a.test", "bold": true },
                    { "text": "  " },
                    { "text": "[a c b]", "alt": true },
                ] },
                { "kind": "paragraph", "runs": [{ "text": "<p>" }] },
                { "kind": "paragraph", "runs": [{ "text": "</p>" }] },
            ])
        );
    }

    #[test]
    fn gfm_leaves_links_glued_to_words_and_empty_www_as_text() {
        let blocks = markdown(
            "xhttp://no.test www. https://e.test\n\nhttps://f.test first",
            true,
        );
        checked_markdown(&blocks);
        assert_eq!(
            blocks[0]["runs"],
            json!([{ "text": "xhttp://no.test www. " }, { "text": "https://e.test", "link": "https://e.test" }])
        );
        assert_eq!(
            blocks[1]["runs"],
            json!([{ "text": "https://f.test", "link": "https://f.test" }, { "text": " first" }])
        );
    }

    /// A reference finds its note as pulldown-cmark does, by Unicode case:
    /// `É` and `é` are one note, numbered once.
    #[test]
    fn a_footnote_matched_only_by_unicode_case_draws_its_note() {
        let blocks = markdown("x[^É] y[^é]\n\n[^é]: A\n", true);
        checked_markdown(&blocks);
        assert_eq!(
            Value::Array(blocks),
            json!([
                { "kind": "paragraph", "runs": [{ "text": "x[1] y[1]" }] },
                { "kind": "footnotes", "items": [{ "blocks": [{ "kind": "paragraph", "runs": [{ "text": "A" }] }] }] },
            ])
        );
    }

    #[test]
    fn a_leaflet_page_draws_every_kind_of_block() {
        let root = json!({});
        let c = ctx(&root, &[]);
        let blob =
            json!({ "url": "https://cdn.test/full.jpg", "thumb": "https://cdn.test/small.jpg" });
        let small = json!({ "src": "https://cdn.test/small.jpg", "fallback": null });
        let b = |v: Value| json!({ "block": v });
        let text = |t: &str| json!({ "$type": "pub.leaflet.blocks.text", "plaintext": t });
        let facet = |s: u64, f: Value| json!({ "index": { "byteStart": s, "byteEnd": s + 1 }, "features": [f] });
        let post = "at://did:plc:a/app.bsky.feed.post/1";
        let content = json!({ "$type": "pub.leaflet.content", "pages": [{ "blocks": [
            b(json!({ "$type": "pub.leaflet.blocks.text", "plaintext": "abcdef", "facets": [
                facet(0, json!({ "$type": "pub.leaflet.richtext.facet#italic" })),
                facet(1, json!({ "$type": "pub.leaflet.richtext.facet#underline" })),
                facet(2, json!({ "$type": "pub.leaflet.richtext.facet#highlight" })),
                facet(3, json!({ "$type": "pub.leaflet.richtext.facet#strikethrough" })),
                facet(4, json!({ "$type": "pub.leaflet.richtext.facet#sparkle" })),
                facet(5, json!({ "$type": "pub.leaflet.richtext.facet#didMention", "did": "did:plc:m" })),
            ] })),
            // A block given bare, not in a wrapper.
            json!({ "$type": "pub.leaflet.blocks.horizontalRule" }),
            b(json!({ "$type": "pub.leaflet.blocks.header", "plaintext": "h1", "level": 1 })),
            b(json!({ "$type": "pub.leaflet.blocks.header", "plaintext": "h2", "level": 2 })),
            b(json!({ "$type": "pub.leaflet.blocks.header", "plaintext": "h3", "level": 3 })),
            b(json!({ "$type": "pub.leaflet.blocks.blockquote", "plaintext": "q" })),
            b(json!({ "$type": "pub.leaflet.blocks.code", "plaintext": "x = 1", "language": "py" })),
            b(json!({ "$type": "pub.leaflet.blocks.code", "plaintext": "y" })),
            b(json!({ "$type": "pub.leaflet.blocks.imageGallery", "images": [
                { "image": blob, "alt": "g", "aspectRatio": { "width": 2, "height": 1 } },
                "not a picture",
                { "image": "https://cdn.test/raw.jpg" },
            ] })),
            b(json!({ "$type": "pub.leaflet.blocks.unorderedList", "startIndex": 5, "children": [
                { "content": text("one"), "children": [{ "content": text("inner") }] },
                {},
                "not an item",
            ] })),
            b(json!({ "$type": "pub.leaflet.blocks.orderedList", "startIndex": 3, "children": [{ "content": text("third") }] })),
            b(json!({ "$type": "pub.leaflet.blocks.orderedList", "startIndex": 1, "children": [{ "content": text("first") }] })),
            b(json!({ "$type": "pub.leaflet.blocks.bskyPost" })),
            b(json!({ "$type": "pub.leaflet.blocks.bskyPost", "postRef": { "uri": "not a link" } })),
            b(json!({ "$type": "pub.leaflet.blocks.website", "src": "https://w.test:8080/p", "description": "d", "previewImage": blob })),
            b(json!({ "$type": "pub.leaflet.blocks.website", "src": "https://w.test/x", "title": "T" })),
            b(json!({ "$type": "pub.leaflet.blocks.website" })),
            b(json!({ "$type": "pub.leaflet.blocks.button", "text": "Read", "url": post })),
            b(json!({ "$type": "pub.leaflet.blocks.button", "url": "did:plc:b" })),
            b(json!({ "$type": "pub.leaflet.blocks.button", "text": "Nowhere" })),
            b(json!({ "$type": "pub.leaflet.blocks.math" })),
            b(json!({})),
            b(text("")),
        ] }] });
        let layout = json!({ "component": "Document", "props": { "value": { "$": "content" } } });
        let tree = resolve(&layout, &json!({ "content": content }), &c).unwrap();
        check::checked(&tree);
        assert_eq!(tree["p"]["look"], "leaflet");
        let para = |t: &str| json!({ "kind": "paragraph", "runs": [{ "text": t }] });
        let card = |kicker: &str, title: &str| json!({ "kind": "card", "kicker": kicker, "title": title, "image": null });
        let mut post_card = card("Bluesky post", "Open the post");
        post_card["description"] = "not a link".into();
        let mut port = card("w.test:8080", "w.test:8080");
        port["image"] = small.clone();
        port["description"] = "d".into();
        port["open"] = json!({ "url": "https://w.test:8080/p" });
        let mut titled = card("w.test", "T");
        titled["open"] = json!({ "url": "https://w.test/x" });
        assert_eq!(
            tree["p"]["blocks"],
            json!([
                { "kind": "paragraph", "runs": [
                    { "text": "a", "italic": true }, { "text": "b", "underline": true },
                    { "text": "c", "highlight": true }, { "text": "d", "strike": true },
                    { "text": "e" }, { "text": "f", "actor": "did:plc:m" },
                ] },
                { "kind": "rule" },
                { "kind": "heading", "level": 2, "runs": [{ "text": "h1" }] },
                { "kind": "heading", "level": 3, "runs": [{ "text": "h2" }] },
                { "kind": "heading", "level": 4, "runs": [{ "text": "h3" }] },
                { "kind": "quote", "blocks": [para("q")] },
                { "kind": "code", "text": "x = 1", "lang": "py" },
                { "kind": "code", "text": "y" },
                { "kind": "gallery", "images": [
                    { "src": small, "alt": "g", "ratio": 2.0 },
                    { "src": null, "alt": null, "ratio": null },
                ] },
                { "kind": "list", "ordered": false, "items": [
                    { "blocks": [para("one"), { "kind": "list", "ordered": false, "items": [{ "blocks": [para("inner")] }] }] },
                    { "blocks": [] },
                ] },
                { "kind": "list", "ordered": true, "start": 3, "items": [{ "blocks": [para("third")] }] },
                { "kind": "list", "ordered": true, "items": [{ "blocks": [para("first")] }] },
                card("Bluesky post", "Open the post"),
                { "kind": "bskyPost", "uri": "not a link", "fallback": post_card },
                port,
                titled,
                card("", ""),
                { "kind": "button", "label": "Read", "open": c.record_call(post) },
                { "kind": "button", "label": "did:plc:b", "open": c.actor_call("did:plc:b") },
                { "kind": "button", "label": "Nowhere" },
                { "kind": "unsupported", "text": "Unsupported block: pub.leaflet.blocks.math" },
                { "kind": "unsupported", "text": "Unsupported block: unknown" },
            ])
        );
    }

    #[test]
    fn markpub_draws_its_markdown_and_without_any_its_plain_text() {
        let root = json!({});
        let c = ctx(&root, &[]);
        let layout = json!({ "component": "Document", "props": { "value": { "$": "content" }, "fallback": { "$": "text" } } });
        let scope = json!({ "content": { "$type": "at.markpub.markdown", "text": { "markdown": "## T\n\n- [ ] task" } } });
        let tree = resolve(&layout, &scope, &c).unwrap();
        check::checked(&tree);
        assert_eq!(tree["p"]["look"], "markdown");
        assert_eq!(
            tree["p"]["blocks"],
            json!([
                { "kind": "heading", "level": 2, "runs": [{ "text": "T" }] },
                { "kind": "list", "ordered": false, "tight": true, "items": [
                    { "check": false, "blocks": [{ "kind": "paragraph", "runs": [{ "text": "task" }] }] },
                ] },
            ])
        );
        let scope =
            json!({ "content": { "$type": "at.markpub.markdown" }, "text": "first\n\nlast" });
        let tree = resolve(&layout, &scope, &c).unwrap();
        check::checked(&tree);
        assert_eq!(tree["p"]["look"], "plain");
        assert_eq!(
            tree["p"]["blocks"],
            json!([
                { "kind": "paragraph", "runs": [{ "text": "first" }] },
                { "kind": "paragraph", "runs": [{ "text": "last" }] },
            ])
        );
    }
}