Skip to content

Commit e553ce7

Browse files
Djunnnislorber
andauthored
fix(docusaurus-utils-validation): baseUrl + routeBasePath: allow empty string, normalized as "/" (#8258)
Co-authored-by: sebastienlorber <lorber.sebastien@gmail.com> fix #8254
1 parent 85d0b56 commit e553ce7

12 files changed

Lines changed: 92 additions & 17 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('validateOptions', () => {
4040
...defaultOptions,
4141
feedOptions: {type: 'rss' as const, title: 'myTitle'},
4242
path: 'not_blog',
43-
routeBasePath: 'myBlog',
43+
routeBasePath: '/myBlog',
4444
postsPerPage: 5,
4545
include: ['api/*', 'docs/*'],
4646
};
@@ -53,7 +53,7 @@ describe('validateOptions', () => {
5353
it('accepts valid user options', () => {
5454
const userOptions: Options = {
5555
...defaultOptions,
56-
routeBasePath: 'myBlog',
56+
routeBasePath: '/myBlog',
5757
beforeDefaultRemarkPlugins: [],
5858
beforeDefaultRehypePlugins: [markdownPluginsFunctionStub],
5959
remarkPlugins: [[markdownPluginsFunctionStub, {option1: '42'}]],

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
RemarkPluginsSchema,
1111
RehypePluginsSchema,
1212
AdmonitionsSchema,
13+
RouteBasePathSchema,
1314
URISchema,
1415
} from '@docusaurus/utils-validation';
1516
import {GlobExcludeDefault} from '@docusaurus/utils';
@@ -56,10 +57,7 @@ const PluginOptionSchema = Joi.object<PluginOptions>({
5657
archiveBasePath: Joi.string()
5758
.default(DEFAULT_OPTIONS.archiveBasePath)
5859
.allow(null),
59-
routeBasePath: Joi.string()
60-
// '' not allowed, see https://github.com/facebook/docusaurus/issues/3374
61-
// .allow('')
62-
.default(DEFAULT_OPTIONS.routeBasePath),
60+
routeBasePath: RouteBasePathSchema.default(DEFAULT_OPTIONS.routeBasePath),
6361
tagsBasePath: Joi.string().default(DEFAULT_OPTIONS.tagsBasePath),
6462
include: Joi.array().items(Joi.string()).default(DEFAULT_OPTIONS.include),
6563
exclude: Joi.array().items(Joi.string()).default(DEFAULT_OPTIONS.exclude),

packages/docusaurus-plugin-content-docs/src/__tests__/options.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('normalizeDocsPluginOptions', () => {
4242
it('accepts correctly defined user options', () => {
4343
const userOptions: Options = {
4444
path: 'my-docs', // Path to data on filesystem, relative to site dir.
45-
routeBasePath: 'my-docs', // URL Route.
45+
routeBasePath: '/my-docs', // URL Route.
4646
tagsBasePath: 'tags', // URL Tags Route.
4747
include: ['**/*.{md,mdx}'], // Extensions to include.
4848
exclude: GlobExcludeDefault,

packages/docusaurus-plugin-content-docs/src/options.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
RemarkPluginsSchema,
1212
RehypePluginsSchema,
1313
AdmonitionsSchema,
14+
RouteBasePathSchema,
1415
URISchema,
1516
} from '@docusaurus/utils-validation';
1617
import {GlobExcludeDefault} from '@docusaurus/utils';
@@ -73,10 +74,7 @@ const OptionsSchema = Joi.object<PluginOptions>({
7374
editUrl: Joi.alternatives().try(URISchema, Joi.function()),
7475
editCurrentVersion: Joi.boolean().default(DEFAULT_OPTIONS.editCurrentVersion),
7576
editLocalizedFiles: Joi.boolean().default(DEFAULT_OPTIONS.editLocalizedFiles),
76-
routeBasePath: Joi.string()
77-
// '' not allowed, see https://github.com/facebook/docusaurus/issues/3374
78-
// .allow('') ""
79-
.default(DEFAULT_OPTIONS.routeBasePath),
77+
routeBasePath: RouteBasePathSchema.default(DEFAULT_OPTIONS.routeBasePath),
8078
tagsBasePath: Joi.string().default(DEFAULT_OPTIONS.tagsBasePath),
8179
// @ts-expect-error: deprecated
8280
homePageId: Joi.any().forbidden().messages({

packages/docusaurus-plugin-content-pages/src/__tests__/options.test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('normalizePagesPluginOptions', () => {
3333
it('accepts correctly defined user options', () => {
3434
const userOptions = {
3535
path: 'src/my-pages',
36-
routeBasePath: 'my-pages',
36+
routeBasePath: '/my-pages',
3737
include: ['**/*.{js,jsx,ts,tsx}'],
3838
exclude: ['**/$*/'],
3939
};
@@ -51,4 +51,15 @@ describe('normalizePagesPluginOptions', () => {
5151
});
5252
}).toThrowErrorMatchingInlineSnapshot(`""path" must be a string"`);
5353
});
54+
55+
it('empty routeBasePath replace default path("/")', () => {
56+
expect(
57+
testValidate({
58+
routeBasePath: '',
59+
}),
60+
).toEqual({
61+
...defaultOptions,
62+
routeBasePath: '/',
63+
});
64+
});
5465
});

packages/docusaurus-plugin-content-pages/src/options.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
RemarkPluginsSchema,
1111
RehypePluginsSchema,
1212
AdmonitionsSchema,
13+
RouteBasePathSchema,
1314
} from '@docusaurus/utils-validation';
1415
import {GlobExcludeDefault} from '@docusaurus/utils';
1516
import type {OptionValidationContext} from '@docusaurus/types';
@@ -30,7 +31,7 @@ export const DEFAULT_OPTIONS: PluginOptions = {
3031

3132
const PluginOptionSchema = Joi.object<PluginOptions>({
3233
path: Joi.string().default(DEFAULT_OPTIONS.path),
33-
routeBasePath: Joi.string().default(DEFAULT_OPTIONS.routeBasePath),
34+
routeBasePath: RouteBasePathSchema.default(DEFAULT_OPTIONS.routeBasePath),
3435
include: Joi.array().items(Joi.string()).default(DEFAULT_OPTIONS.include),
3536
exclude: Joi.array().items(Joi.string()).default(DEFAULT_OPTIONS.exclude),
3637
mdxPageComponent: Joi.string().default(DEFAULT_OPTIONS.mdxPageComponent),

packages/docusaurus-utils-validation/src/__tests__/__snapshots__/validationSchemas.test.ts.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,12 @@ exports[`validation schemas remarkPluginsSchema: for value=false 1`] = `""value"
130130
131131
exports[`validation schemas remarkPluginsSchema: for value=null 1`] = `""value" must be an array"`;
132132
133+
exports[`validation schemas routeBasePathSchema: for value=[] 1`] = `""value" must be a string"`;
134+
135+
exports[`validation schemas routeBasePathSchema: for value={} 1`] = `""value" must be a string"`;
136+
137+
exports[`validation schemas routeBasePathSchema: for value=3 1`] = `""value" must be a string"`;
138+
139+
exports[`validation schemas routeBasePathSchema: for value=null 1`] = `""value" must be a string"`;
140+
133141
exports[`validation schemas uRISchema: for value="spaces are invalid in a URL" 1`] = `""value" does not look like a valid url (value='')"`;

packages/docusaurus-utils-validation/src/__tests__/validationSchemas.test.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
PluginIdSchema,
1515
URISchema,
1616
PathnameSchema,
17+
RouteBasePathSchema,
1718
ContentVisibilitySchema,
1819
} from '../validationSchemas';
1920

@@ -24,8 +25,9 @@ function createTestHelpers({
2425
schema: Joi.Schema;
2526
defaultValue?: unknown;
2627
}) {
27-
function testOK(value: unknown) {
28-
expect(Joi.attempt(value, schema)).toEqual(value ?? defaultValue);
28+
function testOK(value: unknown, options?: {normalizedValue?: unknown}) {
29+
const expectedValue = options?.normalizedValue ?? value ?? defaultValue;
30+
expect(Joi.attempt(value, schema)).toEqual(expectedValue);
2931
}
3032

3133
function testFail(value: unknown) {
@@ -168,6 +170,29 @@ describe('validation schemas', () => {
168170
testFail('https://github.com/foo');
169171
});
170172

173+
it('routeBasePathSchema', () => {
174+
const {testFail, testOK} = createTestHelpers({
175+
schema: RouteBasePathSchema,
176+
defaultValue: undefined,
177+
});
178+
179+
testOK('', {normalizedValue: '/'});
180+
testOK('/');
181+
testOK('/foo', {normalizedValue: '/foo'});
182+
testOK('foo', {normalizedValue: '/foo'});
183+
testOK('blog', {normalizedValue: '/blog'});
184+
testOK('blog/', {normalizedValue: '/blog/'});
185+
testOK('prefix/blog', {normalizedValue: '/prefix/blog'});
186+
testOK('prefix/blog/', {normalizedValue: '/prefix/blog/'});
187+
testOK('/prefix/blog', {normalizedValue: '/prefix/blog'});
188+
testOK(undefined);
189+
190+
testFail(3);
191+
testFail([]);
192+
testFail(null);
193+
testFail({});
194+
});
195+
171196
it('contentVisibilitySchema', () => {
172197
const {testFail, testOK} = createTestHelpers({
173198
schema: ContentVisibilitySchema,

packages/docusaurus-utils-validation/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export {
2020
RemarkPluginsSchema,
2121
RehypePluginsSchema,
2222
AdmonitionsSchema,
23+
RouteBasePathSchema,
2324
URISchema,
2425
PathnameSchema,
2526
FrontMatterTagsSchema,

packages/docusaurus-utils-validation/src/validationSchemas.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import {isValidPathname, DEFAULT_PLUGIN_ID, type Tag} from '@docusaurus/utils';
8+
import {
9+
isValidPathname,
10+
DEFAULT_PLUGIN_ID,
11+
type Tag,
12+
addLeadingSlash,
13+
} from '@docusaurus/utils';
914
import Joi from './Joi';
1015
import {JoiFrontMatter} from './JoiFrontMatter';
1116

@@ -96,6 +101,26 @@ export const PathnameSchema = Joi.string()
96101
'{{#label}} is not a valid pathname. Pathname should start with slash and not contain any domain or query string.',
97102
);
98103

104+
// Normalized schema for url path segments: baseUrl + routeBasePath...
105+
// Note we only add a leading slash
106+
// we don't always want to enforce a trailing slash on urls such as /docs
107+
//
108+
// Examples:
109+
// '' => '/'
110+
// 'docs' => '/docs'
111+
// '/docs' => '/docs'
112+
// 'docs/' => '/docs'
113+
// 'prefix/docs' => '/prefix/docs'
114+
// TODO tighter validation: not all strings are valid path segments
115+
export const RouteBasePathSchema = Joi
116+
// Weird Joi trick needed, otherwise value '' is not normalized...
117+
.alternatives()
118+
.try(Joi.string().required().allow(''))
119+
.custom((value: string) =>
120+
// /!\ do not add trailing slash here
121+
addLeadingSlash(value),
122+
);
123+
99124
const FrontMatterTagSchema = JoiFrontMatter.alternatives()
100125
.try(
101126
JoiFrontMatter.string().required(),

0 commit comments

Comments
 (0)