BREAKING CHANGE(api): redesign smartsitemap around builder-based sitemap creation, parsing, validation, and import utilities
This commit is contained in:
61
ts/smartsitemap.classes.yamlimporter.ts
Normal file
61
ts/smartsitemap.classes.yamlimporter.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import * as plugins from './smartsitemap.plugins.js';
|
||||
import type * as interfaces from './interfaces/index.js';
|
||||
|
||||
/**
|
||||
* Imports sitemap configuration from YAML format.
|
||||
* Supports the enhanced YAML schema with per-frequency URL groups,
|
||||
* default settings, and feed imports.
|
||||
*/
|
||||
export class YamlImporter {
|
||||
/**
|
||||
* Parse a YAML config string and return ISitemapUrl entries.
|
||||
*/
|
||||
static async parseConfig(yamlString: string): Promise<interfaces.ISitemapUrl[]> {
|
||||
const config = (await plugins.smartyaml.yamlStringToObject(yamlString)) as interfaces.ISitemapYamlConfig;
|
||||
const urls: interfaces.ISitemapUrl[] = [];
|
||||
const baseUrl = config.baseUrl?.replace(/\/$/, '') ?? '';
|
||||
|
||||
// Process URL groups by frequency
|
||||
if (config.urls) {
|
||||
const frequencies: interfaces.TChangeFreq[] = [
|
||||
'always', 'hourly', 'daily', 'weekly', 'monthly', 'yearly', 'never',
|
||||
];
|
||||
|
||||
for (const freq of frequencies) {
|
||||
const urlList = config.urls[freq];
|
||||
if (urlList && Array.isArray(urlList)) {
|
||||
for (const path of urlList) {
|
||||
const loc = path.startsWith('http') ? path : `${baseUrl}${path.startsWith('/') ? '' : '/'}${path}`;
|
||||
urls.push({
|
||||
loc,
|
||||
changefreq: freq,
|
||||
priority: config.defaults?.priority,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process feed imports
|
||||
if (config.feeds && Array.isArray(config.feeds)) {
|
||||
// Dynamic import to avoid circular deps at module load time
|
||||
const { FeedImporter } = await import('./smartsitemap.classes.feedimporter.js');
|
||||
|
||||
for (const feedConfig of config.feeds) {
|
||||
if (feedConfig.type === 'news') {
|
||||
const newsUrls = await FeedImporter.fromUrlAsNews(
|
||||
feedConfig.url,
|
||||
feedConfig.publicationName ?? 'Unknown',
|
||||
feedConfig.publicationLanguage ?? 'en',
|
||||
);
|
||||
urls.push(...newsUrls);
|
||||
} else {
|
||||
const standardUrls = await FeedImporter.fromUrl(feedConfig.url);
|
||||
urls.push(...standardUrls);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return urls;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user