Skip to content
Snippets Groups Projects
Verified Commit a592c772 authored by Jacques Erasmus's avatar Jacques Erasmus :speech_balloon: Committed by GitLab
Browse files

Merge branch '486381-sidebar-regex' into 'master'

Fix broken wiki sidebar when slugs contain special characters

See merge request !167331



Merged-by: default avatarJacques Erasmus <jerasmus@gitlab.com>
Approved-by: default avatarVanessa Otto <votto@gitlab.com>
Approved-by: default avatarJacques Erasmus <jerasmus@gitlab.com>
Reviewed-by: default avatarJacques Erasmus <jerasmus@gitlab.com>
Reviewed-by: default avatarVanessa Otto <votto@gitlab.com>
Co-authored-by: default avatarHimanshu Kapoor <hkapoor@gitlab.com>
parents 106183cc 2d2f164e
No related branches found
No related tags found
2 merge requests!170053Security patch upgrade alert: Only expose to admins 17-4,!167331Fix broken wiki sidebar when slugs contain special characters
Pipeline #1479960331 failed
...@@ -9,7 +9,8 @@ function deslugify(slug) { ...@@ -9,7 +9,8 @@ function deslugify(slug) {
export function sidebarEntriesToTree(entries) { export function sidebarEntriesToTree(entries) {
if (!entries.length) return []; if (!entries.length) return [];
const rootPath = entries[0].path.replace(new RegExp(`${entries[0].slug}$`), ''); const regex = new RegExp(`${entries[0].slug.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&')}$`);
const rootPath = entries[0].path.replace(regex, '');
const entriesMap = entries.reduce((acc, entry) => { const entriesMap = entries.reduce((acc, entry) => {
acc[entry.slug] = entry; acc[entry.slug] = entry;
return acc; return acc;
......
...@@ -94,4 +94,55 @@ describe('sidebarEntriesToTree', () => { ...@@ -94,4 +94,55 @@ describe('sidebarEntriesToTree', () => {
}, },
]); ]);
}); });
it('works properly even if the slugs contain special characters and emojis', () => {
const entries = [
{ path: '/00-START-HERE-:-)', slug: '00-START-HERE-:-)', title: '00 START HERE : )' },
{
path: '/Page-that-should-break-the-sidebar-)',
slug: 'Page-that-should-break-the-sidebar-)',
title: 'Page that should break the sidebar )',
},
{
path: '/What-about-this-/-new-subpage',
slug: 'What-about-this-/-new-subpage',
title: 'these subpages seems working just fine',
},
{
path: '/What-about-utf-%F0%9F%A4%AC',
slug: 'What-about-utf-🤬',
title: 'What about utf 🤬',
},
{ path: '/home', slug: 'home', title: 'Home' },
{ path: '/-not-trimmed-', slug: '-not-trimmed-', title: ' Not trimmed ' },
];
expect(sidebarEntriesToTree(entries)).toMatchObject([
{ path: '/-not-trimmed-', slug: '-not-trimmed-', title: ' Not trimmed ' },
{ slug: '00-START-HERE-:-)', path: '/00-START-HERE-:-)', title: '00 START HERE : )' },
{ slug: 'home', path: '/home', title: 'Home' },
{
slug: 'Page-that-should-break-the-sidebar-)',
path: '/Page-that-should-break-the-sidebar-)',
title: 'Page that should break the sidebar )',
},
{
slug: 'What-about-this-',
path: '/What-about-this-',
title: 'What about this ',
children: [
{
slug: 'What-about-this-/-new-subpage',
path: '/What-about-this-/-new-subpage',
title: 'these subpages seems working just fine',
},
],
},
{
slug: 'What-about-utf-🤬',
path: '/What-about-utf-%F0%9F%A4%AC',
title: 'What about utf 🤬',
},
]);
});
}); });
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment