diff --git a/app/assets/javascripts/discourse/helpers/application_helpers.js b/app/assets/javascripts/discourse/helpers/application_helpers.js index ac21bcae24b..9cb947fcfb6 100644 --- a/app/assets/javascripts/discourse/helpers/application_helpers.js +++ b/app/assets/javascripts/discourse/helpers/application_helpers.js @@ -4,12 +4,13 @@ @method breakUp @for Handlebars **/ -Handlebars.registerHelper('breakUp', function(property, options) { +Handlebars.registerHelper('breakUp', function(property, hint, options) { var prop, result, tokens; prop = Ember.Handlebars.get(this, property, options); if (!prop) return ""; + hint = Ember.Handlebars.get(this, hint, options); - return Discourse.Formatter.breakUp(prop); + return Discourse.Formatter.breakUp(prop, hint); }); /** diff --git a/app/assets/javascripts/discourse/lib/formatter.js b/app/assets/javascripts/discourse/lib/formatter.js index 40976524884..2512e3cd4eb 100644 --- a/app/assets/javascripts/discourse/lib/formatter.js +++ b/app/assets/javascripts/discourse/lib/formatter.js @@ -6,22 +6,39 @@ Discourse.Formatter = (function(){ relativeAgeMedium, relativeAgeMediumSpan, longDate, toTitleCase, shortDate, shortDateNoYear, tinyDateYear, breakUp; - breakUp = function(str){ + breakUp = function(str, hint){ var rval = []; var prev = str[0]; var cur; + var brk = "​"; + + var hintPos = []; + if(hint) { + hint = hint.toLowerCase().split(/\s+/).reverse(); + var current = 0; + while(hint.length > 0) { + var word = hint.pop(); + if(word !== str.substr(current, word.length).toLowerCase()) { + break; + } + current += word.length; + hintPos.push(current); + } + } rval.push(prev); for (var i=1;i"); + rval.push(brk); } else if(i>1 && prev.match(/[A-Z]/) && cur.match(/[a-z]/)){ rval.pop(); - rval.push(""); + rval.push(brk); rval.push(prev); } else if(prev.match(/[^A-Za-z0-9]/) && cur.match(/[a-zA-Z0-9]/)){ - rval.push(""); + rval.push(brk); + } else if(hintPos.indexOf(i) > -1) { + rval.push(brk); } rval.push(cur); diff --git a/app/assets/javascripts/discourse/templates/embedded_post.js.handlebars b/app/assets/javascripts/discourse/templates/embedded_post.js.handlebars index 31a3d740a53..e5bcfbf13f4 100644 --- a/app/assets/javascripts/discourse/templates/embedded_post.js.handlebars +++ b/app/assets/javascripts/discourse/templates/embedded_post.js.handlebars @@ -4,7 +4,7 @@
{{avatar this imageSize="large"}}
-
{{{breakUp username}}}
+
{{{breakUp username name}}}
diff --git a/app/assets/javascripts/discourse/templates/post.js.handlebars b/app/assets/javascripts/discourse/templates/post.js.handlebars index 5349123210c..46d0c53e415 100644 --- a/app/assets/javascripts/discourse/templates/post.js.handlebars +++ b/app/assets/javascripts/discourse/templates/post.js.handlebars @@ -22,7 +22,7 @@ {{#unless userDeleted}}
{{avatar this imageSize="large"}} -

{{{breakUp username}}}

+

{{{breakUp username name}}}

{{#if showName}}

{{breakUp name}}

diff --git a/test/javascripts/lib/formatter_test.js b/test/javascripts/lib/formatter_test.js index fef48e1b115..2c65e4f41d1 100644 --- a/test/javascripts/lib/formatter_test.js +++ b/test/javascripts/lib/formatter_test.js @@ -192,13 +192,14 @@ test("updateRelativeAge", function(){ test("breakUp", function(){ - var b = function(s){ return Discourse.Formatter.breakUp(s); }; + var b = function(s,hint){ return Discourse.Formatter.breakUp(s,hint); }; equal(b("hello"), "hello"); equal(b("helloworld"), "helloworld"); - equal(b("HeMans11"), "HeMans11"); - equal(b("he_man"), "he_man"); - equal(b("he11111"), "he11111"); - equal(b("HRCBob"), "HRCBob"); + equal(b("HeMans11"), "He​Mans​11"); + equal(b("he_man"), "he_​man"); + equal(b("he11111"), "he​11111"); + equal(b("HRCBob"), "HRC​Bob"); + equal(b("bobmarleytoo","Bob Marley Too"), "bob​marley​too"); });