Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Jan 17, 2025
1 parent cb724ea commit 2fd6501
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree.
*/

import {createElement} from 'react';
import {fromPartial} from '@total-typescript/shoehorn';
import createSitemap from '../createSitemap';
import type {PluginOptions} from '../options';
Expand Down Expand Up @@ -39,7 +38,7 @@ describe('createSitemap', () => {
const sitemap = await createSitemap({
siteConfig,
routes: routes(['/', '/test']),
head: {},
routesBuildMetadata: {},
options,
});
expect(sitemap).toContain(
Expand All @@ -51,7 +50,7 @@ describe('createSitemap', () => {
const sitemap = await createSitemap({
siteConfig,
routes: routes([]),
head: {},
routesBuildMetadata: {},
options,
});
expect(sitemap).toBeNull();
Expand All @@ -67,7 +66,7 @@ describe('createSitemap', () => {
'/search/foo',
'/tags/foo/bar',
]),
head: {},
routesBuildMetadata: {},
options: {
...options,
ignorePatterns: [
Expand All @@ -94,7 +93,7 @@ describe('createSitemap', () => {
'/search/foo',
'/tags/foo/bar',
]),
head: {},
routesBuildMetadata: {},
options: {
...options,
createSitemapItems: async (params) => {
Expand All @@ -119,7 +118,7 @@ describe('createSitemap', () => {
const sitemap = await createSitemap({
siteConfig,
routes: routes(['/', '/docs/myDoc/', '/blog/post']),
head: {},
routesBuildMetadata: {},
options: {
...options,
createSitemapItems: async () => {
Expand All @@ -135,7 +134,7 @@ describe('createSitemap', () => {
const sitemap = await createSitemap({
siteConfig,
routes: routes(['/', '/test', '/nested/test', '/nested/test2/']),
head: {},
routesBuildMetadata: {},
options,
});

Expand All @@ -149,7 +148,7 @@ describe('createSitemap', () => {
const sitemap = await createSitemap({
siteConfig: {...siteConfig, trailingSlash: true},
routes: routes(['/', '/test', '/nested/test', '/nested/test2/']),
head: {},
routesBuildMetadata: {},
options,
});

Expand All @@ -167,7 +166,7 @@ describe('createSitemap', () => {
trailingSlash: false,
},
routes: routes(['/', '/test', '/nested/test', '/nested/test2/']),
head: {},
routesBuildMetadata: {},
options,
});

Expand All @@ -180,19 +179,10 @@ describe('createSitemap', () => {
it('filters pages with noindex', async () => {
const sitemap = await createSitemap({
siteConfig,
routesPaths: ['/', '/noindex', '/nested/test', '/nested/test2/'],
routes: routes(['/', '/noindex', '/nested/test', '/nested/test2/']),
head: {
routesBuildMetadata: {
'/noindex': {
meta: {
// @ts-expect-error: bad lib def
toComponent: () => [
createElement('meta', {
name: 'robots',
content: 'NoFolloW, NoiNDeX',
}),
],
},
noIndex: true,
},
},
options,
Expand All @@ -204,24 +194,13 @@ describe('createSitemap', () => {
it('does not generate anything for all pages with noindex', async () => {
const sitemap = await createSitemap({
siteConfig,
routesPaths: ['/', '/noindex'],
routes: routes(['/', '/noindex']),
head: {
routesBuildMetadata: {
'/': {
meta: {
// @ts-expect-error: bad lib def
toComponent: () => [
createElement('meta', {name: 'robots', content: 'noindex'}),
],
},
noIndex: true,
},
'/noindex': {
meta: {
// @ts-expect-error: bad lib def
toComponent: () => [
createElement('meta', {name: 'robots', content: 'noindex'}),
],
},
noIndex: true,
},
},
options,
Expand Down
15 changes: 4 additions & 11 deletions packages/docusaurus-plugin-sitemap/src/createSitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ import {isNoIndexMetaRoute} from './head';
import type {CreateSitemapItemsFn, CreateSitemapItemsParams} from './types';
import type {RouteConfig, RouteBuildMetadata} from '@docusaurus/types';
import type {PluginOptions} from './options';
import type {HelmetServerState} from 'react-helmet-async';

// Not all routes should appear in the sitemap, and we should filter:
// - parent routes, used for layouts
// - routes matching options.ignorePatterns
// - routes with no index metadata
function getSitemapRoutes({
routes,
head,
routesBuildMetadata,
options,
}: CreateSitemapParams) {
Expand All @@ -31,20 +29,16 @@ function getSitemapRoutes({
function isRouteExcluded(route: RouteConfig) {
return (
ignoreMatcher(route.path) ||
isNoIndexMetaRoute({head, routesBuildMetadata, route: route.path})
isNoIndexMetaRoute({routesBuildMetadata, route: route.path})
);
}

return flattenRoutes(routes).filter((route) => !isRouteExcluded(route));
}

// Our default implementation receives some additional parameters on purpose
// Params such as "head" are "messy" and not directly exposed to the user
function createDefaultCreateSitemapItems(
internalParams: Pick<
CreateSitemapParams,
'head' | 'routesBuildMetadata' | 'options'
>,
internalParams: Pick<CreateSitemapParams, 'routesBuildMetadata' | 'options'>,
): CreateSitemapItemsFn {
return async (params) => {
const sitemapRoutes = getSitemapRoutes({...params, ...internalParams});
Expand All @@ -64,18 +58,17 @@ function createDefaultCreateSitemapItems(
}

type CreateSitemapParams = CreateSitemapItemsParams & {
head: {[location: string]: HelmetServerState};
routesBuildMetadata: {[location: string]: RouteBuildMetadata};
options: PluginOptions;
};

export default async function createSitemap(
params: CreateSitemapParams,
): Promise<string | null> {
const {head, routesBuildMetadata, options, routes, siteConfig} = params;
const {routesBuildMetadata, options, routes, siteConfig} = params;

const defaultCreateSitemapItems: CreateSitemapItemsFn =
createDefaultCreateSitemapItems({head, routesBuildMetadata, options});
createDefaultCreateSitemapItems({routesBuildMetadata, options});

const sitemapItems = params.options.createSitemapItems
? await params.options.createSitemapItems({
Expand Down
35 changes: 1 addition & 34 deletions packages/docusaurus-plugin-sitemap/src/head.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@
* LICENSE file in the root directory of this source tree.
*/

import type {ReactElement} from 'react';
import type {HelmetServerState} from 'react-helmet-async';
import type {RouteBuildMetadata} from '@docusaurus/types';

// Maybe we want to add a routeConfig.metadata.noIndex instead?
// But using Helmet is more reliable for third-party plugins...
export function isNoIndexMetaRoute({
head,
routesBuildMetadata,
route,
}: {
head: {[location: string]: HelmetServerState};
routesBuildMetadata: {[location: string]: RouteBuildMetadata};
route: string;
}): boolean {
Expand All @@ -25,34 +21,5 @@ export function isNoIndexMetaRoute({
if (routeBuildMetadata) {
return routeBuildMetadata.noIndex;
}

// TODO Docusaurus v4 remove legacy mess
// logic has been moved to core to create routeBuildMetadata
const isNoIndexMetaTag = ({
name,
content,
}: {
name?: string;
content?: string;
}): boolean => {
if (!name || !content) {
return false;
}
return (
// meta name is not case-sensitive
name.toLowerCase() === 'robots' &&
// Robots directives are not case-sensitive
content.toLowerCase().includes('noindex')
);
};

// https://github.com/staylor/react-helmet-async/pull/167
const meta = head[route]?.meta.toComponent() as unknown as
| ReactElement<{name?: string; content?: string}>[]
| undefined;
return (
meta?.some((tag) =>
isNoIndexMetaTag({name: tag.props.name, content: tag.props.content}),
) ?? false
);
return false;
}
3 changes: 1 addition & 2 deletions packages/docusaurus-plugin-sitemap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ export default function pluginSitemap(
return {
name: PluginName,

async postBuild({siteConfig, routes, outDir, head, routesBuildMetadata}) {
async postBuild({siteConfig, routes, outDir, routesBuildMetadata}) {
if (siteConfig.noIndex) {
return;
}
// Generate sitemap.
const generatedSitemap = await createSitemap({
siteConfig,
routes,
head,
routesBuildMetadata,
options,
});
Expand Down

0 comments on commit 2fd6501

Please sign in to comment.