diff --git a/packages/netlify-cms-core/src/lib/__tests__/urlHelper.spec.js b/packages/netlify-cms-core/src/lib/__tests__/urlHelper.spec.js index f5d23d3b..9aeee36b 100644 --- a/packages/netlify-cms-core/src/lib/__tests__/urlHelper.spec.js +++ b/packages/netlify-cms-core/src/lib/__tests__/urlHelper.spec.js @@ -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'); }); diff --git a/packages/netlify-cms-core/src/lib/urlHelper.js b/packages/netlify-cms-core/src/lib/urlHelper.js index d851e4ef..f1592af9 100644 --- a/packages/netlify-cms-core/src/lib/urlHelper.js +++ b/packages/netlify-cms-core/src/lib/urlHelper.js @@ -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;