From dd3f3f109327e99380cf1776505c61f02b7ca3a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lorber?= Date: Tue, 11 Aug 2020 17:20:51 +0200 Subject: [PATCH] fix(v2): doc path special char (space or other) should lead to a valid slug (#3262) * doc path special char (space or other) should lead to a valid slug * doc path special char (space or other) should lead to a valid slug * improve doc bad slug message --- .../src/__tests__/slug.test.ts | 7 +++++++ packages/docusaurus-plugin-content-docs/src/slug.ts | 11 ++++++++++- packages/docusaurus-utils/src/__tests__/index.test.ts | 3 ++- packages/docusaurus-utils/src/index.ts | 3 ++- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/slug.test.ts b/packages/docusaurus-plugin-content-docs/src/__tests__/slug.test.ts index 0fc5716382..9e31cf3ad5 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/slug.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/slug.test.ts @@ -15,6 +15,13 @@ describe('getSlug', () => { ); }); + // See https://github.com/facebook/docusaurus/issues/3223 + test('should handle special chars in doc path', () => { + expect( + getSlug({baseID: 'my dôc', dirName: '/dir with spâce/hey $hello'}), + ).toEqual('/dir with spâce/hey $hello/my dôc'); + }); + test('should handle current dir', () => { expect(getSlug({baseID: 'doc', dirName: '.'})).toEqual('/doc'); expect(getSlug({baseID: 'doc', dirName: '/'})).toEqual('/doc'); diff --git a/packages/docusaurus-plugin-content-docs/src/slug.ts b/packages/docusaurus-plugin-content-docs/src/slug.ts index 72b9f09d5b..465d0d5343 100644 --- a/packages/docusaurus-plugin-content-docs/src/slug.ts +++ b/packages/docusaurus-plugin-content-docs/src/slug.ts @@ -33,7 +33,16 @@ export default function getSlug({ if (!isValidPathname(slug)) { throw new Error( - `Unable to resolve valid document slug. Maybe your slug frontmatter is incorrect? Doc id=${baseID} / dirName=${dirName} / frontmatterSlug=${frontmatterSlug} => bad result slug=${slug}`, + `We couldn't compute a valid slug for document with id=${baseID} in folder=${dirName} +The slug we computed looks invalid: ${slug} +Maybe your slug frontmatter is incorrect or you use weird chars in the file path? +By using the slug frontmatter, you should be able to fix this error, by using the slug of your choice: + +Example => +--- +slug: /my/customDocPath +--- +`, ); } diff --git a/packages/docusaurus-utils/src/__tests__/index.test.ts b/packages/docusaurus-utils/src/__tests__/index.test.ts index 5d99871d6a..1861b72d97 100644 --- a/packages/docusaurus-utils/src/__tests__/index.test.ts +++ b/packages/docusaurus-utils/src/__tests__/index.test.ts @@ -394,10 +394,11 @@ describe('load utils', () => { expect(isValidPathname('/hey/ho/')).toBe(true); expect(isValidPathname('/hey/h%C3%B4/')).toBe(true); expect(isValidPathname('/hey///ho///')).toBe(true); // Unexpected but valid + expect(isValidPathname('/hey/héllô you')).toBe(true); + // expect(isValidPathname('')).toBe(false); expect(isValidPathname('hey')).toBe(false); - expect(isValidPathname('/hey/hô')).toBe(false); expect(isValidPathname('/hey?qs=ho')).toBe(false); expect(isValidPathname('https://fb.com/hey')).toBe(false); expect(isValidPathname('//hey')).toBe(false); diff --git a/packages/docusaurus-utils/src/index.ts b/packages/docusaurus-utils/src/index.ts index fab73e6ae1..baaba7e982 100644 --- a/packages/docusaurus-utils/src/index.ts +++ b/packages/docusaurus-utils/src/index.ts @@ -365,7 +365,8 @@ export function isValidPathname(str: string): boolean { } try { // weird, but is there a better way? - return new URL(str, 'https://domain.com').pathname === str; + const parsedPathname = new URL(str, 'https://domain.com').pathname; + return parsedPathname === str || parsedPathname === encodeURI(str); } catch (e) { return false; }