mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-10 07:37:19 +02:00
fix(docusaurus-utils-validation): baseUrl + routeBasePath: allow empty string, normalized as "/" (#8258)
Co-authored-by: sebastienlorber <lorber.sebastien@gmail.com> fix https://github.com/facebook/docusaurus/issues/8254
This commit is contained in:
parent
85d0b5674f
commit
e553ce7e59
12 changed files with 92 additions and 17 deletions
|
@ -40,7 +40,7 @@ describe('validateOptions', () => {
|
||||||
...defaultOptions,
|
...defaultOptions,
|
||||||
feedOptions: {type: 'rss' as const, title: 'myTitle'},
|
feedOptions: {type: 'rss' as const, title: 'myTitle'},
|
||||||
path: 'not_blog',
|
path: 'not_blog',
|
||||||
routeBasePath: 'myBlog',
|
routeBasePath: '/myBlog',
|
||||||
postsPerPage: 5,
|
postsPerPage: 5,
|
||||||
include: ['api/*', 'docs/*'],
|
include: ['api/*', 'docs/*'],
|
||||||
};
|
};
|
||||||
|
@ -53,7 +53,7 @@ describe('validateOptions', () => {
|
||||||
it('accepts valid user options', () => {
|
it('accepts valid user options', () => {
|
||||||
const userOptions: Options = {
|
const userOptions: Options = {
|
||||||
...defaultOptions,
|
...defaultOptions,
|
||||||
routeBasePath: 'myBlog',
|
routeBasePath: '/myBlog',
|
||||||
beforeDefaultRemarkPlugins: [],
|
beforeDefaultRemarkPlugins: [],
|
||||||
beforeDefaultRehypePlugins: [markdownPluginsFunctionStub],
|
beforeDefaultRehypePlugins: [markdownPluginsFunctionStub],
|
||||||
remarkPlugins: [[markdownPluginsFunctionStub, {option1: '42'}]],
|
remarkPlugins: [[markdownPluginsFunctionStub, {option1: '42'}]],
|
||||||
|
|
|
@ -10,6 +10,7 @@ import {
|
||||||
RemarkPluginsSchema,
|
RemarkPluginsSchema,
|
||||||
RehypePluginsSchema,
|
RehypePluginsSchema,
|
||||||
AdmonitionsSchema,
|
AdmonitionsSchema,
|
||||||
|
RouteBasePathSchema,
|
||||||
URISchema,
|
URISchema,
|
||||||
} from '@docusaurus/utils-validation';
|
} from '@docusaurus/utils-validation';
|
||||||
import {GlobExcludeDefault} from '@docusaurus/utils';
|
import {GlobExcludeDefault} from '@docusaurus/utils';
|
||||||
|
@ -56,10 +57,7 @@ const PluginOptionSchema = Joi.object<PluginOptions>({
|
||||||
archiveBasePath: Joi.string()
|
archiveBasePath: Joi.string()
|
||||||
.default(DEFAULT_OPTIONS.archiveBasePath)
|
.default(DEFAULT_OPTIONS.archiveBasePath)
|
||||||
.allow(null),
|
.allow(null),
|
||||||
routeBasePath: Joi.string()
|
routeBasePath: RouteBasePathSchema.default(DEFAULT_OPTIONS.routeBasePath),
|
||||||
// '' not allowed, see https://github.com/facebook/docusaurus/issues/3374
|
|
||||||
// .allow('')
|
|
||||||
.default(DEFAULT_OPTIONS.routeBasePath),
|
|
||||||
tagsBasePath: Joi.string().default(DEFAULT_OPTIONS.tagsBasePath),
|
tagsBasePath: Joi.string().default(DEFAULT_OPTIONS.tagsBasePath),
|
||||||
include: Joi.array().items(Joi.string()).default(DEFAULT_OPTIONS.include),
|
include: Joi.array().items(Joi.string()).default(DEFAULT_OPTIONS.include),
|
||||||
exclude: Joi.array().items(Joi.string()).default(DEFAULT_OPTIONS.exclude),
|
exclude: Joi.array().items(Joi.string()).default(DEFAULT_OPTIONS.exclude),
|
||||||
|
|
|
@ -42,7 +42,7 @@ describe('normalizeDocsPluginOptions', () => {
|
||||||
it('accepts correctly defined user options', () => {
|
it('accepts correctly defined user options', () => {
|
||||||
const userOptions: Options = {
|
const userOptions: Options = {
|
||||||
path: 'my-docs', // Path to data on filesystem, relative to site dir.
|
path: 'my-docs', // Path to data on filesystem, relative to site dir.
|
||||||
routeBasePath: 'my-docs', // URL Route.
|
routeBasePath: '/my-docs', // URL Route.
|
||||||
tagsBasePath: 'tags', // URL Tags Route.
|
tagsBasePath: 'tags', // URL Tags Route.
|
||||||
include: ['**/*.{md,mdx}'], // Extensions to include.
|
include: ['**/*.{md,mdx}'], // Extensions to include.
|
||||||
exclude: GlobExcludeDefault,
|
exclude: GlobExcludeDefault,
|
||||||
|
|
|
@ -11,6 +11,7 @@ import {
|
||||||
RemarkPluginsSchema,
|
RemarkPluginsSchema,
|
||||||
RehypePluginsSchema,
|
RehypePluginsSchema,
|
||||||
AdmonitionsSchema,
|
AdmonitionsSchema,
|
||||||
|
RouteBasePathSchema,
|
||||||
URISchema,
|
URISchema,
|
||||||
} from '@docusaurus/utils-validation';
|
} from '@docusaurus/utils-validation';
|
||||||
import {GlobExcludeDefault} from '@docusaurus/utils';
|
import {GlobExcludeDefault} from '@docusaurus/utils';
|
||||||
|
@ -73,10 +74,7 @@ const OptionsSchema = Joi.object<PluginOptions>({
|
||||||
editUrl: Joi.alternatives().try(URISchema, Joi.function()),
|
editUrl: Joi.alternatives().try(URISchema, Joi.function()),
|
||||||
editCurrentVersion: Joi.boolean().default(DEFAULT_OPTIONS.editCurrentVersion),
|
editCurrentVersion: Joi.boolean().default(DEFAULT_OPTIONS.editCurrentVersion),
|
||||||
editLocalizedFiles: Joi.boolean().default(DEFAULT_OPTIONS.editLocalizedFiles),
|
editLocalizedFiles: Joi.boolean().default(DEFAULT_OPTIONS.editLocalizedFiles),
|
||||||
routeBasePath: Joi.string()
|
routeBasePath: RouteBasePathSchema.default(DEFAULT_OPTIONS.routeBasePath),
|
||||||
// '' not allowed, see https://github.com/facebook/docusaurus/issues/3374
|
|
||||||
// .allow('') ""
|
|
||||||
.default(DEFAULT_OPTIONS.routeBasePath),
|
|
||||||
tagsBasePath: Joi.string().default(DEFAULT_OPTIONS.tagsBasePath),
|
tagsBasePath: Joi.string().default(DEFAULT_OPTIONS.tagsBasePath),
|
||||||
// @ts-expect-error: deprecated
|
// @ts-expect-error: deprecated
|
||||||
homePageId: Joi.any().forbidden().messages({
|
homePageId: Joi.any().forbidden().messages({
|
||||||
|
|
|
@ -33,7 +33,7 @@ describe('normalizePagesPluginOptions', () => {
|
||||||
it('accepts correctly defined user options', () => {
|
it('accepts correctly defined user options', () => {
|
||||||
const userOptions = {
|
const userOptions = {
|
||||||
path: 'src/my-pages',
|
path: 'src/my-pages',
|
||||||
routeBasePath: 'my-pages',
|
routeBasePath: '/my-pages',
|
||||||
include: ['**/*.{js,jsx,ts,tsx}'],
|
include: ['**/*.{js,jsx,ts,tsx}'],
|
||||||
exclude: ['**/$*/'],
|
exclude: ['**/$*/'],
|
||||||
};
|
};
|
||||||
|
@ -51,4 +51,15 @@ describe('normalizePagesPluginOptions', () => {
|
||||||
});
|
});
|
||||||
}).toThrowErrorMatchingInlineSnapshot(`""path" must be a string"`);
|
}).toThrowErrorMatchingInlineSnapshot(`""path" must be a string"`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('empty routeBasePath replace default path("/")', () => {
|
||||||
|
expect(
|
||||||
|
testValidate({
|
||||||
|
routeBasePath: '',
|
||||||
|
}),
|
||||||
|
).toEqual({
|
||||||
|
...defaultOptions,
|
||||||
|
routeBasePath: '/',
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -10,6 +10,7 @@ import {
|
||||||
RemarkPluginsSchema,
|
RemarkPluginsSchema,
|
||||||
RehypePluginsSchema,
|
RehypePluginsSchema,
|
||||||
AdmonitionsSchema,
|
AdmonitionsSchema,
|
||||||
|
RouteBasePathSchema,
|
||||||
} from '@docusaurus/utils-validation';
|
} from '@docusaurus/utils-validation';
|
||||||
import {GlobExcludeDefault} from '@docusaurus/utils';
|
import {GlobExcludeDefault} from '@docusaurus/utils';
|
||||||
import type {OptionValidationContext} from '@docusaurus/types';
|
import type {OptionValidationContext} from '@docusaurus/types';
|
||||||
|
@ -30,7 +31,7 @@ export const DEFAULT_OPTIONS: PluginOptions = {
|
||||||
|
|
||||||
const PluginOptionSchema = Joi.object<PluginOptions>({
|
const PluginOptionSchema = Joi.object<PluginOptions>({
|
||||||
path: Joi.string().default(DEFAULT_OPTIONS.path),
|
path: Joi.string().default(DEFAULT_OPTIONS.path),
|
||||||
routeBasePath: Joi.string().default(DEFAULT_OPTIONS.routeBasePath),
|
routeBasePath: RouteBasePathSchema.default(DEFAULT_OPTIONS.routeBasePath),
|
||||||
include: Joi.array().items(Joi.string()).default(DEFAULT_OPTIONS.include),
|
include: Joi.array().items(Joi.string()).default(DEFAULT_OPTIONS.include),
|
||||||
exclude: Joi.array().items(Joi.string()).default(DEFAULT_OPTIONS.exclude),
|
exclude: Joi.array().items(Joi.string()).default(DEFAULT_OPTIONS.exclude),
|
||||||
mdxPageComponent: Joi.string().default(DEFAULT_OPTIONS.mdxPageComponent),
|
mdxPageComponent: Joi.string().default(DEFAULT_OPTIONS.mdxPageComponent),
|
||||||
|
|
|
@ -130,4 +130,12 @@ exports[`validation schemas remarkPluginsSchema: for value=false 1`] = `""value"
|
||||||
|
|
||||||
exports[`validation schemas remarkPluginsSchema: for value=null 1`] = `""value" must be an array"`;
|
exports[`validation schemas remarkPluginsSchema: for value=null 1`] = `""value" must be an array"`;
|
||||||
|
|
||||||
|
exports[`validation schemas routeBasePathSchema: for value=[] 1`] = `""value" must be a string"`;
|
||||||
|
|
||||||
|
exports[`validation schemas routeBasePathSchema: for value={} 1`] = `""value" must be a string"`;
|
||||||
|
|
||||||
|
exports[`validation schemas routeBasePathSchema: for value=3 1`] = `""value" must be a string"`;
|
||||||
|
|
||||||
|
exports[`validation schemas routeBasePathSchema: for value=null 1`] = `""value" must be a string"`;
|
||||||
|
|
||||||
exports[`validation schemas uRISchema: for value="spaces are invalid in a URL" 1`] = `""value" does not look like a valid url (value='')"`;
|
exports[`validation schemas uRISchema: for value="spaces are invalid in a URL" 1`] = `""value" does not look like a valid url (value='')"`;
|
||||||
|
|
|
@ -14,6 +14,7 @@ import {
|
||||||
PluginIdSchema,
|
PluginIdSchema,
|
||||||
URISchema,
|
URISchema,
|
||||||
PathnameSchema,
|
PathnameSchema,
|
||||||
|
RouteBasePathSchema,
|
||||||
ContentVisibilitySchema,
|
ContentVisibilitySchema,
|
||||||
} from '../validationSchemas';
|
} from '../validationSchemas';
|
||||||
|
|
||||||
|
@ -24,8 +25,9 @@ function createTestHelpers({
|
||||||
schema: Joi.Schema;
|
schema: Joi.Schema;
|
||||||
defaultValue?: unknown;
|
defaultValue?: unknown;
|
||||||
}) {
|
}) {
|
||||||
function testOK(value: unknown) {
|
function testOK(value: unknown, options?: {normalizedValue?: unknown}) {
|
||||||
expect(Joi.attempt(value, schema)).toEqual(value ?? defaultValue);
|
const expectedValue = options?.normalizedValue ?? value ?? defaultValue;
|
||||||
|
expect(Joi.attempt(value, schema)).toEqual(expectedValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testFail(value: unknown) {
|
function testFail(value: unknown) {
|
||||||
|
@ -168,6 +170,29 @@ describe('validation schemas', () => {
|
||||||
testFail('https://github.com/foo');
|
testFail('https://github.com/foo');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('routeBasePathSchema', () => {
|
||||||
|
const {testFail, testOK} = createTestHelpers({
|
||||||
|
schema: RouteBasePathSchema,
|
||||||
|
defaultValue: undefined,
|
||||||
|
});
|
||||||
|
|
||||||
|
testOK('', {normalizedValue: '/'});
|
||||||
|
testOK('/');
|
||||||
|
testOK('/foo', {normalizedValue: '/foo'});
|
||||||
|
testOK('foo', {normalizedValue: '/foo'});
|
||||||
|
testOK('blog', {normalizedValue: '/blog'});
|
||||||
|
testOK('blog/', {normalizedValue: '/blog/'});
|
||||||
|
testOK('prefix/blog', {normalizedValue: '/prefix/blog'});
|
||||||
|
testOK('prefix/blog/', {normalizedValue: '/prefix/blog/'});
|
||||||
|
testOK('/prefix/blog', {normalizedValue: '/prefix/blog'});
|
||||||
|
testOK(undefined);
|
||||||
|
|
||||||
|
testFail(3);
|
||||||
|
testFail([]);
|
||||||
|
testFail(null);
|
||||||
|
testFail({});
|
||||||
|
});
|
||||||
|
|
||||||
it('contentVisibilitySchema', () => {
|
it('contentVisibilitySchema', () => {
|
||||||
const {testFail, testOK} = createTestHelpers({
|
const {testFail, testOK} = createTestHelpers({
|
||||||
schema: ContentVisibilitySchema,
|
schema: ContentVisibilitySchema,
|
||||||
|
|
|
@ -20,6 +20,7 @@ export {
|
||||||
RemarkPluginsSchema,
|
RemarkPluginsSchema,
|
||||||
RehypePluginsSchema,
|
RehypePluginsSchema,
|
||||||
AdmonitionsSchema,
|
AdmonitionsSchema,
|
||||||
|
RouteBasePathSchema,
|
||||||
URISchema,
|
URISchema,
|
||||||
PathnameSchema,
|
PathnameSchema,
|
||||||
FrontMatterTagsSchema,
|
FrontMatterTagsSchema,
|
||||||
|
|
|
@ -5,7 +5,12 @@
|
||||||
* LICENSE file in the root directory of this source tree.
|
* LICENSE file in the root directory of this source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {isValidPathname, DEFAULT_PLUGIN_ID, type Tag} from '@docusaurus/utils';
|
import {
|
||||||
|
isValidPathname,
|
||||||
|
DEFAULT_PLUGIN_ID,
|
||||||
|
type Tag,
|
||||||
|
addLeadingSlash,
|
||||||
|
} from '@docusaurus/utils';
|
||||||
import Joi from './Joi';
|
import Joi from './Joi';
|
||||||
import {JoiFrontMatter} from './JoiFrontMatter';
|
import {JoiFrontMatter} from './JoiFrontMatter';
|
||||||
|
|
||||||
|
@ -96,6 +101,26 @@ export const PathnameSchema = Joi.string()
|
||||||
'{{#label}} is not a valid pathname. Pathname should start with slash and not contain any domain or query string.',
|
'{{#label}} is not a valid pathname. Pathname should start with slash and not contain any domain or query string.',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Normalized schema for url path segments: baseUrl + routeBasePath...
|
||||||
|
// Note we only add a leading slash
|
||||||
|
// we don't always want to enforce a trailing slash on urls such as /docs
|
||||||
|
//
|
||||||
|
// Examples:
|
||||||
|
// '' => '/'
|
||||||
|
// 'docs' => '/docs'
|
||||||
|
// '/docs' => '/docs'
|
||||||
|
// 'docs/' => '/docs'
|
||||||
|
// 'prefix/docs' => '/prefix/docs'
|
||||||
|
// TODO tighter validation: not all strings are valid path segments
|
||||||
|
export const RouteBasePathSchema = Joi
|
||||||
|
// Weird Joi trick needed, otherwise value '' is not normalized...
|
||||||
|
.alternatives()
|
||||||
|
.try(Joi.string().required().allow(''))
|
||||||
|
.custom((value: string) =>
|
||||||
|
// /!\ do not add trailing slash here
|
||||||
|
addLeadingSlash(value),
|
||||||
|
);
|
||||||
|
|
||||||
const FrontMatterTagSchema = JoiFrontMatter.alternatives()
|
const FrontMatterTagSchema = JoiFrontMatter.alternatives()
|
||||||
.try(
|
.try(
|
||||||
JoiFrontMatter.string().required(),
|
JoiFrontMatter.string().required(),
|
||||||
|
|
|
@ -134,6 +134,11 @@ describe('normalizeConfig', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('normalizes various base URLs', () => {
|
it('normalizes various base URLs', () => {
|
||||||
|
expect(
|
||||||
|
normalizeConfig({
|
||||||
|
baseUrl: '',
|
||||||
|
}).baseUrl,
|
||||||
|
).toBe('/');
|
||||||
expect(
|
expect(
|
||||||
normalizeConfig({
|
normalizeConfig({
|
||||||
baseUrl: 'noSlash',
|
baseUrl: 'noSlash',
|
||||||
|
|
|
@ -180,7 +180,10 @@ const SiteUrlSchema = Joi.string()
|
||||||
|
|
||||||
// TODO move to @docusaurus/utils-validation
|
// TODO move to @docusaurus/utils-validation
|
||||||
export const ConfigSchema = Joi.object<DocusaurusConfig>({
|
export const ConfigSchema = Joi.object<DocusaurusConfig>({
|
||||||
baseUrl: Joi.string()
|
baseUrl: Joi
|
||||||
|
// Weird Joi trick needed, otherwise value '' is not normalized...
|
||||||
|
.alternatives()
|
||||||
|
.try(Joi.string().required().allow(''))
|
||||||
.required()
|
.required()
|
||||||
.custom((value: string) => addLeadingSlash(addTrailingSlash(value))),
|
.custom((value: string) => addLeadingSlash(addTrailingSlash(value))),
|
||||||
baseUrlIssueBanner: Joi.boolean().default(DEFAULT_CONFIG.baseUrlIssueBanner),
|
baseUrlIssueBanner: Joi.boolean().default(DEFAULT_CONFIG.baseUrlIssueBanner),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue