Skip to content

Commit

Permalink
fix(v2): allow negative sidebar positions
Browse files Browse the repository at this point in the history
In some cases, negative sidebar positions can be useful for reversing
the sorting order with minimal maintenance overhead. For example, a docs
folder with changelogs for historical versions should be sorted in
reverse chronological order. This is easy to do for semantic version
numbers by converting them into a negative numerical representation,
e.g. 11.5.1 -> -110501.

The alternative is to make the first version start with a large position
number (e.g. 9999) and decrement it for each version. However, this
requires referring to older versions to get the current sequence number,
thus increasing maintenance overhead. It also makes the number less
intuitive and more prone to error.

Negative sidebar positions work great for this purpose, so make the
front matter validator allow them again as #4796 broke this use case.
  • Loading branch information
kdrag0n committed Jun 28, 2021
1 parent 3fe7389 commit 1ee35b9
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,17 +196,17 @@ describe('validateDocFrontMatter sidebar_position', () => {
testField({
fieldName: 'sidebar_position',
validFrontMatters: [
{sidebar_position: -5},
{sidebar_position: -3.5},
{sidebar_position: 0},
{sidebar_position: 5},
{sidebar_position: 3.5},
],
convertibleFrontMatter: [
[{sidebar_position: '-1.5'}, {sidebar_position: -1.5}],
[{sidebar_position: '1'}, {sidebar_position: 1}],
[{sidebar_position: '1.5'}, {sidebar_position: 1.5}],
],
invalidFrontMatters: [
[{sidebar_position: -1}, 'must be greater than or equal to 0'],
],
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const DocFrontMatterSchema = Joi.object<DocFrontMatter>({
description: Joi.string().allow(''), // see https://github.com/facebook/docusaurus/issues/4591#issuecomment-822372398
slug: Joi.string(),
sidebar_label: Joi.string(),
sidebar_position: Joi.number().min(0),
sidebar_position: Joi.number(),
pagination_label: Joi.string(),
custom_edit_url: URISchema.allow('', null),
parse_number_prefixes: Joi.boolean(),
Expand Down

0 comments on commit 1ee35b9

Please sign in to comment.