feat(theme): make it possible to provide your own page title formatter (#11090)

This commit is contained in:
Sébastien Lorber 2025-04-11 19:16:17 +02:00 committed by GitHub
parent 5b944d6b64
commit 730ce485ba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 386 additions and 107 deletions

View file

@ -0,0 +1,36 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import React, {type ComponentProps, type ReactNode} from 'react';
import {TitleFormatterProvider} from '@docusaurus/theme-common/internal';
import type {Props} from '@theme/ThemeProvider/TitleFormatter';
type FormatterProp = ComponentProps<typeof TitleFormatterProvider>['formatter'];
const formatter: FormatterProp = (params) => {
// Custom title for dogfood plugin instances
if (params.plugin.id.endsWith('tests')) {
const pluginLabel = `${params.plugin.name.replace(
'docusaurus-plugin-content-',
'',
)} plugin`;
return `🐕 Dogfood - ${pluginLabel}`;
}
// Default title otherwise
return params.defaultFormatter(params);
};
export default function ThemeProviderTitleFormatter({
children,
}: Props): ReactNode {
return (
<TitleFormatterProvider formatter={formatter}>
{children}
</TitleFormatterProvider>
);
}