Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sitemap): add xslURL to enable styling #11485

Merged
merged 5 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions .changeset/proud-horses-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/sitemap': minor
---

Adds new `xslURL` option to enable styling of sitemaps
8 changes: 5 additions & 3 deletions packages/integrations/sitemap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -100,7 +100,7 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => {
return new URL(fullPath, finalSiteUrl).href;
});

let routeUrls = routes.reduce<string[]>((urls, r) => {
const routeUrls = routes.reduce<string[]>((urls, r) => {
// Only expose pages, not endpoints or redirects
if (r.type !== 'page') return urls;

Expand All @@ -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);
Expand Down Expand Up @@ -168,13 +168,15 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => {
}
}
const destDir = fileURLToPath(dir);
const xslURL = opts.xslURL ? new URL(opts.xslURL, finalSiteUrl).href : undefined;
florian-lefebvre marked this conversation as resolved.
Show resolved Hide resolved
florian-lefebvre marked this conversation as resolved.
Show resolved Hide resolved
await writeSitemap(
{
hostname: finalSiteUrl.href,
destinationDir: destDir,
publicBasePath: config.base,
sourceData: urlData,
limit: entryLimit,
xslURL: xslURL,
},
config,
);
Expand Down
1 change: 1 addition & 0 deletions packages/integrations/sitemap/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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().optional(),

i18n: z
.object({
Expand Down
5 changes: 4 additions & 1 deletion packages/integrations/sitemap/src/write-sitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type WriteSitemapConfig = {
destinationDir: string;
publicBasePath?: string;
limit?: number;
xslURL?: string;
};

// adapted from sitemap.js/sitemap-simple
Expand All @@ -28,6 +29,7 @@ export async function writeSitemap(
destinationDir,
limit = 50000,
publicBasePath = './',
xslURL: xslUrl,
}: WriteSitemapConfig,
astroConfig: AstroConfig,
) {
Expand All @@ -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);
Expand All @@ -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));
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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-stylesheet type="text/xsl" href="http://example.com/sitemap.xsl"?>'),
xml,
);
});
});

describe('SSR', () => {
Expand All @@ -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-stylesheet type="text/xsl" href="http://example.com/sitemap.xsl"?>'),
xml,
);
});
});
});
1 change: 0 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading