From 81723ba2f2ad37ba7ff73786f39c84268ada4279 Mon Sep 17 00:00:00 2001 From: Sondre Aasemoen Date: Thu, 18 Jul 2024 01:08:32 +0200 Subject: [PATCH 1/3] feat(sitemap): add xslURL to enable styling --- .changeset/proud-horses-cry.md | 5 +++++ packages/integrations/sitemap/src/index.ts | 8 +++++--- packages/integrations/sitemap/src/schema.ts | 1 + packages/integrations/sitemap/src/write-sitemap.ts | 5 ++++- 4 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 .changeset/proud-horses-cry.md diff --git a/.changeset/proud-horses-cry.md b/.changeset/proud-horses-cry.md new file mode 100644 index 000000000000..6c137b719c76 --- /dev/null +++ b/.changeset/proud-horses-cry.md @@ -0,0 +1,5 @@ +--- +'@astrojs/sitemap': minor +--- + +Add new option `xslURL` to `@astrojs/sitemap` to enable styling of sitemaps. diff --git a/packages/integrations/sitemap/src/index.ts b/packages/integrations/sitemap/src/index.ts index 98aa4f31cc74..839a1d277a8b 100644 --- a/packages/integrations/sitemap/src/index.ts +++ b/packages/integrations/sitemap/src/index.ts @@ -88,7 +88,7 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => { const { filter, customPages, serialize, entryLimit } = opts; - let finalSiteUrl = new URL(config.base, config.site); + const finalSiteUrl = new URL(config.base, config.site); const shouldIgnoreStatus = isStatusCodePage(Object.keys(opts.i18n?.locales ?? {})); let pageUrls = pages .filter((p) => !shouldIgnoreStatus(p.pathname)) @@ -100,7 +100,7 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => { return new URL(fullPath, finalSiteUrl).href; }); - let routeUrls = routes.reduce((urls, r) => { + const routeUrls = routes.reduce((urls, r) => { // Only expose pages, not endpoints or redirects if (r.type !== 'page') return urls; @@ -116,7 +116,7 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => { if (fullPath.endsWith('/')) fullPath += r.generate(r.pathname).substring(1); else fullPath += r.generate(r.pathname); - let newUrl = new URL(fullPath, finalSiteUrl).href; + const newUrl = new URL(fullPath, finalSiteUrl).href; if (config.trailingSlash === 'never') { urls.push(newUrl); @@ -168,6 +168,7 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => { } } const destDir = fileURLToPath(dir); + const xslURL = opts.xslURL ? new URL(opts.xslURL, finalSiteUrl).href : undefined; await writeSitemap( { hostname: finalSiteUrl.href, @@ -175,6 +176,7 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => { publicBasePath: config.base, sourceData: urlData, limit: entryLimit, + xslURL: xslURL, }, config ); diff --git a/packages/integrations/sitemap/src/schema.ts b/packages/integrations/sitemap/src/schema.ts index a7682e881a05..0a4827681bb2 100644 --- a/packages/integrations/sitemap/src/schema.ts +++ b/packages/integrations/sitemap/src/schema.ts @@ -9,6 +9,7 @@ export const SitemapOptionsSchema = z filter: z.function().args(z.string()).returns(z.boolean()).optional(), customPages: z.string().url().array().optional(), canonicalURL: z.string().url().optional(), + xslURL: z.string().url().optional(), i18n: z .object({ diff --git a/packages/integrations/sitemap/src/write-sitemap.ts b/packages/integrations/sitemap/src/write-sitemap.ts index 9993d7790bb3..f2f4cbb7a7ab 100644 --- a/packages/integrations/sitemap/src/write-sitemap.ts +++ b/packages/integrations/sitemap/src/write-sitemap.ts @@ -17,6 +17,7 @@ type WriteSitemapConfig = { destinationDir: string; publicBasePath?: string; limit?: number; + xslURL?: string; }; // adapted from sitemap.js/sitemap-simple @@ -28,6 +29,7 @@ export async function writeSitemap( destinationDir, limit = 50000, publicBasePath = './', + xslURL: xslUrl, }: WriteSitemapConfig, astroConfig: AstroConfig ) { @@ -38,6 +40,7 @@ export async function writeSitemap( getSitemapStream: (i) => { const sitemapStream = new SitemapStream({ hostname, + xslUrl, }); const path = `./sitemap-${i}.xml`; const writePath = resolve(destinationDir, path); @@ -63,7 +66,7 @@ export async function writeSitemap( }, }); - let src = Readable.from(sourceData); + const src = Readable.from(sourceData); const indexPath = resolve(destinationDir, `./sitemap-index.xml`); return promisify(pipeline)(src, sitemapAndIndexStream, createWriteStream(indexPath)); } From c9cd54fef948e077c1c690ec108ff3a5c9bd8441 Mon Sep 17 00:00:00 2001 From: bluwy Date: Mon, 30 Sep 2024 11:11:40 +0800 Subject: [PATCH 2/3] Add test --- packages/integrations/sitemap/src/schema.ts | 2 +- .../test/{filter.test.js => config.test.js} | 24 ++++++++++++++++--- pnpm-lock.yaml | 1 - 3 files changed, 22 insertions(+), 5 deletions(-) rename packages/integrations/sitemap/test/{filter.test.js => config.test.js} (61%) diff --git a/packages/integrations/sitemap/src/schema.ts b/packages/integrations/sitemap/src/schema.ts index c0133ba79aa3..ba5438452395 100644 --- a/packages/integrations/sitemap/src/schema.ts +++ b/packages/integrations/sitemap/src/schema.ts @@ -9,7 +9,7 @@ export const SitemapOptionsSchema = z filter: z.function().args(z.string()).returns(z.boolean()).optional(), customPages: z.string().url().array().optional(), canonicalURL: z.string().url().optional(), - xslURL: z.string().url().optional(), + xslURL: z.string().optional(), i18n: z .object({ diff --git a/packages/integrations/sitemap/test/filter.test.js b/packages/integrations/sitemap/test/config.test.js similarity index 61% rename from packages/integrations/sitemap/test/filter.test.js rename to packages/integrations/sitemap/test/config.test.js index adecb59e6c0e..e4b7c38826e8 100644 --- a/packages/integrations/sitemap/test/filter.test.js +++ b/packages/integrations/sitemap/test/config.test.js @@ -3,7 +3,7 @@ import { before, describe, it } from 'node:test'; import { sitemap } from './fixtures/static/deps.mjs'; import { loadFixture, readXML } from './test-utils.js'; -describe('Filter support', () => { +describe('Config', () => { /** @type {import('./test-utils.js').Fixture} */ let fixture; @@ -14,17 +14,26 @@ describe('Filter support', () => { integrations: [ sitemap({ filter: (page) => page === 'http://example.com/one/', + xslURL: '/sitemap.xsl', }), ], }); await fixture.build(); }); - it('Just one page is added', async () => { + it('filter: Just one page is added', async () => { const data = await readXML(fixture.readFile('/sitemap-0.xml')); const urls = data.urlset.url; assert.equal(urls.length, 1); }); + + it('xslURL: Includes xml-stylsheet', async () => { + const xml = await fixture.readFile('/sitemap-0.xml'); + assert.ok( + xml.includes(''), + xml, + ); + }); }); describe('SSR', () => { @@ -34,16 +43,25 @@ describe('Filter support', () => { integrations: [ sitemap({ filter: (page) => page === 'http://example.com/one/', + xslURL: '/sitemap.xsl', }), ], }); await fixture.build(); }); - it('Just one page is added', async () => { + it('filter: Just one page is added', async () => { const data = await readXML(fixture.readFile('/client/sitemap-0.xml')); const urls = data.urlset.url; assert.equal(urls.length, 1); }); + + it('xslURL: Includes xml-stylsheet', async () => { + const xml = await fixture.readFile('/client/sitemap-0.xml'); + assert.ok( + xml.includes(''), + xml, + ); + }); }); }); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fde5059a066f..b5a636d468b5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8853,7 +8853,6 @@ packages: libsql@0.4.5: resolution: {integrity: sha512-sorTJV6PNt94Wap27Sai5gtVLIea4Otb2LUiAUyr3p6BPOScGMKGt5F1b5X/XgkNtcsDKeX5qfeBDj+PdShclQ==} - cpu: [x64, arm64, wasm32] os: [darwin, linux, win32] lilconfig@2.1.0: From c261600c88769bc1e64a904ef390d118f4f365be Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Mon, 30 Sep 2024 08:44:37 +0200 Subject: [PATCH 3/3] Update .changeset/proud-horses-cry.md --- .changeset/proud-horses-cry.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/proud-horses-cry.md b/.changeset/proud-horses-cry.md index 6c137b719c76..8d086b071c59 100644 --- a/.changeset/proud-horses-cry.md +++ b/.changeset/proud-horses-cry.md @@ -2,4 +2,4 @@ '@astrojs/sitemap': minor --- -Add new option `xslURL` to `@astrojs/sitemap` to enable styling of sitemaps. +Adds new `xslURL` option to enable styling of sitemaps