gregjacobs / Autolinker.js

Utility to Automatically Link URLs, Email Addresses, Phone Numbers, Twitter handles, and Hashtags in a given block of text/HTML
MIT License
1.48k stars 238 forks source link

can't seem to get `replaceFn` to fire #199

Closed adamgins closed 5 years ago

adamgins commented 7 years ago

Just creating a new issue, that's a revive of an old closed one #96:

@gregjacobs great package. I have been using in default state in production for ages. Thanks. I wanted to customize some of the mentions, I am having a similar issues to this issue of getting the replaceFn to fire.

I have initialized the autolinker with

var Autolinker = require( 'autolinker' );
var autolinker = new Autolinker( {
    urls : {
        schemeMatches : true,
        wwwMatches    : true,
        tldMatches    : true
    },
    email       : true,
    phone       : true,
    mention     : 'twitter',
    hashtag     : 'twitter',

    stripPrefix : true,
    newWindow   : true,

    truncate : {
        length   : 0,
        location : 'end'
    },

    className : ''
} );

then I call it like so

var linkedText = autolinker.link( inputText, {
                replaceFn : function( match ) {
                    console.log( "href = ", match.getAnchorHref() );
                    console.log( "text = ", match.getAnchorText() );
                    console.log("INPUTTEXT:", inputText, "MATCH TYPE:", match.getType(), match )

                    switch( match.getType() ) {
                        case 'url' :
                            console.log( "url: ", match.getUrl() );

                            return true;  // let Autolinker perform its normal anchor tag replacement

                        case 'email' :
                            var email = match.getEmail();
                            console.log( "email: ", email );

                            if( email === "my@own.address" ) {
                                return false;  // don't auto-link this particular email address; leave as-is
                            } else {
                                return;  // no return value will have Autolinker perform its normal anchor tag replacement (same as returning `true`)
                            }

                        case 'phone' :
                            console.log( "Phone Number: ", match.getNumber() );

                            return '<a href="http://newplace.to.link.phone.numbers.to/">' + match.getNumber() + '</a>';

                        case 'mention' :
                            console.log( "Mention: ", match.getMention() );
                            console.log( "Mention Service Name: ", match.getServiceName() );

                            return '<a href="http://newplace.to.link.mention.handles.to/">' + match.getMention() + '</a>';

                        case 'hashtag' :
                            console.log( "Hashtag: ", match.getHashtag() );

                            return '<a href="http://newplace.to.link.hashtag.handles.to/">' + match.getHashtag() + '</a>';
                    }
                }
            });
            return linkedText;

But the console.logs below never fire. Any ideas pls?

`` console.log( "href = ", match.getAnchorHref() ); console.log( "text = ", match.getAnchorText() ); console.log("INPUTTEXT:", inputText, "MATCH TYPE:", match.getType(), match )

gregjacobs commented 7 years ago

Hey @adamgins, what's the input string you're passing to Autolinker?

adamgins commented 7 years ago

@gregjacobs here's the console.log of a series 'INPUT TEXT' prior to calling the autolinker.link function:

  if (inputText) {
            console.log("INPUT TEXT", inputText)

            var linkedText = autolinker.link( inputText, {
                replaceFn : function( match ) {
                    console.log( "href = ", match.getAnchorHref() );

console.log:

INPUT TEXT Hello
app.js:82641 INPUT TEXT test
app.js:82641 INPUT TEXT test www.smh.com.au
app.js:82641 INPUT TEXT @something
app.js:82641 INPUT TEXT something @something #tag1
app.js:82641 INPUT TEXT #tag2
app.js:82641 INPUT TEXT @some

the replaceFn never gets called, the www.smh.com.au gets converted to a link

paulincai commented 6 years ago

... and a year later replaceFn still never gets called possibly because hashtag results in error if assigned true (as per the documentation). So hashtag can take false not boolean. :(

paulincai commented 6 years ago

ok, got it... for hashtags add either twitter, facebook or instagram as well as the replaceFn function and ... all ok.

gregjacobs commented 5 years ago

Hey guys,

Does anyone have any example input text that the replaceFn doesn't get called for?

Sorry for the delay btw - finally going through the backlog of all the issues now that I have a break from work!

gregjacobs commented 5 years ago

@adamgins Only two years later, I finally figured out what you are doing wrong: the replaceFn needs to go in the options for Autolinker, not as a second argument to the link() function:

var Autolinker = require( 'autolinker' );
var autolinker = new Autolinker( {
    urls : {
        schemeMatches : true,
        wwwMatches    : true,
        tldMatches    : true
    },
    email       : true,
    phone       : true,
    mention     : 'twitter',
    hashtag     : 'twitter',

    stripPrefix : true,
    newWindow   : true,

    truncate : {
        length   : 0,
        location : 'end'
    },

    className : '',

    replaceFn: function() { ... }   // <-- replaceFn goes here
} );

Hoping that you figured this out long before I did :)