Compare commits
4 Commits
main
...
jc/fix-slu
| Author | SHA1 | Date |
|---|---|---|
|
|
f74784922b | |
|
|
81b8727352 | |
|
|
40661d0d31 | |
|
|
1326b4a305 |
|
|
@ -27,6 +27,7 @@ import {
|
||||||
normalizeFrontMatterTags,
|
normalizeFrontMatterTags,
|
||||||
groupTaggedItems,
|
groupTaggedItems,
|
||||||
getContentPathList,
|
getContentPathList,
|
||||||
|
sanitizeURL,
|
||||||
} from '@docusaurus/utils';
|
} from '@docusaurus/utils';
|
||||||
import type {LoadContext} from '@docusaurus/types';
|
import type {LoadContext} from '@docusaurus/types';
|
||||||
import {validateBlogPostFrontMatter} from './blogFrontMatter';
|
import {validateBlogPostFrontMatter} from './blogFrontMatter';
|
||||||
|
|
@ -189,7 +190,7 @@ async function processBlogSourceFile(
|
||||||
const title = frontMatter.title ?? contentTitle ?? parsedBlogFileName.text;
|
const title = frontMatter.title ?? contentTitle ?? parsedBlogFileName.text;
|
||||||
const description = frontMatter.description ?? excerpt ?? '';
|
const description = frontMatter.description ?? excerpt ?? '';
|
||||||
|
|
||||||
const slug = frontMatter.slug || parsedBlogFileName.slug;
|
const slug = sanitizeURL(frontMatter.slug || parsedBlogFileName.slug);
|
||||||
|
|
||||||
const permalink = normalizeUrl([baseUrl, routeBasePath, slug]);
|
const permalink = normalizeUrl([baseUrl, routeBasePath, slug]);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import {
|
||||||
addTrailingSlash,
|
addTrailingSlash,
|
||||||
isValidPathname,
|
isValidPathname,
|
||||||
resolvePathname,
|
resolvePathname,
|
||||||
|
sanitizeURL,
|
||||||
} from '@docusaurus/utils';
|
} from '@docusaurus/utils';
|
||||||
import {
|
import {
|
||||||
DefaultNumberPrefixParser,
|
DefaultNumberPrefixParser,
|
||||||
|
|
@ -66,10 +67,10 @@ export default function getSlug({
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`We couldn't compute a valid slug for document with id "${baseID}" in "${sourceDirName}" directory.
|
`We couldn't compute a valid slug for document with id "${baseID}" in "${sourceDirName}" directory.
|
||||||
The slug we computed looks invalid: ${slug}.
|
The slug we computed looks invalid: ${slug}.
|
||||||
Maybe your slug front matter is incorrect or you use weird chars in the file path?
|
Maybe your slug front matter is incorrect or you used weird chars in the file path?
|
||||||
By using the slug front matter, you should be able to fix this error, by using the slug of your choice:
|
By using the slug front matter, you should be able to fix this error:
|
||||||
|
|
||||||
Example =>
|
Example:
|
||||||
---
|
---
|
||||||
slug: /my/customDocPath
|
slug: /my/customDocPath
|
||||||
---
|
---
|
||||||
|
|
@ -79,5 +80,5 @@ slug: /my/customDocPath
|
||||||
return slug;
|
return slug;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ensureValidSlug(computeSlug());
|
return ensureValidSlug(sanitizeURL(computeSlug()));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@
|
||||||
import fs from 'fs-extra';
|
import fs from 'fs-extra';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import {
|
import {
|
||||||
|
sanitizeURL,
|
||||||
encodePath,
|
encodePath,
|
||||||
fileToPath,
|
fileToPath,
|
||||||
aliasedSitePath,
|
aliasedSitePath,
|
||||||
|
|
@ -110,7 +111,7 @@ export default async function pluginContentPages(
|
||||||
const permalink = normalizeUrl([
|
const permalink = normalizeUrl([
|
||||||
baseUrl,
|
baseUrl,
|
||||||
options.routeBasePath,
|
options.routeBasePath,
|
||||||
encodePath(fileToPath(relativeSource)),
|
encodePath(sanitizeURL(fileToPath(relativeSource))),
|
||||||
]);
|
]);
|
||||||
if (isMarkdownSource(relativeSource)) {
|
if (isMarkdownSource(relativeSource)) {
|
||||||
const content = await fs.readFile(source, 'utf-8');
|
const content = await fs.readFile(source, 'utf-8');
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
* LICENSE file in the root directory of this source tree.
|
* LICENSE file in the root directory of this source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {normalizeUrl, getEditUrl} from '../urlUtils';
|
import {normalizeUrl, getEditUrl, sanitizeURL} from '../urlUtils';
|
||||||
|
|
||||||
describe('normalizeUrl', () => {
|
describe('normalizeUrl', () => {
|
||||||
test('should normalize urls correctly', () => {
|
test('should normalize urls correctly', () => {
|
||||||
|
|
@ -132,6 +132,18 @@ describe('normalizeUrl', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('sanitizeURL', () => {
|
||||||
|
test('sanitize paths that are RR dynamic routes', () => {
|
||||||
|
expect(sanitizeURL('/:user')).toEqual('/user');
|
||||||
|
expect(sanitizeURL('/f{u}')).toEqual('/fu');
|
||||||
|
expect(sanitizeURL('/a(1)')).toEqual('/a1');
|
||||||
|
});
|
||||||
|
test('do not sanitize paths that can be encoded', () => {
|
||||||
|
expect(sanitizeURL('/a:b')).toEqual('/a:b');
|
||||||
|
expect(sanitizeURL('/你好')).toEqual('/你好');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('getEditUrl', () => {
|
describe('getEditUrl', () => {
|
||||||
test('returns right path', () => {
|
test('returns right path', () => {
|
||||||
expect(
|
expect(
|
||||||
|
|
|
||||||
|
|
@ -83,6 +83,13 @@ export function normalizeUrl(rawUrls: string[]): string {
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Some slugs that seem valid may be dynamic routes for React router. We sanitize these paths.
|
||||||
|
*/
|
||||||
|
export function sanitizeURL(url: string): string {
|
||||||
|
return url.replace(/(?:(?<=\/):)|[()*[\]{}]/g, '');
|
||||||
|
}
|
||||||
|
|
||||||
export function getEditUrl(
|
export function getEditUrl(
|
||||||
fileRelativePath: string,
|
fileRelativePath: string,
|
||||||
editUrl?: string,
|
editUrl?: string,
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
# Filename with brackets
|
||||||
|
|
||||||
|
I don't actually represent any React router quirks, but it's funny that I can end up not being sanitized
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
label: Weird paths
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
---
|
||||||
|
# Windows doesn't allow filepaths containing :
|
||||||
|
slug: :file
|
||||||
|
---
|
||||||
|
|
||||||
|
# Filename with leading colon
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
# Filename with parentheses
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
---
|
||||||
|
# Windows doesn't allow filepaths containing *
|
||||||
|
slug: file*aster
|
||||||
|
---
|
||||||
|
|
||||||
|
# Filename with asterisk
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
# Filename with braces
|
||||||
|
|
@ -28,6 +28,10 @@ const sidebars = {
|
||||||
dirName: 'tests',
|
dirName: 'tests',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
type: 'autogenerated',
|
||||||
|
dirName: 'weird-paths',
|
||||||
|
},
|
||||||
|
{
|
||||||
type: 'link',
|
type: 'link',
|
||||||
label: 'External Link test',
|
label: 'External Link test',
|
||||||
href: 'https://docusaurus.io',
|
href: 'https://docusaurus.io',
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue