import { extractNews } from "../news.js";
function cleanArticleBody(body) {
  if (!body) return body;
  let cleaned = body;
  cleaned = stripReferencesSection(cleaned);
  cleaned = cleaned.replace(/\$\$[\s\S]*?\$\$/g, "");
  cleaned = cleaned.replace(/^\|[^\n]*\b(?:Cite this page|Wikidata item|Wikimedia Commons|Wikiquote)\b[^\n]*\|[\s]*$\n(?:^\|[\s\-:|]+\|[\s]*$\n?)?/gim, "").replace(/\[\s*edit\s*\]/gi, "");
  cleaned = cleaned.replace(/\n{3,}/g, "\n\n").trim();
  return cleaned;
}
function stripReferencesSection(body) {
  const lines = body.split("\n");
  const out = [];
  let inSection = false;
  let sectionHeadingLevel = 0;
  const CHROME_TITLES = /^(?:references|notes|citations|external links|further reading|see also|bibliography)\s*$/i;
  const HEADING_RE = /^(#+)\s+(.+?)\s*$/;
  const BOLD_TITLE_RE = /^\*\*(.+?)\*\*\s*$/;
  for (const line of lines) {
    if (!inSection) {
      const h2 = line.match(HEADING_RE);
      const bt = line.match(BOLD_TITLE_RE);
      const plain = !h2 && !bt && CHROME_TITLES.test(line.trim());
      if (h2 && CHROME_TITLES.test(h2[2])) {
        inSection = true;
        sectionHeadingLevel = h2[1].length;
        continue;
      }
      if (bt && CHROME_TITLES.test(bt[1])) {
        inSection = true;
        sectionHeadingLevel = 0;
        continue;
      }
      if (plain) {
        inSection = true;
        sectionHeadingLevel = 0;
        continue;
      }
      out.push(line);
      continue;
    }
    const h = line.match(HEADING_RE);
    if (h) {
      const level = h[1].length;
      if (sectionHeadingLevel === 0 || level <= sectionHeadingLevel) {
        if (CHROME_TITLES.test(h[2])) {
          sectionHeadingLevel = level;
          continue;
        }
        inSection = false;
        out.push(line);
        continue;
      }
    }
  }
  return out.join("\n");
}
async function extractArticle(html, url) {
  const result = await extractNews(html, url);
  if (!result) return null;
  const title = (result.title ?? "").trim();
  const body = cleanArticleBody((result.markdown ?? "").trim());
  if (!title && !body) return null;
  const meta = result.metadata ?? {};
  const data = {
    title,
    body,
    url
  };
  if (meta.author) data.author = meta.author;
  if (meta.date) data.date = meta.date;
  if (meta.description) data.description = meta.description;
  if (meta.language) data.language = meta.language;
  return data;
}
export {
  cleanArticleBody,
  extractArticle
};
//# sourceMappingURL=Article.js.map