Skip to content

Commit

Permalink
feat(sitemap): add separator options
Browse files Browse the repository at this point in the history
this allow you to output sitemap files without dashes
  • Loading branch information
Hacksore committed Sep 2, 2024
1 parent e450704 commit 14e3f3a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
11 changes: 8 additions & 3 deletions packages/integrations/sitemap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export type SitemapOptions =

// called for each sitemap item just before to save them on disk, sync or async
serialize?(item: SitemapItem): SitemapItem | Promise<SitemapItem | undefined> | undefined;

separator?: string;
}
| undefined;

Expand All @@ -44,7 +46,6 @@ function formatConfigErrorMessage(err: ZodError) {
}

const PKG_NAME = '@astrojs/sitemap';
const OUTFILE = 'sitemap-index.xml';
const STATUS_CODE_PAGES = new Set(['404', '500']);

const isStatusCodePage = (locales: string[]) => {
Expand All @@ -66,6 +67,9 @@ const isStatusCodePage = (locales: string[]) => {
};
const createPlugin = (options?: SitemapOptions): AstroIntegration => {
let config: AstroConfig;
let separator = options?.separator ?? '-';

let outfile = `sitemap${separator}index.xml`;

return {
name: PKG_NAME,
Expand Down Expand Up @@ -142,7 +146,7 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => {
}

if (pageUrls.length === 0) {
logger.warn(`No pages found!\n\`${OUTFILE}\` not created.`);
logger.warn(`No pages found!\n\`${outfile}\` not created.`);
return;
}

Expand Down Expand Up @@ -175,10 +179,11 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => {
publicBasePath: config.base,
sourceData: urlData,
limit: entryLimit,
separator,
},
config,
);
logger.info(`\`${OUTFILE}\` created at \`${path.relative(process.cwd(), destDir)}\``);
logger.info(`\`${outfile}\` created at \`${path.relative(process.cwd(), destDir)}\``);
} catch (err) {
if (err instanceof ZodError) {
logger.warn(formatConfigErrorMessage(err));
Expand Down
6 changes: 4 additions & 2 deletions packages/integrations/sitemap/src/write-sitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { SitemapItem } from './index.js';

type WriteSitemapConfig = {
hostname: string;
separator: string;
sitemapHostname?: string;
sourceData: SitemapItem[];
destinationDir: string;
Expand All @@ -28,6 +29,7 @@ export async function writeSitemap(
destinationDir,
limit = 50000,
publicBasePath = './',
separator,
}: WriteSitemapConfig,
astroConfig: AstroConfig,
) {
Expand All @@ -39,7 +41,7 @@ export async function writeSitemap(
const sitemapStream = new SitemapStream({
hostname,
});
const path = `./sitemap-${i}.xml`;
const path = `./sitemap${separator}${i}.xml`;
const writePath = resolve(destinationDir, path);
if (!publicBasePath.endsWith('/')) {
publicBasePath += '/';
Expand All @@ -64,6 +66,6 @@ export async function writeSitemap(
});

let src = Readable.from(sourceData);
const indexPath = resolve(destinationDir, `./sitemap-index.xml`);
const indexPath = resolve(destinationDir, `./sitemap${separator}index.xml`);
return promisify(pipeline)(src, sitemapAndIndexStream, createWriteStream(indexPath));
}

0 comments on commit 14e3f3a

Please sign in to comment.