Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide an optional callback to enable adding custom Matcher instances #240

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/Autolinker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 [];
}
};


Expand Down