fix(netlify-cms-core): avoid leading replacement char in slug (#1965)

This commit is contained in:
Paul Melero 2018-12-27 05:45:06 +01:00 committed by Shawn Erquhart
parent b374ffe18b
commit 79c0445768
2 changed files with 9 additions and 2 deletions

View File

@ -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');
});

View File

@ -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;