smartsitemap/ts/smartsitemap.classes.sitemapwebsite.ts

42 lines
1.1 KiB
TypeScript
Raw Normal View History

2020-10-26 12:18:33 +00:00
import * as plugins from './smartsitemap.plugins';
export type TUpdateFrequency = 'never' | 'daily' | 'weekly' | 'monthly' | 'yearly';
export interface IUrlInfo {
url: string;
timestamp: number;
frequency?: TUpdateFrequency;
}
export class SitemapWebsite {
urlInfos: IUrlInfo[] = [];
constructor() {}
public addUrl(urlInfoArg: IUrlInfo) {
this.urlInfos.push(urlInfoArg);
}
public exportSitemapXml() {
const urls: {
loc: string;
lastmod: string;
changefreq: TUpdateFrequency;
}[] = [];
for (const urlInfoArg of this.urlInfos) {
urls.push({
loc: urlInfoArg.url,
lastmod: new Date(urlInfoArg.timestamp).toISOString(),
changefreq: urlInfoArg.frequency ? urlInfoArg.frequency : 'weekly'
});
}
const sitemapObject: any = {
urlset: {
'@_xmlns': 'http://www.sitemaps.org/schemas/sitemap/0.9',
url: urls,
},
};
const smartxmlInstance = new plugins.smartxml.SmartXml();
const sitemapString = smartxmlInstance.createXmlFromObject(sitemapObject);
return sitemapString;
}
}