BREAKING CHANGE(api): redesign smartsitemap around builder-based sitemap creation, parsing, validation, and import utilities
This commit is contained in:
95
ts/smartsitemap.classes.newsbuilder.ts
Normal file
95
ts/smartsitemap.classes.newsbuilder.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
import * as plugins from './smartsitemap.plugins.js';
|
||||
import type * as interfaces from './interfaces/index.js';
|
||||
import { UrlsetBuilder } from './smartsitemap.classes.urlsetbuilder.js';
|
||||
import { FeedImporter } from './smartsitemap.classes.feedimporter.js';
|
||||
|
||||
/**
|
||||
* Specialized builder for Google News sitemaps.
|
||||
* Extends UrlsetBuilder with news-specific convenience methods.
|
||||
* All standard builder methods (add, filter, merge, etc.) are inherited.
|
||||
*/
|
||||
export class NewsSitemapBuilder extends UrlsetBuilder {
|
||||
private publicationName: string;
|
||||
private publicationLanguage: string;
|
||||
|
||||
constructor(options: interfaces.INewsSitemapOptions) {
|
||||
super(options);
|
||||
this.publicationName = options.publicationName;
|
||||
this.publicationLanguage = options.publicationLanguage ?? 'en';
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a news article URL with convenient parameters.
|
||||
* Automatically fills in publication name and language from constructor options.
|
||||
*/
|
||||
addNewsUrl(
|
||||
loc: string,
|
||||
title: string,
|
||||
publicationDate: Date | string | number,
|
||||
keywords?: string[] | string,
|
||||
): this {
|
||||
this.add({
|
||||
loc,
|
||||
news: {
|
||||
publication: {
|
||||
name: this.publicationName,
|
||||
language: this.publicationLanguage,
|
||||
},
|
||||
publicationDate,
|
||||
title,
|
||||
keywords,
|
||||
},
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Import from RSS/Atom feed URL, automatically mapping items to news entries.
|
||||
*/
|
||||
async importFromFeedUrl(feedUrl: string, options?: interfaces.IFeedImportOptions): Promise<this> {
|
||||
const imported = await FeedImporter.fromUrlAsNews(
|
||||
feedUrl,
|
||||
options?.publicationName ?? this.publicationName,
|
||||
options?.publicationLanguage ?? this.publicationLanguage,
|
||||
options,
|
||||
);
|
||||
this.addUrls(imported);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Import from RSS/Atom feed string, automatically mapping items to news entries.
|
||||
*/
|
||||
async importFromFeedString(feedXml: string, options?: interfaces.IFeedImportOptions): Promise<this> {
|
||||
const imported = await FeedImporter.fromStringAsNews(
|
||||
feedXml,
|
||||
options?.publicationName ?? this.publicationName,
|
||||
options?.publicationLanguage ?? this.publicationLanguage,
|
||||
options,
|
||||
);
|
||||
this.addUrls(imported);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Import from @tsclass/tsclass IArticle array with proper news mapping.
|
||||
*/
|
||||
importFromArticles(articles: plugins.tsclass.content.IArticle[]): this {
|
||||
for (const article of articles) {
|
||||
this.add({
|
||||
loc: article.url,
|
||||
lastmod: article.timestamp ? new Date(article.timestamp) : undefined,
|
||||
news: {
|
||||
publication: {
|
||||
name: this.publicationName,
|
||||
language: this.publicationLanguage,
|
||||
},
|
||||
publicationDate: article.timestamp ? new Date(article.timestamp) : new Date(),
|
||||
title: article.title || '',
|
||||
keywords: article.tags,
|
||||
},
|
||||
});
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user