Skip to content

can't seem to get replaceFn to fire #199

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

Closed
adamgins opened this issue Feb 19, 2017 · 6 comments
Closed

can't seem to get replaceFn to fire #199

adamgins opened this issue Feb 19, 2017 · 6 comments

Comments

@adamgins
Copy link

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 === "[email protected]" ) {
                                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
Copy link
Owner

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

@adamgins
Copy link
Author

adamgins commented Feb 19, 2017

@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
Copy link

... 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
Copy link

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

@gregjacobs
Copy link
Owner

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
Copy link
Owner

gregjacobs commented Jan 22, 2019

@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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants