typst_library/model/
link.rs1use 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#[elem(Show)]
42pub struct LinkElem {
43 #[required]
72 #[parse(
73 let dest = args.expect::<LinkTarget>("destination")?;
74 dest.clone()
75 )]
76 pub dest: LinkTarget,
77
78 #[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 #[internal]
94 #[ghost]
95 pub current: Option<Destination>,
96}
97
98impl LinkElem {
99 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#[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#[derive(Debug, Clone, Eq, PartialEq, Hash)]
180pub enum Destination {
181 Url(Url),
183 Position(Position),
185 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#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
211pub struct Url(EcoString);
212
213impl Url {
214 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 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}