Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/sitemap-skip-index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@doc-kit/generator-react': patch
---

Skip the `index` entry when building page URLs in the `sitemap` generator so the index page is only listed once under `indexURL`.
45 changes: 45 additions & 0 deletions packages/react/src/sitemap/__tests__/generate.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';

import { setConfig } from '@doc-kit/core/utils/configuration/index.mjs';

import { generate } from '../generate.mjs';

const config = await setConfig({ target: ['sitemap'] });

config.sitemap.output = undefined;
config.sitemap.baseURL = 'https://example.com';

const entry = (api, depth = 1) => ({
api,
path: `/${api}`,
heading: { depth },
});

const extractLocations = sitemap =>
Array.from(sitemap.matchAll(/<loc>(.*?)<\/loc>/g), match => match[1]);

describe('sitemap', () => {
it('lists depth-1 pages alongside the index URL', async () => {
const sitemap = await generate([
entry('fs', 1),
entry('fs', 2),
entry('http', 1),
]);

assert.deepEqual(extractLocations(sitemap), [
'https://example.com/fs.html',
'https://example.com/http.html',
'https://example.com/',
]);
});

it('does not list the index page twice when index.md is an input', async () => {
const sitemap = await generate([entry('index', 1), entry('foo', 1)]);

assert.deepEqual(extractLocations(sitemap), [
'https://example.com/foo.html',
'https://example.com/',
]);
});
});
2 changes: 1 addition & 1 deletion packages/react/src/sitemap/generate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function generate(entries) {
const lastmod = new Date().toISOString().split('T')[0];

const apiPages = entries
.filter(entry => entry.heading.depth === 1)
.filter(entry => entry.heading.depth === 1 && entry.api !== 'index')
.map(entry => createPageSitemapEntry(entry, config, lastmod));

const loc = populate(config.indexURL, config);
Expand Down
Loading