Add HTML meta tags for document authors and keywords (#6134)

This commit is contained in:
Approximately Equal 2025-04-07 13:18:52 -07:00 committed by GitHub
parent 9829bd8326
commit 94a497a01f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -263,13 +263,13 @@ fn handle(
/// Wrap the nodes in `<html>` and `<body>` if they are not yet rooted, /// Wrap the nodes in `<html>` and `<body>` if they are not yet rooted,
/// supplying a suitable `<head>`. /// supplying a suitable `<head>`.
fn root_element(output: Vec<HtmlNode>, info: &DocumentInfo) -> SourceResult<HtmlElement> { fn root_element(output: Vec<HtmlNode>, info: &DocumentInfo) -> SourceResult<HtmlElement> {
let head = head_element(info);
let body = match classify_output(output)? { let body = match classify_output(output)? {
OutputKind::Html(element) => return Ok(element), OutputKind::Html(element) => return Ok(element),
OutputKind::Body(body) => body, OutputKind::Body(body) => body,
OutputKind::Leafs(leafs) => HtmlElement::new(tag::body).with_children(leafs), OutputKind::Leafs(leafs) => HtmlElement::new(tag::body).with_children(leafs),
}; };
Ok(HtmlElement::new(tag::html) Ok(HtmlElement::new(tag::html).with_children(vec![head.into(), body.into()]))
.with_children(vec![head_element(info).into(), body.into()]))
} }
/// Generate a `<head>` element. /// Generate a `<head>` element.
@ -302,6 +302,24 @@ fn head_element(info: &DocumentInfo) -> HtmlElement {
); );
} }
if !info.author.is_empty() {
children.push(
HtmlElement::new(tag::meta)
.with_attr(attr::name, "authors")
.with_attr(attr::content, info.author.join(", "))
.into(),
)
}
if !info.keywords.is_empty() {
children.push(
HtmlElement::new(tag::meta)
.with_attr(attr::name, "keywords")
.with_attr(attr::content, info.keywords.join(", "))
.into(),
)
}
HtmlElement::new(tag::head).with_children(children) HtmlElement::new(tag::head).with_children(children)
} }