From e60cde7a4c355091422409353f17d9f1f08c9be4 Mon Sep 17 00:00:00 2001 From: Edson Samuel Jr Date: Fri, 25 May 2018 10:48:21 -0300 Subject: [PATCH] Provide an optional callback to enable adding custom Matcher instances --- src/Autolinker.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Autolinker.js b/src/Autolinker.js index 2053c2c6..6e2fa2b2 100644 --- a/src/Autolinker.js +++ b/src/Autolinker.js @@ -140,6 +140,11 @@ var Autolinker = function( cfg ) { this.replaceFn = cfg.replaceFn || null; this.context = cfg.context || this; + var applyCustomMatchers = cfg.applyCustomMatchers; + if( typeof applyCustomMatchers === 'function' ) { + this.applyCustomMatchers = applyCustomMatchers; + } + this.htmlParser = null; this.matchers = null; this.tagBuilder = null; @@ -866,6 +871,9 @@ Autolinker.prototype = { new matchersNs.Url( { tagBuilder: tagBuilder, stripPrefix: this.stripPrefix, stripTrailingSlash: this.stripTrailingSlash, decodePercentEncoding: this.decodePercentEncoding } ) ]; + // merge with additional custom-made Matcher instances + matchers = matchers.concat(this.applyCustomMatchers(this)); + return ( this.matchers = matchers ); } else { @@ -907,8 +915,21 @@ Autolinker.prototype = { } return tagBuilder; - } + }, + /** + * An optional callback to be used when adding custom {@link Autolinker.Matcher Matcher} + * objects. This base implementation adds a failsafe return as an empty array, preventing + * it to misbehave while merging its results with default {@link Autolinker.Matcher Matchers} + * + * @param {Autolinker} autolinker - the actual autolinker instance + * exposed to the custom implementation of this callback function + * + * @return {Autolinker.matcher.Matcher[]} + */ + applyCustomMatchers: function (autolinker) { + return []; + } };