Function goose_eggs::get_title

source ·
pub fn get_title(html: &str) -> Option<String>
Expand description

Use a regular expression to get the web page title.

Example

use goose_eggs::{get_html_header, get_title};

// For this example we grab just a subset of a web page, enough to demonstrate. Normally
// you'd use the entire html snippet returned from [`validate_page`] or
// [`validate_and_load_static_assets`].
let html = r#"
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8" />
    <link rel="canonical" href="https://example.com/" />
    <link rel="shortlink" href="https://example.com/" />
    <meta name="Generator" content="Drupal 9 (https://www.drupal.org)" />
    <meta name="MobileOptimized" content="width" />
    <meta name="HandheldFriendly" content="true" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Example Website</title>
  </head>
<body>
  This is the web page body.
</body>
</html>
"#;

// Start by extracting the HTML header from the HTML.
let html_header = get_html_header(html).map_or_else(|| "".to_string(), |h| h.to_string());
// Next extract the title from the HTML header.
let title = get_title(&html_header).map_or_else(|| "".to_string(), |t| t.to_string());
assert_eq!(title, "Example Website");