import {
  extractSection,
  extractLinksAndImages,
  filterDecorativeImages,
  resolveRelativeUrls
} from "./markdown.js";
import { extractMetadata } from "./metadata.js";
import { stripBoilerplateMarkdown } from "./boilerplate.js";
import { sanitizeExtractedMarkdown } from "./markdown-sanitize.js";
import { registerSiteExtractor } from "./v1/site-extractors.js";
import { getExtractProvider } from "../providers/extract-provider.js";
function registerExtractor(extractor) {
  registerSiteExtractor(extractor);
}
async function extractContent(html, url, options = {}) {
  const provider = await getExtractProvider();
  return provider.extract(html, url, options);
}
function mergeMetadata(base, html) {
  try {
    const meta = extractMetadata(html);
    return {
      ...meta,
      // Extractor-provided fields win when set (they already inspected the article body).
      description: base.description || meta.description,
      author: base.author || meta.author,
      date: base.date || meta.date,
      language: base.language,
      og_image: base.og_image ?? meta.og_image,
      og_type: base.og_type ?? meta.og_type,
      canonical_url: base.canonical_url ?? meta.canonical_url,
      keywords: base.keywords ?? meta.keywords
    };
  } catch {
    return base;
  }
}
function applyPostProcessing(result, url, html, options) {
  let markdown = result.markdown;
  markdown = resolveRelativeUrls(markdown, url);
  markdown = stripBoilerplateMarkdown(markdown);
  markdown = filterDecorativeImages(markdown);
  markdown = sanitizeExtractedMarkdown(markdown);
  if (options.section) {
    const { content } = extractSection(markdown, options.section, options.sectionIndex ?? 0);
    markdown = content;
  }
  const { links, images } = extractLinksAndImages(markdown);
  const metadata = mergeMetadata(result.metadata, html);
  if (options.maxChars && markdown.length > options.maxChars) {
    markdown = markdown.slice(0, options.maxChars);
  }
  return { ...result, markdown, links, images, metadata };
}
export {
  applyPostProcessing,
  extractContent,
  mergeMetadata,
  registerExtractor
};
//# sourceMappingURL=pipeline.js.map