mirror of
https://github.com/typst/typst
synced 2025-08-18 00:48:34 +08:00
Extract LinkTarget::resolve
This commit is contained in:
parent
12c66e36b5
commit
733a2cda15
@ -20,8 +20,8 @@ use typst_library::math::EquationElem;
|
|||||||
use typst_library::model::{
|
use typst_library::model::{
|
||||||
Attribution, BibliographyElem, CiteElem, CiteGroup, CslSource, Destination, EmphElem,
|
Attribution, BibliographyElem, CiteElem, CiteGroup, CslSource, Destination, EmphElem,
|
||||||
EnumElem, FigureCaption, FigureElem, FootnoteElem, FootnoteEntry, HeadingElem,
|
EnumElem, FigureCaption, FigureElem, FootnoteElem, FootnoteEntry, HeadingElem,
|
||||||
LinkElem, LinkTarget, ListElem, Outlinable, OutlineElem, OutlineEntry, ParElem,
|
LinkElem, ListElem, Outlinable, OutlineElem, OutlineEntry, ParElem, ParbreakElem,
|
||||||
ParbreakElem, QuoteElem, RefElem, StrongElem, TableCell, TableElem, TermsElem, Works,
|
QuoteElem, RefElem, StrongElem, TableCell, TableElem, TermsElem, Works,
|
||||||
};
|
};
|
||||||
use typst_library::pdf::EmbedElem;
|
use typst_library::pdf::EmbedElem;
|
||||||
use typst_library::text::{
|
use typst_library::text::{
|
||||||
@ -216,14 +216,8 @@ const TERMS_RULE: ShowFn<TermsElem> = |elem, _, styles| {
|
|||||||
|
|
||||||
const LINK_RULE: ShowFn<LinkElem> = |elem, engine, _| {
|
const LINK_RULE: ShowFn<LinkElem> = |elem, engine, _| {
|
||||||
let body = elem.body.clone();
|
let body = elem.body.clone();
|
||||||
Ok(match &elem.dest {
|
let dest = elem.dest.resolve(engine.introspector).at(elem.span())?;
|
||||||
LinkTarget::Dest(dest) => body.linked(dest.clone()),
|
Ok(body.linked(dest))
|
||||||
LinkTarget::Label(label) => {
|
|
||||||
let elem = engine.introspector.query_label(*label).at(elem.span())?;
|
|
||||||
let dest = Destination::Location(elem.location().unwrap());
|
|
||||||
body.linked(dest)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const HEADING_RULE: ShowFn<HeadingElem> = |elem, engine, styles| {
|
const HEADING_RULE: ShowFn<HeadingElem> = |elem, engine, styles| {
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
|
||||||
|
use comemo::Tracked;
|
||||||
use ecow::{eco_format, EcoString};
|
use ecow::{eco_format, EcoString};
|
||||||
|
|
||||||
use crate::diag::{bail, StrResult};
|
use crate::diag::{bail, StrResult};
|
||||||
use crate::foundations::{
|
use crate::foundations::{
|
||||||
cast, elem, Content, Label, Packed, Repr, ShowSet, Smart, StyleChain, Styles,
|
cast, elem, Content, Label, Packed, Repr, ShowSet, Smart, StyleChain, Styles,
|
||||||
};
|
};
|
||||||
use crate::introspection::Location;
|
use crate::introspection::{Introspector, Locatable, Location};
|
||||||
use crate::layout::Position;
|
use crate::layout::Position;
|
||||||
use crate::text::TextElem;
|
use crate::text::TextElem;
|
||||||
|
|
||||||
@ -124,6 +125,19 @@ pub enum LinkTarget {
|
|||||||
Label(Label),
|
Label(Label),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl LinkTarget {
|
||||||
|
/// Resolves the destination.
|
||||||
|
pub fn resolve(&self, introspector: Tracked<Introspector>) -> StrResult<Destination> {
|
||||||
|
Ok(match self {
|
||||||
|
LinkTarget::Dest(dest) => dest.clone(),
|
||||||
|
LinkTarget::Label(label) => {
|
||||||
|
let elem = introspector.query_label(*label)?;
|
||||||
|
Destination::Location(elem.location().unwrap())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
cast! {
|
cast! {
|
||||||
LinkTarget,
|
LinkTarget,
|
||||||
self => match self {
|
self => match self {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user