-
Notifications
You must be signed in to change notification settings - Fork 239
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
Custom matcher #292
Comments
Thanks Stephane, your snippet helped me register a custom matcher for Amazon's ARNs, based on the Mention matcher and match source It'd be great to see this extension use case supported without hacks like this :) Maybe it could be an extra argument passed into the Autolinker constructor even. |
Unfortunately, @gregjacobs broke everything in version 4 😃. It's seems to be hard to add a custom matcher now. |
Hey @kakone, apologies! Can you tell me more about your custom matcher use case? What are you matching exactly? I'm trying to figure out how to best allow for this custom matchers scenario. In v4.0, I changed how matching worked to combine all of the previous "matchers" into one single non-deterministic finite state machine for performance reasons. (Autolinker previously walked through the input string one time for each matcher, and now only walks through the input string exactly once.) Are you looking to match on a regular expression? Curious as to what your current CustomMatcher class looks like. Best, |
I use autolink to handle the URLs and email adresses but I add a custom matcher for my business app : I detect the orders numbers (some purchase orders, repair orders, ...) to create a link to my web app. This is the source code of my custom matcher : export class OrderNumberMatcher extends Matcher {
private ordersUrls;
constructor(cfg: MatcherConfig, ordersUrls) {
super(cfg);
this.ordersUrls = ordersUrls;
}
parseMatches(text: string) {
let matches: Match[] = [],
match: RegExpExecArray | null;
for (let regExp in this.ordersUrls) {
let regularExpression = new RegExp(regExp, "g");
while ((match = regularExpression.exec(text)) !== null) {
matches.push(new OrderNumberMatch({
tagBuilder: this.tagBuilder,
matchedText: match[0],
offset: match.index
}, this.ordersUrls[regExp]));
}
}
return matches;
}
}
So, my OrderNumberMatch is like this : export class OrderNumberMatch extends Match {
private url: string;
constructor(cfg: MatchConfig, url: string) {
super(cfg);
this.url = url;
}
getType() {
return 'orderNumber';
}
getAnchorHref() {
return this.url.replace("{0}", this.matchedText);
}
getAnchorText() {
return this.matchedText;
}
} |
Hello,
Thanks for this great library, it's very useful. I wanted to add my own custom matcher to the matchers list. I tried this code and it works great :
So, could you make the getTagBuilder and getMatchers methods public? With these two public methods, it will be easy to add a custom matcher. Or is there another easy way to add a custom matcher ?
Cordially,
Stephane.
The text was updated successfully, but these errors were encountered: