typst_library/model/
link.rs

1use std::ops::Deref;
2
3use ecow::{eco_format, EcoString};
4
5use crate::diag::{bail, warning, At, SourceResult, StrResult};
6use crate::engine::Engine;
7use crate::foundations::{
8    cast, elem, Content, Label, NativeElement, Packed, Repr, Show, ShowSet, Smart,
9    StyleChain, Styles, TargetElem,
10};
11use crate::html::{attr, tag, HtmlElem};
12use crate::introspection::Location;
13use crate::layout::Position;
14use crate::text::TextElem;
15
16/// Links to a URL or a location in the document.
17///
18/// By default, links do not look any different from normal text. However,
19/// you can easily apply a style of your choice with a show rule.
20///
21/// # Example
22/// ```example
23/// #show link: underline
24///
25/// https://example.com \
26///
27/// #link("https://example.com") \
28/// #link("https://example.com")[
29///   See example.com
30/// ]
31/// ```
32///
33/// # Hyphenation
34/// If you enable hyphenation or justification, by default, it will not apply to
35/// links to prevent unwanted hyphenation in URLs. You can opt out of this
36/// default via `{show link: set text(hyphenate: true)}`.
37///
38/// # Syntax
39/// This function also has dedicated syntax: Text that starts with `http://` or
40/// `https://` is automatically turned into a link.
41#[elem(Show)]
42pub struct LinkElem {
43    /// The destination the link points to.
44    ///
45    /// - To link to web pages, `dest` should be a valid URL string. If the URL
46    ///   is in the `mailto:` or `tel:` scheme and the `body` parameter is
47    ///   omitted, the email address or phone number will be the link's body,
48    ///   without the scheme.
49    ///
50    /// - To link to another part of the document, `dest` can take one of three
51    ///   forms:
52    ///   - A [label] attached to an element. If you also want automatic text
53    ///     for the link based on the element, consider using a
54    ///     [reference]($ref) instead.
55    ///
56    ///   - A [`location`] (typically retrieved from [`here`], [`locate`] or
57    ///     [`query`]).
58    ///
59    ///   - A dictionary with a `page` key of type [integer]($int) and `x` and
60    ///     `y` coordinates of type [length]. Pages are counted from one, and
61    ///     the coordinates are relative to the page's top left corner.
62    ///
63    /// ```example
64    /// = Introduction <intro>
65    /// #link("mailto:hello@typst.app") \
66    /// #link(<intro>)[Go to intro] \
67    /// #link((page: 1, x: 0pt, y: 0pt))[
68    ///   Go to top
69    /// ]
70    /// ```
71    #[required]
72    #[parse(
73        let dest = args.expect::<LinkTarget>("destination")?;
74        dest.clone()
75    )]
76    pub dest: LinkTarget,
77
78    /// The content that should become a link.
79    ///
80    /// If `dest` is an URL string, the parameter can be omitted. In this case,
81    /// the URL will be shown as the link.
82    #[required]
83    #[parse(match &dest {
84        LinkTarget::Dest(Destination::Url(url)) => match args.eat()? {
85            Some(body) => body,
86            None => body_from_url(url),
87        },
88        _ => args.expect("body")?,
89    })]
90    pub body: Content,
91
92    /// A destination style that should be applied to elements.
93    #[internal]
94    #[ghost]
95    pub current: Option<Destination>,
96}
97
98impl LinkElem {
99    /// Create a link element from a URL with its bare text.
100    pub fn from_url(url: Url) -> Self {
101        let body = body_from_url(&url);
102        Self::new(LinkTarget::Dest(Destination::Url(url)), body)
103    }
104}
105
106impl Show for Packed<LinkElem> {
107    #[typst_macros::time(name = "link", span = self.span())]
108    fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
109        let body = self.body.clone();
110
111        Ok(if TargetElem::target_in(styles).is_html() {
112            if let LinkTarget::Dest(Destination::Url(url)) = &self.dest {
113                HtmlElem::new(tag::a)
114                    .with_attr(attr::href, url.clone().into_inner())
115                    .with_body(Some(body))
116                    .pack()
117                    .spanned(self.span())
118            } else {
119                engine.sink.warn(warning!(
120                    self.span(),
121                    "non-URL links are not yet supported by HTML export"
122                ));
123                body
124            }
125        } else {
126            match &self.dest {
127                LinkTarget::Dest(dest) => body.linked(dest.clone()),
128                LinkTarget::Label(label) => {
129                    let elem = engine.introspector.query_label(*label).at(self.span())?;
130                    let dest = Destination::Location(elem.location().unwrap());
131                    body.clone().linked(dest)
132                }
133            }
134        })
135    }
136}
137
138impl ShowSet for Packed<LinkElem> {
139    fn show_set(&self, _: StyleChain) -> Styles {
140        let mut out = Styles::new();
141        out.set(TextElem::set_hyphenate(Smart::Custom(false)));
142        out
143    }
144}
145
146fn body_from_url(url: &Url) -> Content {
147    let text = ["mailto:", "tel:"]
148        .into_iter()
149        .find_map(|prefix| url.strip_prefix(prefix))
150        .unwrap_or(url);
151    let shorter = text.len() < url.len();
152    TextElem::packed(if shorter { text.into() } else { (**url).clone() })
153}
154
155/// A target where a link can go.
156#[derive(Debug, Clone, PartialEq, Hash)]
157pub enum LinkTarget {
158    Dest(Destination),
159    Label(Label),
160}
161
162cast! {
163    LinkTarget,
164    self => match self {
165        Self::Dest(v) => v.into_value(),
166        Self::Label(v) => v.into_value(),
167    },
168    v: Destination => Self::Dest(v),
169    v: Label => Self::Label(v),
170}
171
172impl From<Destination> for LinkTarget {
173    fn from(dest: Destination) -> Self {
174        Self::Dest(dest)
175    }
176}
177
178/// A link destination.
179#[derive(Debug, Clone, Eq, PartialEq, Hash)]
180pub enum Destination {
181    /// A link to a URL.
182    Url(Url),
183    /// A link to a point on a page.
184    Position(Position),
185    /// An unresolved link to a location in the document.
186    Location(Location),
187}
188
189impl Destination {}
190
191impl Repr for Destination {
192    fn repr(&self) -> EcoString {
193        eco_format!("{self:?}")
194    }
195}
196
197cast! {
198    Destination,
199    self => match self {
200        Self::Url(v) => v.into_value(),
201        Self::Position(v) => v.into_value(),
202        Self::Location(v) => v.into_value(),
203    },
204    v: Url => Self::Url(v),
205    v: Position => Self::Position(v),
206    v: Location => Self::Location(v),
207}
208
209/// A uniform resource locator with a maximum length.
210#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
211pub struct Url(EcoString);
212
213impl Url {
214    /// Create a URL from a string, checking the maximum length.
215    pub fn new(url: impl Into<EcoString>) -> StrResult<Self> {
216        let url = url.into();
217        if url.len() > 8000 {
218            bail!("URL is too long")
219        }
220        Ok(Self(url))
221    }
222
223    /// Extract the underlying [`EcoString`].
224    pub fn into_inner(self) -> EcoString {
225        self.0
226    }
227}
228
229impl Deref for Url {
230    type Target = EcoString;
231
232    fn deref(&self) -> &Self::Target {
233        &self.0
234    }
235}
236
237cast! {
238    Url,
239    self => self.0.into_value(),
240    v: EcoString => Self::new(v)?,
241}