diff --git a/crates/typst-library/src/math/equation.rs b/crates/typst-library/src/math/equation.rs index 32be216a4..145f45095 100644 --- a/crates/typst-library/src/math/equation.rs +++ b/crates/typst-library/src/math/equation.rs @@ -235,7 +235,10 @@ impl Outlinable for Packed { self.block(StyleChain::default()) && self.numbering().is_some() } - fn prefix(&self, numbers: Content) -> Content { + fn prefix(&self, numbers: Content, add_supplement: Smart) -> Content { + if add_supplement == Smart::Custom(false) { + return numbers; + } let supplement = self.supplement(); if !supplement.is_empty() { supplement + TextElem::packed('\u{a0}') + numbers diff --git a/crates/typst-library/src/model/figure.rs b/crates/typst-library/src/model/figure.rs index 5a137edbd..d5cca9d4e 100644 --- a/crates/typst-library/src/model/figure.rs +++ b/crates/typst-library/src/model/figure.rs @@ -436,7 +436,10 @@ impl Outlinable for Packed { || self.numbering().is_some()) } - fn prefix(&self, numbers: Content) -> Content { + fn prefix(&self, numbers: Content, add_supplement: Smart) -> Content { + if add_supplement == Smart::Custom(false) { + return numbers; + } let supplement = self.supplement(); if !supplement.is_empty() { supplement + TextElem::packed('\u{a0}') + numbers diff --git a/crates/typst-library/src/model/heading.rs b/crates/typst-library/src/model/heading.rs index 00931c815..f436d962d 100644 --- a/crates/typst-library/src/model/heading.rs +++ b/crates/typst-library/src/model/heading.rs @@ -364,8 +364,17 @@ impl Outlinable for Packed { (**self).resolve_level(StyleChain::default()) } - fn prefix(&self, numbers: Content) -> Content { - numbers + fn prefix(&self, numbers: Content, add_supplement: Smart) -> Content { + if add_supplement == Smart::Custom(true) { + let supplement = self.supplement(); + if !supplement.is_empty() { + supplement + TextElem::packed('\u{a0}') + numbers + } else { + numbers + } + } else { + numbers + } } fn body(&self) -> Content { diff --git a/crates/typst-library/src/model/outline.rs b/crates/typst-library/src/model/outline.rs index 489c375e6..93c895663 100644 --- a/crates/typst-library/src/model/outline.rs +++ b/crates/typst-library/src/model/outline.rs @@ -357,7 +357,7 @@ pub trait Outlinable: Refable { } /// Constructs the default prefix given the formatted numbering. - fn prefix(&self, numbers: Content) -> Content; + fn prefix(&self, numbers: Content, add_supplement: Smart) -> Content; /// The body of the entry. fn body(&self) -> Content; @@ -421,7 +421,7 @@ impl Show for Packed { let context = Context::new(None, Some(styles)); let context = context.track(); - let prefix = self.prefix(engine, context, span)?; + let prefix = self.prefix(engine, context, span, Smart::Auto)?; let inner = self.inner(engine, context, span)?; let block = if self.element.is::() { let body = prefix.unwrap_or_default() + inner; @@ -542,16 +542,21 @@ impl OutlineEntry { } /// Formats the element's numbering (if any). - /// - /// This also appends the element's supplement in case of figures or - /// equations. For instance, it would output `1.1` for a heading, but - /// `Figure 1` for a figure, as is usual for outlines. #[func(contextual)] pub fn prefix( &self, engine: &mut Engine, context: Tracked, span: Span, + /// Whether to add the element's supplement if it has one. + /// + /// If set to `{auto}`, the supplement is added for figures and + /// equations that have one. For instance, it would output `[1.1]` + /// for a heading, but `[Figure 1]` for a figure, as is usual for + /// outlines. + #[named] + #[default] + add_supplement: Smart, ) -> SourceResult> { let outlinable = self.outlinable().at(span)?; let Some(numbering) = outlinable.numbering() else { return Ok(None) }; @@ -559,7 +564,7 @@ impl OutlineEntry { let styles = context.styles().at(span)?; let numbers = outlinable.counter().display_at_loc(engine, loc, styles, numbering)?; - Ok(Some(outlinable.prefix(numbers))) + Ok(Some(outlinable.prefix(numbers, add_supplement))) } /// Creates the default inner content of the entry. diff --git a/tests/ref/outline-entry-force-supplement.png b/tests/ref/outline-entry-force-supplement.png new file mode 100644 index 000000000..5e44e3000 Binary files /dev/null and b/tests/ref/outline-entry-force-supplement.png differ diff --git a/tests/ref/outline-entry-no-supplement.png b/tests/ref/outline-entry-no-supplement.png new file mode 100644 index 000000000..6b15a784e Binary files /dev/null and b/tests/ref/outline-entry-no-supplement.png differ diff --git a/tests/suite/model/outline.typ b/tests/suite/model/outline.typ index 49fd7d7cb..1587400ce 100644 --- a/tests/suite/model/outline.typ +++ b/tests/suite/model/outline.typ @@ -171,6 +171,46 @@ == Middle heading === Lower heading +--- outline-entry-force-supplement --- +#set page(width: 200pt) +#set heading(numbering: "1.", supplement: [Chapter]) + +#show outline.entry.where(level: 1): it => link( + it.element.location(), + it.indented( + it.prefix(add-supplement: true), + it.inner(), + ) +) + +#outline() + += Top +== Middle +=== Lower +== Middle Again += Top again +#set heading(numbering: none) += Top again again + +--- outline-entry-no-supplement --- +#set page(width: 200pt) +#set math.equation(numbering: "(i)", supplement: [Eq]) + +#show outline.entry.where(level: 1): it => link( + it.element.location(), + it.indented( + it.prefix(add-supplement: false), + it.inner(), + ) +) + +#outline(target: math.equation) + +$ 1 $ +#set math.equation(numbering: none) +$ 2 $ + --- outline-entry-inner --- #set heading(numbering: "1.") #show outline.entry: it => block(it.inner())