From 5cf2c39836f96f261debfd652b566cce906fad85 Mon Sep 17 00:00:00 2001 From: Liviu Ionescu Date: Thu, 31 Oct 2024 12:59:35 +0200 Subject: [PATCH] feat(blog): Add `frontMatter.title_meta` to override title for SEO (#10586) Co-authored-by: sebastien --- .../src/__tests__/frontMatter.test.ts | 15 +++++++++++++++ .../src/frontMatter.ts | 1 + .../src/plugin-content-blog.d.ts | 5 +++++ .../src/theme/BlogPostPage/Metadata/index.tsx | 2 +- website/docs/api/plugins/plugin-content-docs.mdx | 1 + 5 files changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/frontMatter.test.ts b/packages/docusaurus-plugin-content-blog/src/__tests__/frontMatter.test.ts index 9c05dcca944a..f5ca63694c65 100644 --- a/packages/docusaurus-plugin-content-blog/src/__tests__/frontMatter.test.ts +++ b/packages/docusaurus-plugin-content-blog/src/__tests__/frontMatter.test.ts @@ -93,6 +93,21 @@ describe('validateBlogPostFrontMatter title', () => { {title: ''}, {title: 'title'}, ], + invalidFrontMatters: [ + [{title: null}, 'must be a string'], + [{title: false}, 'must be a string'], + ], + }); +}); + +describe('validateBlogPostFrontMatter title_meta', () => { + testField({ + prefix: 'title_meta', + validFrontMatters: [{title: ''}, {title_meta: 'title'}], + invalidFrontMatters: [ + [{title_meta: null}, 'must be a string'], + [{title_meta: false}, 'must be a string'], + ], }); }); diff --git a/packages/docusaurus-plugin-content-blog/src/frontMatter.ts b/packages/docusaurus-plugin-content-blog/src/frontMatter.ts index 6c3436198038..0e822258290b 100644 --- a/packages/docusaurus-plugin-content-blog/src/frontMatter.ts +++ b/packages/docusaurus-plugin-content-blog/src/frontMatter.ts @@ -33,6 +33,7 @@ const FrontMatterAuthorErrorMessage = const BlogFrontMatterSchema = Joi.object({ id: Joi.string(), title: Joi.string().allow(''), + title_meta: Joi.string(), description: Joi.string().allow(''), tags: FrontMatterTagsSchema, date: Joi.date().raw(), diff --git a/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts b/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts index 578688a4595b..1606aaa5f5e8 100644 --- a/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts +++ b/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts @@ -143,6 +143,11 @@ declare module '@docusaurus/plugin-content-blog' { * @see {@link BlogPostMetadata.title} */ title?: string; + /** + * Will be used for SEO page metadata and override BlogPostMetadata.title. + * @see {@link BlogPostMetadata.title_meta} + */ + title_meta?: string; /** * Will override the default excerpt. * @see {@link BlogPostMetadata.description} diff --git a/packages/docusaurus-theme-classic/src/theme/BlogPostPage/Metadata/index.tsx b/packages/docusaurus-theme-classic/src/theme/BlogPostPage/Metadata/index.tsx index 26a62049b827..2eb2eeb33b0e 100644 --- a/packages/docusaurus-theme-classic/src/theme/BlogPostPage/Metadata/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/BlogPostPage/Metadata/index.tsx @@ -17,7 +17,7 @@ export default function BlogPostPageMetadata(): JSX.Element { const image = assets.image ?? frontMatter.image; return ( diff --git a/website/docs/api/plugins/plugin-content-docs.mdx b/website/docs/api/plugins/plugin-content-docs.mdx index fa9ddbf53e6a..b14df3682712 100644 --- a/website/docs/api/plugins/plugin-content-docs.mdx +++ b/website/docs/api/plugins/plugin-content-docs.mdx @@ -278,6 +278,7 @@ Accepted fields: | --- | --- | --- | --- | | `id` | `string` | file path (including folders, without the extension) | A unique document ID. | | `title` | `string` | Markdown title or `id` | The text title of your document. Used for the page metadata and as a fallback value in multiple places (sidebar, next/previous buttons...). Automatically added at the top of your doc if it does not contain any Markdown title. | +| `title_meta` | `string` | `frontMatter.title` | The SEO metadata title of your document used in `` for `` and `og:title`. Permits to override `frontMatter.title` when the displayed title and SEO title should be different. | | `pagination_label` | `string` | `sidebar_label` or `title` | The text used in the document next/previous buttons for this document. | | `sidebar_label` | `string` | `title` | The text shown in the document sidebar for this document. | | `sidebar_position` | `number` | Default ordering | Controls the position of a doc inside the generated sidebar slice when using `autogenerated` sidebar items. See also [Autogenerated sidebar metadata](/docs/sidebar/autogenerated#autogenerated-sidebar-metadata). |