Skip to content

Commit 4e84a35

Browse files
committed
Change the interface from truncateMiddle and truncateSmart options to bundling these in with the truncate option
1 parent 1bc184e commit 4e84a35

File tree

12 files changed

+299
-224
lines changed

12 files changed

+299
-224
lines changed

Gruntfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ module.exports = function(grunt) {
7373
'src/match/Phone.js',
7474
'src/match/Twitter.js',
7575
'src/match/Url.js',
76+
'src/truncate/TruncateEnd.js',
7677
'src/truncate/TruncateMiddle.js',
7778
'src/truncate/TruncateSmart.js'
7879
],

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,27 @@ providing an Object as the second parameter to [Autolinker.link()](http://gregja
101101
- [stripPrefix](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker-cfg-stripPrefix) : Boolean<br />
102102
`true` to have the 'http://' or 'https://' and/or the 'www.' stripped from the
103103
beginning of links, `false` otherwise. Defaults to `true`.<br /><br />
104-
- [truncate](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker-cfg-truncate) : Number<br />
104+
- [truncate](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker-cfg-truncate) : Number/Object<br />
105105
A number for how many characters long URLs/emails/Twitter handles/Twitter
106106
hashtags should be truncated to inside the text of a link. If the match is
107107
over the number of characters, it will be truncated to this length by
108108
replacing the end of the string with a two period ellipsis ('..').<br /><br />
109+
109110
Example: a url like 'http://www.yahoo.com/some/long/path/to/a/file' truncated
110111
to 25 characters may look like this: 'yahoo.com/some/long/pat..'<br /><br />
112+
113+
In the object form, both `length` and `location` may be specified to perform
114+
truncation. Available options for `location` are: 'end' (default), 'middle',
115+
or 'smart'. Example usage:
116+
117+
```javascript
118+
truncate: { length: 32, location: 'middle' }
119+
```
120+
121+
The 'smart' truncation option is for URLs where the algorithm attempts to
122+
strip out unnecessary parts of the URL (such as the 'www.', then URL scheme,
123+
hash, etc.) before trying to find a good point to insert the ellipsis if it is
124+
still too long. For details, see source code of: (TruncateSmart)[http://gregjacobs.github.io/Autolinker.js/gh-pages/docs/#!/api/Autolinker.truncate.TruncateSmart]
111125
- [className](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker-cfg-className) : String<br />
112126
A CSS class name to add to the generated anchor tags. This class will be added
113127
to all links, as well as this class plus "url"/"email"/"phone"/"twitter"/"hashtag"

src/AnchorTagBuilder.js

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
*
88
* Builds anchor (&lt;a&gt;) tags for the Autolinker utility when a match is found.
99
*
10-
* Normally this class is instantiated, configured, and used internally by an {@link Autolinker} instance, but may
11-
* actually be retrieved in a {@link Autolinker#replaceFn replaceFn} to create {@link Autolinker.HtmlTag HtmlTag} instances
12-
* which may be modified before returning from the {@link Autolinker#replaceFn replaceFn}. For example:
10+
* Normally this class is instantiated, configured, and used internally by an
11+
* {@link Autolinker} instance, but may actually be retrieved in a {@link Autolinker#replaceFn replaceFn}
12+
* to create {@link Autolinker.HtmlTag HtmlTag} instances which may be modified
13+
* before returning from the {@link Autolinker#replaceFn replaceFn}. For
14+
* example:
1315
*
1416
* var html = Autolinker.link( "Test google.com", {
1517
* replaceFn : function( autolinker, match ) {
@@ -31,7 +33,7 @@ Autolinker.AnchorTagBuilder = Autolinker.Util.extend( Object, {
3133
*/
3234

3335
/**
34-
* @cfg {Number} truncate
36+
* @cfg {Object} truncate
3537
* @inheritdoc Autolinker#truncate
3638
*/
3739

@@ -59,13 +61,11 @@ Autolinker.AnchorTagBuilder = Autolinker.Util.extend( Object, {
5961
* @return {Autolinker.HtmlTag} The HtmlTag instance for the anchor tag.
6062
*/
6163
build : function( match ) {
62-
var tag = new Autolinker.HtmlTag( {
64+
return new Autolinker.HtmlTag( {
6365
tagName : 'a',
6466
attrs : this.createAttrs( match.getType(), match.getAnchorHref() ),
6567
innerHtml : this.processAnchorText( match.getAnchorText() )
6668
} );
67-
68-
return tag;
6969
},
7070

7171

@@ -76,7 +76,7 @@ Autolinker.AnchorTagBuilder = Autolinker.Util.extend( Object, {
7676
* @protected
7777
* @param {"url"/"email"/"phone"/"twitter"/"hashtag"} matchType The type of
7878
* match that an anchor tag is being generated for.
79-
* @param {String} href The href for the anchor tag.
79+
* @param {String} anchorHref The href for the anchor tag.
8080
* @return {Object} A key/value Object (map) of the anchor tag's attributes.
8181
*/
8282
createAttrs : function( matchType, anchorHref ) {
@@ -134,24 +134,32 @@ Autolinker.AnchorTagBuilder = Autolinker.Util.extend( Object, {
134134

135135

136136
/**
137-
* Performs the truncation of the `anchorText`, if the `anchorText` is
138-
* longer than the {@link #truncate} option. Truncates the text to 2
139-
* characters fewer than the {@link #truncate} option, and adds ".." to the
140-
* end.
137+
* Performs the truncation of the `anchorText` based on the {@link #truncate}
138+
* option. If the `anchorText` is longer than the length specified by the
139+
* {@link #truncate} option, the truncation is performed based on the
140+
* `location` property. See {@link #truncate} for details.
141141
*
142142
* @private
143-
* @param {String} text The anchor tag's text (i.e. what will be displayed).
143+
* @param {String} anchorText The anchor tag's text (i.e. what will be
144+
* displayed).
144145
* @return {String} The truncated anchor text.
145146
*/
146147
doTruncate : function( anchorText ) {
147-
var truncateLength = this.truncate || Number.POSITIVE_INFINITY;
148-
if (this.truncateSmart) {
149-
return Autolinker.truncate.TruncateSmart( anchorText, truncateLength, ".." );
150-
}
151-
if (this.truncateMiddle) {
152-
return Autolinker.truncate.TruncateMiddle( anchorText, truncateLength, ".." );
148+
var truncate = this.truncate;
149+
if( !truncate ) return anchorText;
150+
151+
var truncateLength = truncate.length,
152+
truncateLocation = truncate.location;
153+
154+
if( truncateLocation === 'smart' ) {
155+
return Autolinker.truncate.TruncateSmart( anchorText, truncateLength, '..' );
156+
157+
} else if( truncateLocation === 'middle' ) {
158+
return Autolinker.truncate.TruncateMiddle( anchorText, truncateLength, '..' );
159+
160+
} else {
161+
return Autolinker.truncate.TruncateEnd( anchorText, truncateLength, '..' );
153162
}
154-
return Autolinker.Util.ellipsis( anchorText, truncateLength );
155163
}
156164

157165
} );

src/Autolinker.js

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
* - An {@link Autolinker.HtmlTag} instance, which can be used to build/modify an HTML tag before writing out its HTML text.
103103
*
104104
* @constructor
105-
* @param {Object} [config] The configuration options for the Autolinker instance, specified in an Object (map).
105+
* @param {Object} [cfg] The configuration options for the Autolinker instance, specified in an Object (map).
106106
*/
107107
var Autolinker = function( cfg ) {
108108
Autolinker.Util.assign( this, cfg ); // assign the properties of `cfg` onto the Autolinker instance. Prototype properties will be used for missing configs.
@@ -112,6 +112,15 @@ var Autolinker = function( cfg ) {
112112
if( hashtag !== false && hashtag !== 'twitter' && hashtag !== 'facebook' && hashtag !== 'instagram' ) {
113113
throw new Error( "invalid `hashtag` cfg - see docs" );
114114
}
115+
116+
// Normalize the `truncate` option
117+
var truncate = this.truncate = this.truncate || {};
118+
if( typeof truncate === 'number' ) {
119+
this.truncate = { length: truncate, location: 'end' };
120+
} else if( typeof truncate === 'object' ) {
121+
this.truncate.length = truncate.length || Number.POSITIVE_INFINITY;
122+
this.truncate.location = truncate.location || 'end';
123+
}
115124
};
116125

117126
Autolinker.prototype = {
@@ -175,40 +184,50 @@ Autolinker.prototype = {
175184
stripPrefix : true,
176185

177186
/**
178-
* @cfg {Number} truncate
187+
* @cfg {Number/Object} truncate
179188
*
180-
* A number for how many characters long matched text should be truncated to inside the text of
181-
* a link. If the matched text is over this number of characters, it will be truncated to this length by
182-
* adding a two period ellipsis ('..') to the end of the string.
189+
* ## Number Form
183190
*
184-
* For example: A url like 'http://www.yahoo.com/some/long/path/to/a/file' truncated to 25 characters might look
185-
* something like this: 'yahoo.com/some/long/pat..'
186-
*/
187-
truncate : undefined,
188-
189-
/**
190-
* @cfg {Boolean} truncateMiddle
191+
* A number for how many characters matched text should be truncated to
192+
* inside the text of a link. If the matched text is over this number of
193+
* characters, it will be truncated to this length by adding a two period
194+
* ellipsis ('..') to the end of the string.
191195
*
192-
* When true, truncation will occur at the dead-center of a URL, as opposed to the end of a URL.
193-
* Requires: truncate
196+
* For example: A url like 'http://www.yahoo.com/some/long/path/to/a/file'
197+
* truncated to 25 characters might look something like this:
198+
* 'yahoo.com/some/long/pat..'
194199
*
195-
* For example: A url like 'http://www.yahoo.com/some/long/path/to/a/file' truncated to 25 character might look
196-
* something like this: 'yahoo.com/s..th/to/a/file'
197-
*/
198-
truncateMiddle : false,
199-
200-
/**
201-
* @cfg {Boolean} truncateSmart
200+
* Example Usage:
201+
*
202+
* truncate: 25
202203
*
203-
* When true, ellipsis characters will be placed within a section of the URL causing it to still be somewhat human
204-
* readable.
205-
* Requires: truncate
206-
* Overrides: truncateMiddle
207204
*
208-
* For example: A url like 'http://www.yahoo.com/some/long/path/to/a/file' truncated to 25 character might look
209-
* something like this: 'yahoo.com/some..to/a/file'
205+
* ## Object Form
206+
*
207+
* An Object may also be provided with two properties: `length` (Number) and
208+
* `location` (String). `location` may be one of the following: 'end'
209+
* (default), 'middle', or 'smart'.
210+
*
211+
* Example Usage:
212+
*
213+
* truncate: { length: 25, location: 'middle' }
214+
*
215+
* @cfg {Number} truncate.length How many characters to allow before
216+
* truncation will occur.
217+
* @cfg {"end"/"middle"/"smart"} [truncate.location="end"]
218+
*
219+
* - 'end' (default): will truncate up to the number of characters, and then
220+
* add an ellipsis at the end. Ex: 'yahoo.com/some/long/pat..'
221+
* - 'middle': will truncate and add the ellipsis in the middle. Ex:
222+
* 'yahoo.com/s..th/to/a/file'
223+
* - 'smart': for URLs where the algorithm attempts to strip out unnecessary
224+
* parts first (such as the 'www.', then URL scheme, hash, etc.),
225+
* attempting to make the URL human-readable before looking for a good
226+
* point to insert the ellipsis if it is still too long. Ex:
227+
* 'yahoo.com/some..to/a/file'. For more details, see
228+
* {@link Autolinker.truncate.TruncateSmart}.
210229
*/
211-
truncateSmart : false,
230+
truncate : undefined,
212231

213232
/**
214233
* @cfg {String} className
@@ -455,14 +474,13 @@ Autolinker.prototype = {
455474
tagBuilder = this.tagBuilder = new Autolinker.AnchorTagBuilder( {
456475
newWindow : this.newWindow,
457476
truncate : this.truncate,
458-
truncateMiddle: this.truncateMiddle,
459-
truncateSmart: this.truncateSmart,
460477
className : this.className
461478
} );
462479
}
463480

464481
return tagBuilder;
465-
},
482+
}
483+
466484
};
467485

468486

src/HtmlTag.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ Autolinker.HtmlTag = Autolinker.Util.extend( Object, {
170170
/**
171171
* Retrieves an attribute from the HtmlTag. If the attribute does not exist, returns `undefined`.
172172
*
173-
* @param {String} name The attribute name to retrieve.
173+
* @param {String} attrName The attribute name to retrieve.
174174
* @return {String} The attribute's value, or `undefined` if it does not exist on the HtmlTag.
175175
*/
176176
getAttr : function( attrName ) {

src/truncate/TruncateEnd.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*global Autolinker */
2+
/**
3+
* A truncation feature where the ellipsis will be placed at the end of the URL.
4+
*
5+
* @param {String} anchorText
6+
* @param {Number} truncateLen The maximum length of the truncated output URL string.
7+
* @param {String} ellipsisChars The characters to place within the url, e.g. "..".
8+
* @return {String} The truncated URL.
9+
*/
10+
Autolinker.truncate.TruncateEnd = function(anchorText, truncateLen, ellipsisChars){
11+
return Autolinker.Util.ellipsis( anchorText, truncateLen, ellipsisChars );
12+
};

src/truncate/TruncateMiddle.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/*global Autolinker */
12
/**
23
* Date: 2015-10-05
34
* Author: Kasper Søfren <[email protected]> (https://github.com/kafoso)

0 commit comments

Comments
 (0)