Skip to main content

extract_head_meta

Function extract_head_meta 

Source
pub fn extract_head_meta(html: &str) -> HeadMeta
Expand description

Extracts {title, lang, canonical} from html in a single lol_html pass.

  • title is the text content of the first <title> element with inner tags stripped (matching the historical extract_title behaviour). Whitespace is collapsed and trimmed; returns the empty string when the element is missing or its text content is whitespace-only.
  • lang is the lang attribute on the root <html> element. <html lang="…"> inside a <pre> block is ignored because lol_html only matches the real element.
  • canonical is the href attribute of the first <link rel~="canonical"> in <head>. Selector matches the space-separated token set so rel="canonical other-token" is detected, while quoting style is irrelevant (the parser normalises it).

§Examples

use ssg::util::head_dom::extract_head_meta;
let html = r#"<html lang="en"><head><title>Hi</title></head></html>"#;
let meta = extract_head_meta(html);
assert_eq!(meta.title, "Hi");
assert_eq!(meta.lang, "en");