Skip to main content

extract_meta_tags

Function extract_meta_tags 

Source
pub fn extract_meta_tags(
    html_content: &str,
) -> Result<Vec<MetaTag>, MetadataError>
Available on crate feature html only.
Expand description

Extracts <meta> tags from HTML content in one streaming pass (feature html).

Both <meta …> and <meta … /> are handled; attribute names are matched case-insensitively; name, property and http-equiv identify a tag, in that order of preference; entity references in values are decoded. Malformed markup ends the scan and what was found so far is returned, because a whole HTML page rarely satisfies a strict reader end to end. Use extract_meta_tags_lenient to learn where the scan stopped.

§Errors

None today; the signature keeps Result so a strict mode can report without a breaking change.

§Implementation note

Scans <…> tags in one forward pass and parses each <meta> tag with oxml (attribute parsing and XML/HTML character and entity reference decoding without full-document DOM allocation). This replaces the previous scraper / html5ever and quick-xml dependencies. See issue #22.

§Example

use metadata_gen::metatags::extract_meta_tags;

let html = r#"<head><meta name="description" content="A &amp; B"><meta property="og:title" content="T"></head>"#;
let tags = extract_meta_tags(html).unwrap();
assert_eq!(tags[0].content, "A & B");
assert_eq!(tags[1].name, "og:title");