Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/sitemap-simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 -
Expand All @@ -36,6 +41,7 @@ export const simpleSitemapAndIndex = async ({
limit = 50000,
gzip = true,
publicBasePath = './',
nameGenerator = defaultNameGenerator,
}: {
hostname: string;
sitemapHostname?: string;
Expand All @@ -44,6 +50,7 @@ export const simpleSitemapAndIndex = async ({
publicBasePath?: string;
limit?: number;
gzip?: boolean;
nameGenerator?: (index: number) => string;
}): Promise<void> => {
await promises.mkdir(destinationDir, { recursive: true });
const sitemapAndIndexStream = new SitemapAndIndexStream({
Expand All @@ -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 += '/';
Expand Down
48 changes: 48 additions & 0 deletions tests/sitemap-simple.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/';

Expand Down Expand Up @@ -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');
});
});