import { parseHTML } from "linkedom";
function getMetaContent(doc, nameOrProperty) {
  const el = doc.querySelector(`meta[name="${nameOrProperty}"]`) ?? doc.querySelector(`meta[property="${nameOrProperty}"]`);
  return el?.getAttribute("content") ?? void 0;
}
function firstPresent(...values) {
  for (const v of values) {
    if (v && v.trim().length > 0) return v;
  }
  return void 0;
}
function extractMetadata(html) {
  const { document: doc } = parseHTML(html);
  const result = {};
  const title = doc.querySelector("title")?.textContent?.trim();
  if (title) result.title = title;
  const description = firstPresent(
    getMetaContent(doc, "description"),
    getMetaContent(doc, "og:description"),
    getMetaContent(doc, "twitter:description")
  );
  if (description) result.description = description;
  const author = getMetaContent(doc, "author");
  if (author) result.author = author;
  const date = firstPresent(
    getMetaContent(doc, "date"),
    getMetaContent(doc, "article:published_time")
  );
  if (date) result.date = date;
  const keywords = getMetaContent(doc, "keywords");
  if (keywords) {
    result.keywords = keywords.split(",").map((k) => k.trim()).filter(Boolean);
  }
  const ogImage = firstPresent(
    getMetaContent(doc, "og:image"),
    getMetaContent(doc, "og:image:secure_url"),
    getMetaContent(doc, "twitter:image"),
    getMetaContent(doc, "twitter:image:src")
  );
  if (ogImage) result.og_image = ogImage;
  const ogType = firstPresent(
    getMetaContent(doc, "og:type"),
    getMetaContent(doc, "twitter:card")
  );
  if (ogType) result.og_type = ogType;
  const canonical = doc.querySelector('link[rel="canonical"]')?.getAttribute("href");
  if (canonical) result.canonical_url = canonical;
  return result;
}
export {
  extractMetadata
};
//# sourceMappingURL=metadata.js.map