feat(blog): Add frontMatter.title_meta to override title for SEO (#10586)

Co-authored-by: sebastien <lorber.sebastien@gmail.com>
This commit is contained in:
Liviu Ionescu 2024-10-31 12:59:35 +02:00 committed by GitHub
parent 5c1ce0137c
commit 5cf2c39836
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 23 additions and 1 deletions

View file

@ -93,6 +93,21 @@ describe('validateBlogPostFrontMatter title', () => {
{title: ''}, {title: ''},
{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'],
],
}); });
}); });

View file

@ -33,6 +33,7 @@ const FrontMatterAuthorErrorMessage =
const BlogFrontMatterSchema = Joi.object<BlogPostFrontMatter>({ const BlogFrontMatterSchema = Joi.object<BlogPostFrontMatter>({
id: Joi.string(), id: Joi.string(),
title: Joi.string().allow(''), title: Joi.string().allow(''),
title_meta: Joi.string(),
description: Joi.string().allow(''), description: Joi.string().allow(''),
tags: FrontMatterTagsSchema, tags: FrontMatterTagsSchema,
date: Joi.date().raw(), date: Joi.date().raw(),

View file

@ -143,6 +143,11 @@ declare module '@docusaurus/plugin-content-blog' {
* @see {@link BlogPostMetadata.title} * @see {@link BlogPostMetadata.title}
*/ */
title?: string; 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. * Will override the default excerpt.
* @see {@link BlogPostMetadata.description} * @see {@link BlogPostMetadata.description}

View file

@ -17,7 +17,7 @@ export default function BlogPostPageMetadata(): JSX.Element {
const image = assets.image ?? frontMatter.image; const image = assets.image ?? frontMatter.image;
return ( return (
<PageMetadata <PageMetadata
title={title} title={frontMatter.title_meta ?? title}
description={description} description={description}
keywords={keywords} keywords={keywords}
image={image}> image={image}>

View file

@ -278,6 +278,7 @@ Accepted fields:
| --- | --- | --- | --- | | --- | --- | --- | --- |
| `id` | `string` | file path (including folders, without the extension) | A unique document ID. | | `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` | `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 `<head>` for `<title>` 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. | | `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_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). | | `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). |