Skip to main content

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. The app answers [`crate::Links::preview`] from its own cache and
7//! notifies when a fetch lands.
8
9use gpui::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    /// An app's own mark for a link it owns, painted in the text colour where
26    /// a favicon would go.
27    pub glyph: Option<ui::icons::Icon>,
28}
29
30/// The host, without its `www.` — all a card can say about a URL nobody has
31/// resolved.
32pub(crate) fn host(url: &str) -> &str {
33    let after = url.split_once("://").map_or(url, |(_, rest)| rest);
34    let host = after.split(['/', '?', '#']).next().unwrap_or(after);
35    host.strip_prefix("www.").unwrap_or(host)
36}