1
0
Fork 0
mirror of https://github.com/TryGhost/Ghost-Admin.git synced 2023-12-14 02:33:04 +01:00

Improve title casing

This commit is contained in:
Matthew Harrison-Jones 2013-07-18 14:42:16 +01:00
parent 9de10810a3
commit 52b0695e86
2 changed files with 23 additions and 1 deletions

22
assets/vendor/to-title-case.js vendored Normal file
View file

@ -0,0 +1,22 @@
/*
* To Title Case 2.0.1 http://individed.com/code/to-title-case/
* Copyright © 20082012 David Gouch. Licensed under the MIT License.
*/
String.prototype.toTitleCase = function () {
var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|vs?\.?|via)$/i;
return this.replace(/([^\W_]+[^\s-]*) */g, function (match, p1, index, title) {
if (index > 0 && index + p1.length !== title.length &&
p1.search(smallWords) > -1 && title.charAt(index - 2) !== ":" &&
title.charAt(index - 1).search(/[^\s-]/) < 0) {
return match.toLowerCase();
}
if (p1.substr(1).search(/[A-Z]|\../) > -1) {
return match;
}
return match.charAt(0).toUpperCase() + match.substr(1);
});
};

View file

@ -38,7 +38,7 @@
md = text.toLocaleLowerCase();
break;
case "titlecase":
md = text.replace(/\w\S*/g, function (text) {return text.charAt(0).toUpperCase() + text.substr(1).toLowerCase(); });
md = text.toTitleCase();
break;
case "selectword":
cursor = this.elem.getCursor();