Skip to content

Commit 5cf2c39

Browse files
ilg-ulslorber
andauthored
feat(blog): Add frontMatter.title_meta to override title for SEO (#10586)
Co-authored-by: sebastien <[email protected]>
1 parent 5c1ce01 commit 5cf2c39

File tree

5 files changed

+23
-1
lines changed

5 files changed

+23
-1
lines changed

packages/docusaurus-plugin-content-blog/src/__tests__/frontMatter.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,21 @@ describe('validateBlogPostFrontMatter title', () => {
9393
{title: ''},
9494
{title: 'title'},
9595
],
96+
invalidFrontMatters: [
97+
[{title: null}, 'must be a string'],
98+
[{title: false}, 'must be a string'],
99+
],
100+
});
101+
});
102+
103+
describe('validateBlogPostFrontMatter title_meta', () => {
104+
testField({
105+
prefix: 'title_meta',
106+
validFrontMatters: [{title: ''}, {title_meta: 'title'}],
107+
invalidFrontMatters: [
108+
[{title_meta: null}, 'must be a string'],
109+
[{title_meta: false}, 'must be a string'],
110+
],
96111
});
97112
});
98113

packages/docusaurus-plugin-content-blog/src/frontMatter.ts

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const FrontMatterAuthorErrorMessage =
3333
const BlogFrontMatterSchema = Joi.object<BlogPostFrontMatter>({
3434
id: Joi.string(),
3535
title: Joi.string().allow(''),
36+
title_meta: Joi.string(),
3637
description: Joi.string().allow(''),
3738
tags: FrontMatterTagsSchema,
3839
date: Joi.date().raw(),

packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ declare module '@docusaurus/plugin-content-blog' {
143143
* @see {@link BlogPostMetadata.title}
144144
*/
145145
title?: string;
146+
/**
147+
* Will be used for SEO page metadata and override BlogPostMetadata.title.
148+
* @see {@link BlogPostMetadata.title_meta}
149+
*/
150+
title_meta?: string;
146151
/**
147152
* Will override the default excerpt.
148153
* @see {@link BlogPostMetadata.description}

packages/docusaurus-theme-classic/src/theme/BlogPostPage/Metadata/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default function BlogPostPageMetadata(): JSX.Element {
1717
const image = assets.image ?? frontMatter.image;
1818
return (
1919
<PageMetadata
20-
title={title}
20+
title={frontMatter.title_meta ?? title}
2121
description={description}
2222
keywords={keywords}
2323
image={image}>

website/docs/api/plugins/plugin-content-docs.mdx

+1
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ Accepted fields:
278278
| --- | --- | --- | --- |
279279
| `id` | `string` | file path (including folders, without the extension) | A unique document ID. |
280280
| `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. |
281+
| `title_meta` | `string` | `frontMatter.title` | The SEO metadata title of your document used in `<head>` for `<title>` and `og:title`. Permits to override `frontMatter.title` when the displayed title and SEO title should be different. |
281282
| `pagination_label` | `string` | `sidebar_label` or `title` | The text used in the document next/previous buttons for this document. |
282283
| `sidebar_label` | `string` | `title` | The text shown in the document sidebar for this document. |
283284
| `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). |

0 commit comments

Comments
 (0)