From 8d94869bf8eb09be241ca030f32abf2bf946cc27 Mon Sep 17 00:00:00 2001 From: SeaHOH Date: Sat, 26 Dec 2020 02:20:42 +0800 Subject: [PATCH] Fixed escapes single '_' of badge urls; affects [static website] (#5979) Fixed escapes inline single '_' of badge urls Co-authored-by: chris48s --- core/badge-urls/path-helpers.js | 7 ++----- core/badge-urls/path-helpers.spec.js | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 core/badge-urls/path-helpers.spec.js diff --git a/core/badge-urls/path-helpers.js b/core/badge-urls/path-helpers.js index 81c7222fe4..b7b3bded44 100644 --- a/core/badge-urls/path-helpers.js +++ b/core/badge-urls/path-helpers.js @@ -5,11 +5,8 @@ function escapeFormat(t) { return ( t - // Inline single underscore. - .replace(/([^_])_([^_])/g, '$1 $2') - // Leading or trailing underscore. - .replace(/([^_])_$/, '$1 ') - .replace(/^_([^_])/, ' $1') + // Single underscore. + .replace(/(^|[^_])((?:__)*)_(?!_)/g, '$1$2 ') // Double underscore and double dash. .replace(/__/g, '_') .replace(/--/g, '-') diff --git a/core/badge-urls/path-helpers.spec.js b/core/badge-urls/path-helpers.spec.js new file mode 100644 index 0000000000..ef7a0068e1 --- /dev/null +++ b/core/badge-urls/path-helpers.spec.js @@ -0,0 +1,21 @@ +'use strict' + +const { test, given } = require('sazerac') +const { escapeFormat } = require('./path-helpers') + +describe('Badge URL helper functions', function () { + test(escapeFormat, () => { + given('_single leading underscore').expect(' single leading underscore') + given('single trailing underscore_').expect('single trailing underscore ') + given('__double leading underscores').expect('_double leading underscores') + given('double trailing underscores__').expect( + 'double trailing underscores_' + ) + given('treble___underscores').expect('treble_ underscores') + given('fourfold____underscores').expect('fourfold__underscores') + given('double--dashes').expect('double-dashes') + given('treble---dashes').expect('treble--dashes') + given('fourfold----dashes').expect('fourfold--dashes') + given('once_in_a_blue--moon').expect('once in a blue-moon') + }) +})