Debian Codegraph @

node/autolinker/dist/commonjs/anchor-tag-builder.js

raw
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.AnchorTagBuilder = void 0;
4 var html_tag_1 = require("./html-tag");
5 var truncate_smart_1 = require("./truncate/truncate-smart");
6 var truncate_middle_1 = require("./truncate/truncate-middle");
7 var truncate_end_1 = require("./truncate/truncate-end");
8 /**
9 * @protected
10 * @class Autolinker.AnchorTagBuilder
11 * @extends Object
12 *
13 * Builds anchor (<a>) tags for the Autolinker utility when a match is
14 * found.
15 *
16 * Normally this class is instantiated, configured, and used internally by an
17 * {@link Autolinker} instance, but may actually be used indirectly in a
18 * {@link Autolinker#replaceFn replaceFn} to create {@link Autolinker.HtmlTag HtmlTag}
19 * instances which may be modified before returning from the
20 * {@link Autolinker#replaceFn replaceFn}. For example:
21 *
22 * var html = Autolinker.link( "Test google.com", {
23 * replaceFn : function( match ) {
24 * var tag = match.buildTag(); // returns an {@link Autolinker.HtmlTag} instance
25 * tag.setAttr( 'rel', 'nofollow' );
26 *
27 * return tag;
28 * }
29 * } );
30 *
31 * // generated html:
32 * // Test <a href="http://google.com" target="_blank" rel="nofollow">google.com</a>
33 */
34 var AnchorTagBuilder = /** @class */ (function () {
35 /**
36 * @method constructor
37 * @param {Object} [cfg] The configuration options for the AnchorTagBuilder instance, specified in an Object (map).
38 */
39 function AnchorTagBuilder(cfg) {
40 if (cfg === void 0) { cfg = {}; }
41 /**
42 * @cfg {Boolean} newWindow
43 * @inheritdoc Autolinker#newWindow
44 */
45 this.newWindow = false; // default value just to get the above doc comment in the ES5 output and documentation generator
46 /**
47 * @cfg {Object} truncate
48 * @inheritdoc Autolinker#truncate
49 */
50 this.truncate = {}; // default value just to get the above doc comment in the ES5 output and documentation generator
51 /**
52 * @cfg {String} className
53 * @inheritdoc Autolinker#className
54 */
55 this.className = ''; // default value just to get the above doc comment in the ES5 output and documentation generator
56 this.newWindow = cfg.newWindow || false;
57 this.truncate = cfg.truncate || {};
58 this.className = cfg.className || '';
59 }
60 /**
61 * Generates the actual anchor (&lt;a&gt;) tag to use in place of the
62 * matched text, via its `match` object.
63 *
64 * @param {Autolinker.match.Match} match The Match instance to generate an
65 * anchor tag from.
66 * @return {Autolinker.HtmlTag} The HtmlTag instance for the anchor tag.
67 */
68 AnchorTagBuilder.prototype.build = function (match) {
69 return new html_tag_1.HtmlTag({
70 tagName: 'a',
71 attrs: this.createAttrs(match),
72 innerHtml: this.processAnchorText(match.getAnchorText()),
73 });
74 };
75 /**
76 * Creates the Object (map) of the HTML attributes for the anchor (&lt;a&gt;)
77 * tag being generated.
78 *
79 * @protected
80 * @param {Autolinker.match.Match} match The Match instance to generate an
81 * anchor tag from.
82 * @return {Object} A key/value Object (map) of the anchor tag's attributes.
83 */
84 AnchorTagBuilder.prototype.createAttrs = function (match) {
85 var attrs = {
86 href: match.getAnchorHref(), // we'll always have the `href` attribute
87 };
88 var cssClass = this.createCssClass(match);
89 if (cssClass) {
90 attrs['class'] = cssClass;
91 }
92 if (this.newWindow) {
93 attrs['target'] = '_blank';
94 attrs['rel'] = 'noopener noreferrer'; // Issue #149. See https://mathiasbynens.github.io/rel-noopener/
95 }
96 if (this.truncate) {
97 if (this.truncate.length && this.truncate.length < match.getAnchorText().length) {
98 attrs['title'] = match.getAnchorHref();
99 }
100 }
101 return attrs;
102 };
103 /**
104 * Creates the CSS class that will be used for a given anchor tag, based on
105 * the `matchType` and the {@link #className} config.
106 *
107 * Example returns:
108 *
109 * - "" // no {@link #className}
110 * - "myLink myLink-url" // url match
111 * - "myLink myLink-email" // email match
112 * - "myLink myLink-phone" // phone match
113 * - "myLink myLink-hashtag" // hashtag match
114 * - "myLink myLink-mention myLink-twitter" // mention match with Twitter service
115 *
116 * @protected
117 * @param {Autolinker.match.Match} match The Match instance to generate an
118 * anchor tag from.
119 * @return {String} The CSS class string for the link. Example return:
120 * "myLink myLink-url". If no {@link #className} was configured, returns
121 * an empty string.
122 */
123 AnchorTagBuilder.prototype.createCssClass = function (match) {
124 var className = this.className;
125 if (!className) {
126 return '';
127 }
128 else {
129 var returnClasses = [className], cssClassSuffixes = match.getCssClassSuffixes();
130 for (var i = 0, len = cssClassSuffixes.length; i < len; i++) {
131 returnClasses.push(className + '-' + cssClassSuffixes[i]);
132 }
133 return returnClasses.join(' ');
134 }
135 };
136 /**
137 * Processes the `anchorText` by truncating the text according to the
138 * {@link #truncate} config.
139 *
140 * @private
141 * @param {String} anchorText The anchor tag's text (i.e. what will be
142 * displayed).
143 * @return {String} The processed `anchorText`.
144 */
145 AnchorTagBuilder.prototype.processAnchorText = function (anchorText) {
146 anchorText = this.doTruncate(anchorText);
147 return anchorText;
148 };
149 /**
150 * Performs the truncation of the `anchorText` based on the {@link #truncate}
151 * option. If the `anchorText` is longer than the length specified by the
152 * {@link #truncate} option, the truncation is performed based on the
153 * `location` property. See {@link #truncate} for details.
154 *
155 * @private
156 * @param {String} anchorText The anchor tag's text (i.e. what will be
157 * displayed).
158 * @return {String} The truncated anchor text.
159 */
160 AnchorTagBuilder.prototype.doTruncate = function (anchorText) {
161 var truncate = this.truncate;
162 if (!truncate || !truncate.length)
163 return anchorText;
164 var truncateLength = truncate.length, truncateLocation = truncate.location;
165 if (truncateLocation === 'smart') {
166 return (0, truncate_smart_1.truncateSmart)(anchorText, truncateLength);
167 }
168 else if (truncateLocation === 'middle') {
169 return (0, truncate_middle_1.truncateMiddle)(anchorText, truncateLength);
170 }
171 else {
172 return (0, truncate_end_1.truncateEnd)(anchorText, truncateLength);
173 }
174 };
175 return AnchorTagBuilder;
176 }());
177 exports.AnchorTagBuilder = AnchorTagBuilder;
178 //# sourceMappingURL=anchor-tag-builder.js.map