diff --git a/lib/sitemap-simple.ts b/lib/sitemap-simple.ts index 4d065a5..d345c1e 100644 --- a/lib/sitemap-simple.ts +++ b/lib/sitemap-simple.ts @@ -13,6 +13,11 @@ import { URL } from 'url'; import { WriteStream } from 'fs'; const pipeline = promisify(pline); + +function defaultNameGenerator(index: number) { + return `./sitemap-${index}.xml`; +} + /** * * @param {object} options - @@ -36,6 +41,7 @@ export const simpleSitemapAndIndex = async ({ limit = 50000, gzip = true, publicBasePath = './', + nameGenerator = defaultNameGenerator, }: { hostname: string; sitemapHostname?: string; @@ -44,6 +50,7 @@ export const simpleSitemapAndIndex = async ({ publicBasePath?: string; limit?: number; gzip?: boolean; + nameGenerator?: (index: number) => string; }): Promise => { await promises.mkdir(destinationDir, { recursive: true }); const sitemapAndIndexStream = new SitemapAndIndexStream({ @@ -52,7 +59,7 @@ export const simpleSitemapAndIndex = async ({ const sitemapStream = new SitemapStream({ hostname, }); - const path = `./sitemap-${i}.xml`; + const path = nameGenerator(i); const writePath = resolve(destinationDir, path + (gzip ? '.gz' : '')); if (!publicBasePath.endsWith('/')) { publicBasePath += '/'; diff --git a/tests/sitemap-simple.test.ts b/tests/sitemap-simple.test.ts index 526aa1f..83b41a5 100644 --- a/tests/sitemap-simple.test.ts +++ b/tests/sitemap-simple.test.ts @@ -37,6 +37,16 @@ describe('simpleSitemapAndIndex', () => { ]); }); + afterEach(() => { + removeFilesArray([ + resolve(targetFolder, `./sitemap-index.xml.gz`), + resolve(targetFolder, `./0.xml`), + resolve(targetFolder, `./1.xml`), + resolve(targetFolder, `./2.xml`), + resolve(targetFolder, `./3.xml`), + ]); + }); + it('writes both a sitemap and index', async () => { const baseURL = 'https://example.com/sub/'; @@ -303,4 +313,42 @@ describe('simpleSitemapAndIndex', () => { ); expect(xml.toString()).toContain('1.example.com'); }); + + it('able to generate sitemap names with provided generator', async () => { + const baseURL = 'https://example.com/sub/'; + + await simpleSitemapAndIndex({ + hostname: baseURL, + sourceData: [ + 'https://1.example.com/a', + 'https://2.example.com/a', + 'https://3.example.com/a', + 'https://4.example.com/a', + ], + destinationDir: targetFolder, + limit: 1, + nameGenerator: (index) => `${index}.xml`, + gzip: false, + }); + + const index = ( + await streamToPromise( + createReadStream(resolve(targetFolder, `./sitemap-index.xml`)) + ) + ).toString(); + expect(index).toContain(`${baseURL}0`); + expect(index).toContain(`${baseURL}1`); + expect(index).toContain(`${baseURL}2`); + expect(index).toContain(`${baseURL}3`); + expect(index).not.toContain(`${baseURL}4`); + expect(existsSync(resolve(targetFolder, './0.xml'))).toBe(true); + expect(existsSync(resolve(targetFolder, './1.xml'))).toBe(true); + expect(existsSync(resolve(targetFolder, './2.xml'))).toBe(true); + expect(existsSync(resolve(targetFolder, './3.xml'))).toBe(true); + expect(existsSync(resolve(targetFolder, './4.xml'))).toBe(false); + const xml = await streamToPromise( + createReadStream(resolve(targetFolder, './0.xml')) + ); + expect(xml.toString()).toContain('https://1.example.com/a'); + }); });