fix(netlify-cms-core): avoid leading replacement char in slug (#1965)
This commit is contained in:
parent
b374ffe18b
commit
79c0445768
@ -101,10 +101,14 @@ describe('sanitizeSlug', () => {
|
||||
expect(sanitizeSlug('test test')).toEqual('test-test');
|
||||
});
|
||||
|
||||
it('removes trailing replacemenets', () => {
|
||||
it('removes trailing replacements', () => {
|
||||
expect(sanitizeSlug('test test ')).toEqual('test-test');
|
||||
});
|
||||
|
||||
it('removes leading replacements', () => {
|
||||
expect(sanitizeSlug('"test" test')).toEqual('test-test');
|
||||
});
|
||||
|
||||
it('uses alternate replacements', () => {
|
||||
expect(sanitizeSlug('test test ', Map({ sanitize_replacement: '_' }))).toEqual('test_test');
|
||||
});
|
||||
|
@ -83,11 +83,14 @@ export function sanitizeSlug(str, options = Map()) {
|
||||
partialRight(sanitizeFilename, { replacement }),
|
||||
])(str);
|
||||
|
||||
// Remove any doubled or trailing replacement characters (that were added in the sanitizers).
|
||||
// Remove any doubled or leading/trailing replacement characters (that were added in the sanitizers).
|
||||
const doubleReplacement = new RegExp(`(?:${escapeRegExp(replacement)})+`, 'g');
|
||||
const trailingReplacment = new RegExp(`${escapeRegExp(replacement)}$`);
|
||||
const leadingReplacment = new RegExp(`^${escapeRegExp(replacement)}`);
|
||||
|
||||
const normalizedSlug = sanitizedSlug
|
||||
.replace(doubleReplacement, replacement)
|
||||
.replace(leadingReplacment, '')
|
||||
.replace(trailingReplacment, '');
|
||||
|
||||
return normalizedSlug;
|
||||
|
Loading…
x
Reference in New Issue
Block a user