From 3c46056599105e6dee05241baa175a01b4da30c8 Mon Sep 17 00:00:00 2001 From: Tobias Schmitz Date: Sun, 13 Jul 2025 17:46:55 +0200 Subject: [PATCH] fix: public outline `entry.inner()` function --- crates/typst-layout/src/rules.rs | 2 +- crates/typst-library/src/model/outline.rs | 71 +++++++++++++---------- 2 files changed, 42 insertions(+), 31 deletions(-) diff --git a/crates/typst-layout/src/rules.rs b/crates/typst-layout/src/rules.rs index ccdae8c68..f4392d2b5 100644 --- a/crates/typst-layout/src/rules.rs +++ b/crates/typst-layout/src/rules.rs @@ -496,7 +496,7 @@ const OUTLINE_ENTRY_RULE: ShowFn = |elem, engine, styles| { let close = quotes.double_close; eco_format!("{prefix} {open}{body}{close} {page_str} {page_nr}",) }; - let inner = elem.inner(context, span, body, page)?; + let inner = elem.build_inner(context, span, body, page)?; let block = if elem.element.is::() { let body = prefix.unwrap_or_default() + inner; BlockElem::new() diff --git a/crates/typst-library/src/model/outline.rs b/crates/typst-library/src/model/outline.rs index 9421c9a4e..0f94b1f58 100644 --- a/crates/typst-library/src/model/outline.rs +++ b/crates/typst-library/src/model/outline.rs @@ -501,6 +501,47 @@ impl OutlineEntry { /// This includes the body, the fill, and page number. #[func(contextual)] pub fn inner( + &self, + engine: &mut Engine, + context: Tracked, + span: Span, + ) -> SourceResult { + let body = self.body().at(span)?; + let page = self.page(engine, context, span)?; + self.build_inner(context, span, body, page) + } + + /// The content which is displayed in place of the referred element at its + /// entry in the outline. For a heading, this is its + /// [`body`]($heading.body); for a figure a caption and for equations, it is + /// empty. + #[func] + pub fn body(&self) -> StrResult { + Ok(self.outlinable()?.body()) + } + + /// The page number of this entry's element, formatted with the numbering + /// set for the referenced page. + #[func(contextual)] + pub fn page( + &self, + engine: &mut Engine, + context: Tracked, + span: Span, + ) -> SourceResult { + let loc = self.element_location().at(span)?; + let styles = context.styles().at(span)?; + let numbering = engine + .introspector + .page_numbering(loc) + .cloned() + .unwrap_or_else(|| NumberingPattern::from_str("1").unwrap().into()); + Counter::new(CounterKey::Page).display_at_loc(engine, loc, styles, &numbering) + } +} + +impl OutlineEntry { + pub fn build_inner( &self, context: Tracked, span: Span, @@ -556,36 +597,6 @@ impl OutlineEntry { Ok(Content::sequence(seq)) } - /// The content which is displayed in place of the referred element at its - /// entry in the outline. For a heading, this is its - /// [`body`]($heading.body); for a figure a caption and for equations, it is - /// empty. - #[func] - pub fn body(&self) -> StrResult { - Ok(self.outlinable()?.body()) - } - - /// The page number of this entry's element, formatted with the numbering - /// set for the referenced page. - #[func(contextual)] - pub fn page( - &self, - engine: &mut Engine, - context: Tracked, - span: Span, - ) -> SourceResult { - let loc = self.element_location().at(span)?; - let styles = context.styles().at(span)?; - let numbering = engine - .introspector - .page_numbering(loc) - .cloned() - .unwrap_or_else(|| NumberingPattern::from_str("1").unwrap().into()); - Counter::new(CounterKey::Page).display_at_loc(engine, loc, styles, &numbering) - } -} - -impl OutlineEntry { fn outlinable(&self) -> StrResult<&dyn Outlinable> { self.element .with::()