mirror of
https://github.com/typst/typst
synced 2025-05-14 17:15:28 +08:00
Prepend section numbers to headings in HTML output. (#5522)
This commit is contained in:
parent
bf1c7db6fc
commit
60f1d8f9b5
@ -217,21 +217,7 @@ impl Synthesize for Packed<HeadingElem> {
|
|||||||
impl Show for Packed<HeadingElem> {
|
impl Show for Packed<HeadingElem> {
|
||||||
#[typst_macros::time(name = "heading", span = self.span())]
|
#[typst_macros::time(name = "heading", span = self.span())]
|
||||||
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
|
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
|
||||||
if TargetElem::target_in(styles).is_html() {
|
let html = TargetElem::target_in(styles).is_html();
|
||||||
// HTML's h1 is closer to a title element. There should only be one.
|
|
||||||
// Meanwhile, a level 1 Typst heading is a section heading. For this
|
|
||||||
// reason, levels are offset by one: A Typst level 1 heading becomes
|
|
||||||
// a `<h2>`.
|
|
||||||
let level = self.resolve_level(styles);
|
|
||||||
let t = [tag::h2, tag::h3, tag::h4, tag::h5, tag::h6][level.get().min(5) - 1];
|
|
||||||
|
|
||||||
// TODO: Don't ignore the various non-body properties.
|
|
||||||
let body = self.body().clone();
|
|
||||||
return Ok(HtmlElem::new(t)
|
|
||||||
.with_body(Some(body))
|
|
||||||
.pack()
|
|
||||||
.spanned(self.span()));
|
|
||||||
}
|
|
||||||
|
|
||||||
const SPACING_TO_NUMBERING: Em = Em::new(0.3);
|
const SPACING_TO_NUMBERING: Em = Em::new(0.3);
|
||||||
|
|
||||||
@ -250,7 +236,7 @@ impl Show for Packed<HeadingElem> {
|
|||||||
.display_at_loc(engine, location, styles, numbering)?
|
.display_at_loc(engine, location, styles, numbering)?
|
||||||
.spanned(span);
|
.spanned(span);
|
||||||
|
|
||||||
if hanging_indent.is_auto() {
|
if hanging_indent.is_auto() && !html {
|
||||||
let pod = Region::new(Axes::splat(Abs::inf()), Axes::splat(false));
|
let pod = Region::new(Axes::splat(Abs::inf()), Axes::splat(false));
|
||||||
|
|
||||||
// We don't have a locator for the numbering here, so we just
|
// We don't have a locator for the numbering here, so we just
|
||||||
@ -268,19 +254,31 @@ impl Show for Packed<HeadingElem> {
|
|||||||
indent = size.x + SPACING_TO_NUMBERING.resolve(styles);
|
indent = size.x + SPACING_TO_NUMBERING.resolve(styles);
|
||||||
}
|
}
|
||||||
|
|
||||||
realized = numbering
|
let spacing = if html {
|
||||||
+ HElem::new(SPACING_TO_NUMBERING.into()).with_weak(true).pack()
|
SpaceElem::shared().clone()
|
||||||
+ realized;
|
} else {
|
||||||
|
HElem::new(SPACING_TO_NUMBERING.into()).with_weak(true).pack()
|
||||||
|
};
|
||||||
|
|
||||||
|
realized = numbering + spacing + realized;
|
||||||
}
|
}
|
||||||
|
|
||||||
if indent != Abs::zero() {
|
if indent != Abs::zero() && !html {
|
||||||
realized = realized.styled(ParElem::set_hanging_indent(indent.into()));
|
realized = realized.styled(ParElem::set_hanging_indent(indent.into()));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(BlockElem::new()
|
Ok(if html {
|
||||||
.with_body(Some(BlockBody::Content(realized)))
|
// HTML's h1 is closer to a title element. There should only be one.
|
||||||
.pack()
|
// Meanwhile, a level 1 Typst heading is a section heading. For this
|
||||||
.spanned(span))
|
// reason, levels are offset by one: A Typst level 1 heading becomes
|
||||||
|
// a `<h2>`.
|
||||||
|
let level = self.resolve_level(styles);
|
||||||
|
let t = [tag::h2, tag::h3, tag::h4, tag::h5, tag::h6][level.get().min(5) - 1];
|
||||||
|
HtmlElem::new(t).with_body(Some(realized)).pack().spanned(span)
|
||||||
|
} else {
|
||||||
|
let realized = BlockBody::Content(realized);
|
||||||
|
BlockElem::new().with_body(Some(realized)).pack().spanned(span)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user