Skip to content

Commit 24dbcb7

Browse files
committed
Implement frontMatter.sidebar_label for blog
1 parent df6f53a commit 24dbcb7

File tree

7 files changed

+34
-6
lines changed

7 files changed

+34
-6
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ describe('validateBlogPostFrontMatter description', () => {
7979
prefix: 'description',
8080
validFrontMatters: [
8181
// See https://github.com/facebook/docusaurus/issues/4591#issuecomment-822372398
82+
{description: undefined},
8283
{description: ''},
8384
{description: 'description'},
8485
],
@@ -90,6 +91,7 @@ describe('validateBlogPostFrontMatter title', () => {
9091
prefix: 'title',
9192
validFrontMatters: [
9293
// See https://github.com/facebook/docusaurus/issues/4591#issuecomment-822372398
94+
{title: undefined},
9395
{title: ''},
9496
{title: 'title'},
9597
],
@@ -103,14 +105,33 @@ describe('validateBlogPostFrontMatter title', () => {
103105
describe('validateBlogPostFrontMatter title_meta', () => {
104106
testField({
105107
prefix: 'title_meta',
106-
validFrontMatters: [{title: ''}, {title_meta: 'title'}],
108+
validFrontMatters: [
109+
{title_meta: undefined},
110+
{title_meta: ''},
111+
{title_meta: 'title'},
112+
],
107113
invalidFrontMatters: [
108114
[{title_meta: null}, 'must be a string'],
109115
[{title_meta: false}, 'must be a string'],
110116
],
111117
});
112118
});
113119

120+
describe('validateBlogPostFrontMatter sidebar_label', () => {
121+
testField({
122+
prefix: 'title_meta',
123+
validFrontMatters: [
124+
{sidebar_label: undefined},
125+
{sidebar_label: ''},
126+
{sidebar_label: 'title'},
127+
],
128+
invalidFrontMatters: [
129+
[{sidebar_label: null}, 'must be a string'],
130+
[{sidebar_label: false}, 'must be a string'],
131+
],
132+
});
133+
});
134+
114135
describe('validateBlogPostFrontMatter id', () => {
115136
testField({
116137
prefix: 'id',

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ describe('toBlogSidebarProp', () => {
9494
permalink: '/blog/blog-2',
9595
unlisted: true,
9696
date: '2024-01-01',
97-
frontMatter: {hello: 'world'},
97+
frontMatter: {hello: 'world', sidebar_label: 'title 2 (custom)'},
9898
tags: [{label: 'tag1', permalink: '/tag1', inline: false}],
9999
},
100100
}),
@@ -117,7 +117,7 @@ describe('toBlogSidebarProp', () => {
117117
{
118118
"date": "2024-01-01",
119119
"permalink": "/blog/blog-2",
120-
"title": "title 2",
120+
"title": "title 2 (custom)",
121121
"unlisted": true,
122122
},
123123
],

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ const FrontMatterAuthorErrorMessage =
3333
const BlogFrontMatterSchema = Joi.object<BlogPostFrontMatter>({
3434
id: Joi.string(),
3535
title: Joi.string().allow(''),
36-
title_meta: Joi.string(),
36+
title_meta: Joi.string().allow(''),
37+
sidebar_label: Joi.string().allow(''),
3738
description: Joi.string().allow(''),
3839
tags: FrontMatterTagsSchema,
3940
date: Joi.date().raw(),

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,17 @@ declare module '@docusaurus/plugin-content-blog' {
145145
title?: string;
146146
/**
147147
* Will be used for SEO page metadata and override BlogPostMetadata.title.
148-
* @see {@link BlogPostMetadata.title_meta}
149148
*/
150149
title_meta?: string;
151150
/**
152151
* Will override the default excerpt.
153152
* @see {@link BlogPostMetadata.description}
154153
*/
155154
description?: string;
155+
/**
156+
* Will override the default excerpt.
157+
*/
158+
sidebar_label?: string;
156159
/**
157160
* Front matter tags, unnormalized.
158161
* @see {@link BlogPostMetadata.tags}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ export function toBlogSidebarProp({
6565
return {
6666
title: blogSidebarTitle,
6767
items: blogPosts.map((blogPost) => ({
68-
title: blogPost.metadata.title,
68+
title:
69+
blogPost.metadata.frontMatter.sidebar_label ?? blogPost.metadata.title,
6970
permalink: blogPost.metadata.permalink,
7071
unlisted: blogPost.metadata.unlisted,
7172
date: blogPost.metadata.date,

website/_dogfooding/_blog tests/2023-08-05.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: Ensure heading anchor slugs respect GitHub emoji behavior
33
date: 2023-08-05
4+
sidebar_label: 'Ensure heading... (custom label)'
45
---
56

67
## :smiley: This is a friendly header

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ Accepted fields:
250250
| `author_image_url` | `string` | `undefined` | ⚠️ Prefer using `authors`. The URL to the author's thumbnail image. |
251251
| `author_title` | `string` | `undefined` | ⚠️ Prefer using `authors`. A description of the author. |
252252
| `title` | `string` | Markdown title | The blog post title. |
253+
| `sidebar_label` | `string` | `title` | A custom label for the blog sidebar, replacing the default one (`title`). |
253254
| `date` | `string` | File name or file creation time | The blog post creation date. If not specified, this can be extracted from the file or folder name, e.g, `2021-04-15-blog-post.mdx`, `2021-04-15-blog-post/index.mdx`, `2021/04/15/blog-post.mdx`. Otherwise, it is the Markdown file creation time. |
254255
| `tags` | `Tag[]` | `undefined` | A list of strings or objects of two string fields `label` and `permalink` to tag to your post. Strings can be a reference to keys of a [tags file](#tags-file) (usually `tags.yml`) |
255256
| `draft` | `boolean` | `false` | Draft blog posts will only be available during development. |

0 commit comments

Comments
 (0)