import * as plugins from './smartsitemap.plugins.js'; import type * as interfaces from './interfaces/index.js'; import { UrlsetBuilder } from './smartsitemap.classes.urlsetbuilder.js'; /** * Parses existing sitemap XML into structured data. * Handles both sitemaps and files. */ export class SitemapParser { /** * Parse a sitemap XML string into structured data. */ static async parse(xml: string): Promise { const smartXml = new plugins.smartxml.SmartXml(); const parsed = smartXml.parseXmlToObject(xml); // The parser returns ordered format (preserveOrder: true) // We need to walk the structure to extract urls or sitemap entries return SitemapParser.processOrderedParsed(parsed); } /** * Fetch and parse a sitemap from a URL. */ static async parseUrl(url: string): Promise { const response = await plugins.webrequest.webrequest(url); const xml = await response.text(); return SitemapParser.parse(xml); } /** * Parse a sitemap XML and return a pre-populated UrlsetBuilder. */ static async toBuilder(xml: string, options?: interfaces.ISitemapOptions): Promise { const parsed = await SitemapParser.parse(xml); const builder = new UrlsetBuilder(options); builder.addUrls(parsed.urls); return builder; } /** * Detect whether XML is a urlset or sitemapindex without full parsing. */ static detectType(xml: string): 'urlset' | 'sitemapindex' | 'unknown' { if (xml.includes('