113 lines
4.1 KiB
TypeScript
113 lines
4.1 KiB
TypeScript
import * as plugins from './smartsitemap.plugins.js';
|
|
import type * as interfaces from './interfaces/index.js';
|
|
import { UrlsetBuilder } from './smartsitemap.classes.urlsetbuilder.js';
|
|
import { NewsSitemapBuilder } from './smartsitemap.classes.newsbuilder.js';
|
|
import { SitemapIndexBuilder } from './smartsitemap.classes.indexbuilder.js';
|
|
import { SitemapParser } from './smartsitemap.classes.sitemapparser.js';
|
|
import { FeedImporter } from './smartsitemap.classes.feedimporter.js';
|
|
import { YamlImporter } from './smartsitemap.classes.yamlimporter.js';
|
|
import { SitemapValidator } from './smartsitemap.classes.validator.js';
|
|
|
|
/**
|
|
* Main entry point for @push.rocks/smartsitemap.
|
|
* Provides static factory methods for creating, parsing, and validating sitemaps.
|
|
*
|
|
* @example Simple sitemap
|
|
* ```typescript
|
|
* const xml = SmartSitemap.create()
|
|
* .addUrl('https://example.com/')
|
|
* .addUrl('https://example.com/about')
|
|
* .toXml();
|
|
* ```
|
|
*
|
|
* @example News sitemap from RSS feed
|
|
* ```typescript
|
|
* const builder = SmartSitemap.createNews({ publicationName: 'My Pub' });
|
|
* await builder.importFromFeedUrl('https://example.com/rss/');
|
|
* const xml = builder.toXml();
|
|
* ```
|
|
*/
|
|
export class SmartSitemap {
|
|
// ──────────────────────────────────────────────
|
|
// Static Factory Methods
|
|
// ──────────────────────────────────────────────
|
|
|
|
/** Create a standard sitemap builder */
|
|
static create(options?: interfaces.ISitemapOptions): UrlsetBuilder {
|
|
return new UrlsetBuilder(options);
|
|
}
|
|
|
|
/** Create a news sitemap builder */
|
|
static createNews(options: interfaces.INewsSitemapOptions): NewsSitemapBuilder {
|
|
return new NewsSitemapBuilder(options);
|
|
}
|
|
|
|
/** Create a sitemap index builder */
|
|
static createIndex(options?: interfaces.ISitemapOptions): SitemapIndexBuilder {
|
|
return new SitemapIndexBuilder(options);
|
|
}
|
|
|
|
/** Parse a sitemap XML string into structured data */
|
|
static async parse(xml: string): Promise<interfaces.IParsedSitemap> {
|
|
return SitemapParser.parse(xml);
|
|
}
|
|
|
|
/** Fetch and parse a sitemap from a URL */
|
|
static async parseUrl(url: string): Promise<interfaces.IParsedSitemap> {
|
|
return SitemapParser.parseUrl(url);
|
|
}
|
|
|
|
/** Create a UrlsetBuilder populated from an RSS/Atom feed URL */
|
|
static async fromFeedUrl(
|
|
feedUrl: string,
|
|
options?: interfaces.IFeedImportOptions,
|
|
): Promise<UrlsetBuilder> {
|
|
const urls = await FeedImporter.fromUrl(feedUrl, options);
|
|
const builder = new UrlsetBuilder();
|
|
builder.addUrls(urls);
|
|
return builder;
|
|
}
|
|
|
|
/** Create a UrlsetBuilder populated from an RSS/Atom feed string */
|
|
static async fromFeedString(
|
|
feedXml: string,
|
|
options?: interfaces.IFeedImportOptions,
|
|
): Promise<UrlsetBuilder> {
|
|
const urls = await FeedImporter.fromString(feedXml, options);
|
|
const builder = new UrlsetBuilder();
|
|
builder.addUrls(urls);
|
|
return builder;
|
|
}
|
|
|
|
/** Create a UrlsetBuilder populated from a YAML config string */
|
|
static async fromYaml(yamlString: string): Promise<UrlsetBuilder> {
|
|
const urls = await YamlImporter.parseConfig(yamlString);
|
|
const builder = new UrlsetBuilder();
|
|
builder.addUrls(urls);
|
|
return builder;
|
|
}
|
|
|
|
/** Create a NewsSitemapBuilder populated from @tsclass/tsclass IArticle array */
|
|
static fromArticles(
|
|
articles: plugins.tsclass.content.IArticle[],
|
|
options: interfaces.INewsSitemapOptions,
|
|
): NewsSitemapBuilder {
|
|
const builder = new NewsSitemapBuilder(options);
|
|
builder.importFromArticles(articles);
|
|
return builder;
|
|
}
|
|
|
|
/** Create a UrlsetBuilder from a simple URL string array */
|
|
static fromUrls(urls: string[], options?: interfaces.ISitemapOptions): UrlsetBuilder {
|
|
const builder = new UrlsetBuilder(options);
|
|
builder.addFromArray(urls);
|
|
return builder;
|
|
}
|
|
|
|
/** Validate a sitemap XML string */
|
|
static async validate(xml: string): Promise<interfaces.IValidationResult> {
|
|
const parsed = await SitemapParser.parse(xml);
|
|
return SitemapValidator.validateUrlset(parsed.urls);
|
|
}
|
|
}
|