Set cite attribute of blockquote if attribution is a link.

This commit is contained in:
Michael Färber 2025-01-21 11:58:41 +01:00
parent 61c3e363a4
commit e84bb4b6e6
3 changed files with 41 additions and 5 deletions

View File

@ -4,12 +4,12 @@ use crate::foundations::{
cast, elem, Content, Depth, Label, NativeElement, Packed, Show, ShowSet, Smart, cast, elem, Content, Depth, Label, NativeElement, Packed, Show, ShowSet, Smart,
StyleChain, Styles, TargetElem, StyleChain, Styles, TargetElem,
}; };
use crate::html::{tag, HtmlElem}; use crate::html::{tag, HtmlAttr, HtmlElem};
use crate::introspection::Locatable; use crate::introspection::Locatable;
use crate::layout::{ use crate::layout::{
Alignment, BlockBody, BlockElem, Em, HElem, PadElem, Spacing, VElem, Alignment, BlockBody, BlockElem, Em, HElem, PadElem, Spacing, VElem,
}; };
use crate::model::{CitationForm, CiteElem}; use crate::model::{CitationForm, CiteElem, Destination, LinkElem, LinkTarget};
use crate::text::{SmartQuoteElem, SmartQuotes, SpaceElem, TextElem}; use crate::text::{SmartQuoteElem, SmartQuotes, SpaceElem, TextElem};
/// Displays a quote alongside an optional attribution. /// Displays a quote alongside an optional attribution.
@ -186,15 +186,28 @@ impl Show for Packed<QuoteElem> {
.styled(QuoteElem::set_depth(Depth(1))); .styled(QuoteElem::set_depth(Depth(1)));
} }
let attribution = self.attribution(styles);
if block { if block {
realized = if html { realized = if html {
HtmlElem::new(tag::blockquote).with_body(Some(realized)).pack() let mut elem = HtmlElem::new(tag::blockquote).with_body(Some(realized));
if let Some(Attribution::Content(attribution)) = attribution {
if let Some(link) = attribution.to_packed::<LinkElem>() {
if let LinkTarget::Dest(Destination::Url(url)) = &link.dest {
elem = elem.with_attr(
HtmlAttr::constant("cite"),
url.clone().into_inner(),
);
}
}
}
elem.pack()
} else { } else {
BlockElem::new().with_body(Some(BlockBody::Content(realized))).pack() BlockElem::new().with_body(Some(BlockBody::Content(realized))).pack()
} }
.spanned(self.span()); .spanned(self.span());
if let Some(attribution) = self.attribution(styles).as_ref() { if let Some(attribution) = attribution.as_ref() {
let attribution = match attribution { let attribution = match attribution {
Attribution::Content(content) => content.clone(), Attribution::Content(content) => content.clone(),
Attribution::Label(label) => CiteElem::new(*label) Attribution::Label(label) => CiteElem::new(*label)
@ -218,7 +231,7 @@ impl Show for Packed<QuoteElem> {
if !html { if !html {
realized = PadElem::new(realized).pack(); realized = PadElem::new(realized).pack();
} }
} else if let Some(Attribution::Label(label)) = self.attribution(styles) { } else if let Some(Attribution::Label(label)) = attribution {
realized += SpaceElem::shared().clone() realized += SpaceElem::shared().clone()
+ CiteElem::new(*label).pack().spanned(self.span()); + CiteElem::new(*label).pack().spanned(self.span());
} }

View File

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<blockquote cite="https://typst.app/home">
Compose papers faster
</blockquote>
<p>
<a href="https://typst.app/home">typst.com</a>
</p>
</body>
</html>

View File

@ -99,3 +99,11 @@ And I quote: #quote(attribution: [René Descartes])[cogito, ergo sum].
--- quote-nesting-html html --- --- quote-nesting-html html ---
When you said that #quote[he surely meant that #quote[she intended to say #quote[I'm sorry]]], I was quite confused. When you said that #quote[he surely meant that #quote[she intended to say #quote[I'm sorry]]], I was quite confused.
--- quote-attribution-link html ---
#quote(
block: true,
attribution: link("https://typst.app/home")[typst.com]
)[
Compose papers faster
]