mq-markdown 0.10.2

Markdown parsing and manipulation utilities for mq
Documentation
#![cfg(feature = "html-to-markdown")]
pub mod converter;
mod iframe;
pub mod node;
mod noise;
pub mod options;
pub mod parser;
mod scoring;

use miette::miette;
pub use options::ConversionOptions;
use scraper::ElementRef;
use scraper::Html;
use scraper::Selector;
use std::collections::BTreeMap;
use yaml_rust2::Yaml;
use yaml_rust2::yaml::Hash;

fn find_element<'a>(html: &'a Html, selector_str: &str) -> Option<ElementRef<'a>> {
    Selector::parse(selector_str)
        .ok()
        .and_then(|sel| html.select(&sel).next())
}

/// `options.base_url` takes priority over the first `<base href>`; a relative href is joined onto it.
fn resolve_base_url(html: &Html, options_base: Option<&str>) -> Option<url::Url> {
    let explicit = options_base.and_then(|b| url::Url::parse(b).ok());
    let base_href = find_element(html, "base[href]").and_then(|el| el.value().attr("href"));
    match (explicit, base_href) {
        (Some(e), Some(h)) if url::Url::parse(h).is_err() => e.join(h).ok().or(Some(e)),
        (Some(e), _) => Some(e),
        (None, Some(h)) => url::Url::parse(h).ok(),
        (None, None) => None,
    }
}

/// Rewrites relative `href`/`src` on link/image/embed elements into absolute URLs against `base`.
fn resolve_relative_urls(nodes: &mut [node::HtmlNode], base: &url::Url) {
    for n in nodes.iter_mut() {
        if let node::HtmlNode::Element(el) = n {
            let attr_name = match el.tag_name.as_str() {
                "a" => Some("href"),
                "img" | "source" | "iframe" | "embed" | "video" | "audio" | "object" => Some("src"),
                _ => None,
            };
            if let Some(attr_name) = attr_name
                && let Some(Some(value)) = el.attributes.get(attr_name)
                && !value.is_empty()
                && let Ok(resolved) = base.join(value)
            {
                el.attributes.insert(attr_name.to_string(), Some(resolved.to_string()));
            }
            resolve_relative_urls(&mut el.children, base);
        }
    }
}

/// Recursively drops unrendered and boilerplate (ads/share/comments/cookie banners/...) nodes.
fn strip_noise_nodes(nodes: &mut Vec<node::HtmlNode>) {
    nodes.retain(|n| match n {
        node::HtmlNode::Element(el) => {
            !(noise::is_hidden_element(el) || noise::is_noise_by_class_id(el) || noise::is_ui_control(el))
        }
        _ => true,
    });
    for n in nodes.iter_mut() {
        if let node::HtmlNode::Element(el) = n {
            strip_noise_nodes(&mut el.children);
        }
    }
}

fn extract_title_text(html: &Html) -> Option<String> {
    let head = find_element(html, "head")?;
    let title_sel = Selector::parse("title").ok()?;
    let title_text = head.select(&title_sel).next()?.text().collect::<String>();
    let trimmed = title_text.trim().to_string();
    if trimmed.is_empty() { None } else { Some(trimmed) }
}

/// Collects `<meta name=... content=...>` and `<meta property=... content=...>` pairs
/// within `head`, keyed by the lowercased `name`/`property` value (OGP and Twitter Card
/// tags use `property`/`name` inconsistently across sites, so both are checked).
fn collect_meta_content(head: &scraper::ElementRef) -> BTreeMap<String, String> {
    let mut map = BTreeMap::new();
    let Ok(selector) = Selector::parse("meta") else {
        return map;
    };
    for meta_node in head.select(&selector) {
        let Some(content) = meta_node
            .value()
            .attr("content")
            .map(str::trim)
            .filter(|s| !s.is_empty())
        else {
            continue;
        };
        for attr in ["name", "property"] {
            if let Some(key) = meta_node.value().attr(attr) {
                map.entry(key.to_lowercase()).or_insert_with(|| content.to_string());
            }
        }
    }
    map
}

/// Recursively unwraps a JSON-LD document (arrays and `@graph` wrappers) into individual
/// schema.org node objects.
fn flatten_json_ld(value: serde_json::Value, out: &mut Vec<serde_json::Value>) {
    match value {
        serde_json::Value::Array(items) => items.into_iter().for_each(|item| flatten_json_ld(item, out)),
        serde_json::Value::Object(ref map) => {
            if let Some(graph) = map.get("@graph").cloned() {
                flatten_json_ld(graph, out);
            }
            out.push(value);
        }
        _ => {}
    }
}

const SCHEMA_ARTICLE_TYPES: &[&str] = &["Article", "NewsArticle", "BlogPosting", "TechArticle", "Report"];

/// Finds the first `<script type="application/ld+json">` node whose `@type` is an
/// article-like schema.org type (anywhere in the document, not just `<head>`, since
/// sites commonly emit JSON-LD just before `</body>`).
fn find_article_schema(html: &Html) -> Option<serde_json::Value> {
    let selector = Selector::parse(r#"script[type="application/ld+json"]"#).ok()?;
    let mut docs = Vec::new();
    for script in html.select(&selector) {
        let text: String = script.text().collect();
        if let Ok(value) = serde_json::from_str(text.trim()) {
            flatten_json_ld(value, &mut docs);
        }
    }
    docs.into_iter().find(|doc| match doc.get("@type") {
        Some(serde_json::Value::String(t)) => SCHEMA_ARTICLE_TYPES.iter().any(|a| t.contains(a)),
        Some(serde_json::Value::Array(items)) => items
            .iter()
            .filter_map(|v| v.as_str())
            .any(|t| SCHEMA_ARTICLE_TYPES.iter().any(|a| t.contains(a))),
        _ => false,
    })
}

/// Extracts a display string from a schema.org value: a plain string, an object's
/// `name` (falling back to `url`), or the comma-joined result over an array.
fn schema_text(value: &serde_json::Value) -> Option<String> {
    match value {
        serde_json::Value::String(s) if !s.trim().is_empty() => Some(s.trim().to_string()),
        serde_json::Value::Object(map) => map
            .get("name")
            .and_then(schema_text)
            .or_else(|| map.get("url").and_then(schema_text)),
        serde_json::Value::Array(items) => {
            let names: Vec<String> = items.iter().filter_map(schema_text).collect();
            (!names.is_empty()).then(|| names.join(", "))
        }
        _ => None,
    }
}

/// Where a front matter field can be read from.
enum Source {
    Meta(&'static str),
    Schema(&'static str),
}

/// Front matter fields with their sources in priority order.
/// `twitter:creator` is an @handle, not a display name, so it is the last resort for `author`.
const FIELDS: [(&str, &[Source]); 5] = [
    (
        "description",
        &[
            Source::Meta("description"),
            Source::Meta("og:description"),
            Source::Meta("twitter:description"),
            Source::Schema("description"),
        ],
    ),
    (
        "author",
        &[
            Source::Meta("author"),
            Source::Meta("article:author"),
            Source::Schema("author"),
            Source::Meta("twitter:creator"),
        ],
    ),
    (
        "published",
        &[
            Source::Meta("article:published_time"),
            Source::Meta("date"),
            Source::Meta("pubdate"),
            Source::Schema("datePublished"),
        ],
    ),
    (
        "image",
        &[
            Source::Meta("og:image"),
            Source::Meta("twitter:image"),
            Source::Schema("image"),
        ],
    ),
    ("site", &[Source::Meta("og:site_name"), Source::Schema("publisher")]),
];

fn extract_front_matter_from_head_ref(html: &Html) -> Option<BTreeMap<String, Yaml>> {
    let head_element = find_element(html, "head")?;
    let mut fm_map = BTreeMap::new();

    if let Ok(title_selector) = Selector::parse("title")
        && let Some(title_node) = head_element.select(&title_selector).next()
    {
        let title_str = title_node.text().collect::<String>().trim().to_string();
        if !title_str.is_empty() {
            fm_map.insert("title".to_string(), Yaml::String(title_str));
        }
    }

    let meta = collect_meta_content(&head_element);
    let schema = find_article_schema(html);
    let schema_field = |key: &str| schema.as_ref().and_then(|s| s.get(key)).and_then(schema_text);

    if let Some(keywords_content) = meta.get("keywords") {
        let keywords: Vec<Yaml> = keywords_content
            .split(',')
            .map(str::trim)
            .filter(|s| !s.is_empty())
            .map(|k| Yaml::String(k.to_string()))
            .collect();
        if !keywords.is_empty() {
            fm_map.insert("keywords".to_string(), Yaml::Array(keywords));
        }
    }

    for (key, sources) in FIELDS {
        let value = sources
            .iter()
            .filter_map(|source| match source {
                Source::Meta(name) => meta.get(*name).cloned(),
                Source::Schema(name) => schema_field(name),
            })
            .find(|value| !value.trim().is_empty());
        if let Some(value) = value {
            fm_map.insert(key.to_string(), Yaml::String(value));
        }
    }

    if fm_map.is_empty() { None } else { Some(fm_map) }
}

/// Converts an HTML document to a Markdown string.
pub fn convert_html_to_markdown(html_input: &str, options: ConversionOptions) -> miette::Result<String> {
    if html_input.trim().is_empty() {
        return Ok("".to_string());
    }

    let html = Html::parse_document(html_input);

    let mut front_matter_str = String::new();

    if options.generate_front_matter
        && let Some(fm_data) = extract_front_matter_from_head_ref(&html)
        && !fm_data.is_empty()
    {
        // fm_data is a BTreeMap, so key order stays sorted going into the Hash.
        let mut yaml_hash = Hash::new();
        for (k, v) in fm_data {
            yaml_hash.insert(Yaml::String(k), v);
        }
        let yaml_value = Yaml::Hash(yaml_hash);
        let mut yaml = String::new();

        yaml_rust2::YamlEmitter::new(&mut yaml)
            .dump(&yaml_value)
            .map_err(|_| miette!("YAML serialization failed"))?;

        // `dump` always prefixes with `---`; we add our own delimiters below.
        let content = yaml.strip_prefix("---\n").unwrap_or(&yaml).trim_end_matches('\n');
        front_matter_str = format!("---\n{}\n---\n\n", content.trim());
    }

    // Smart extraction: prefer semantic/entry-point containers, then content-scoring
    // (for non-semantic markup), then fall back to the full document.
    const ENTRY_POINT_SELECTORS: &[&str] = &[
        "main",
        "[role=\"main\"]",
        "article",
        "[role=\"article\"]",
        "#content",
        ".post-content",
        ".entry-content",
        ".article-content",
        ".article-body",
        ".markdown-body",
    ];
    // An entry point is skipped if it would drop headings/prose elsewhere in the body
    // (e.g. a feed of several `<article>`s, or a small `<article>` beside the real content).
    let body = find_element(&html, "body");
    let doc_children = ENTRY_POINT_SELECTORS
        .iter()
        .find_map(|sel| {
            let el = find_element(&html, sel)?;
            if body.is_some_and(|b| scoring::has_heading_or_prose_outside(b, el.id())) {
                return None;
            }
            Some(el.children().collect::<Vec<_>>())
        })
        .or_else(|| scoring::find_best_candidate(&html).map(|el| el.children().collect::<Vec<_>>()))
        .unwrap_or_else(|| html.root_element().children().collect());

    let mut nodes_for_markdown_conversion = parser::map_nodes_to_html_nodes(doc_children)?;
    strip_noise_nodes(&mut nodes_for_markdown_conversion);
    if let Some(base) = resolve_base_url(&html, options.base_url.as_deref()) {
        resolve_relative_urls(&mut nodes_for_markdown_conversion, &base);
    }
    let body_markdown = converter::convert_nodes_to_markdown(&nodes_for_markdown_conversion, &options)?;

    // Extract <title> from <head> separately so it is available even when smart extraction
    // selected only the children of <main>/<article> (which do not include <head>).
    let title_prefix = if options.use_title_as_h1 {
        extract_title_text(&html)
            .map(|t| format!("# {}\n\n", t))
            .unwrap_or_default()
    } else {
        String::new()
    };

    Ok(format!("{}{}{}", front_matter_str, title_prefix, body_markdown))
}