markdown/preview.rs
1//! Who describes a link.
2//!
3//! `markdown` fetches nothing. Resolving a URL's Open Graph data is an HTTP
4//! request and an HTML parse, neither of which a component library has any
5//! business carrying, and `wasm32-unknown-unknown` would spell both differently
6//! anyway. Installed once at boot like the highlighter and read at paint: the
7//! app answers from its own cache and notifies when a fetch lands.
8
9use gpui::{App, Global, SharedString};
10
11/// What a bookmark paints beyond the URL it already has.
12///
13/// Every field is optional because a preview arrives in pieces, and a card
14/// holding none of them still shows its host.
15#[derive(Clone, Debug, Default, PartialEq, Eq)]
16pub struct Preview {
17 pub title: Option<SharedString>,
18 pub description: Option<SharedString>,
19 pub image: Option<SharedString>,
20 pub icon: Option<SharedString>,
21 /// The footer's identity where the host is not the most specific one — a
22 /// repository, a subreddit. Which path names a *unit* is the app's
23 /// knowledge, not this crate's.
24 pub label: Option<SharedString>,
25}
26
27/// `None` for a URL the caller has nothing for *yet*: the card paints its host
28/// and repaints when the answer arrives.
29pub type LinkPreview = fn(url: &str) -> Option<Preview>;
30
31struct Installed(LinkPreview);
32
33impl Global for Installed {}
34
35/// `markdown::set_link_preview(cx, my_previews)` — call once at boot. Without
36/// it a bookmark shows its host and its URL, which is what a link looks like
37/// before anyone has resolved it.
38pub fn set_link_preview(cx: &mut App, preview: LinkPreview) {
39 cx.set_global(Installed(preview));
40}
41
42pub(crate) fn of(cx: &App, url: &str) -> Option<Preview> {
43 (cx.try_global::<Installed>()?.0)(url)
44}
45
46/// The host, without its `www.` — all a card can say about a URL nobody has
47/// resolved.
48pub(crate) fn host(url: &str) -> &str {
49 let after = url.split_once("://").map_or(url, |(_, rest)| rest);
50 let host = after.split(['/', '?', '#']).next().unwrap_or(after);
51 host.strip_prefix("www.").unwrap_or(host)
52}