jqueryellipsislinkifywbr

Add <wbr> and ellipsis to long links (linkified with jquery.linkify)


I'm using "Linkify" to add links to static text... This is what I'm using:

https://github.com/maranomynet/linkify/blob/master/1.0/jquery.linkify-1.0.js

I would like to add a <wbr> (word break) after 15 characters, and a &hellip; after 30 or so... (if the link is <30 chars, don't add the …)

So, the link would be something like: https://github.com/mara<wbr></wbr>nomynet/linkify&hellip;

I suppose I have to work with the var "$2" in that jquery.linkify-1.0.js, but I'm a little confused on how to do it...

Any clue?

Thanks!


Solution

  • I don't pretend to be a JavaScript/jQuery master, but here's what I came up with that appears to work. If someone has a better method of performing some of the functions, I'm all ears -- I'm more of a C# guy, so Javascript/jQuery is a weak link I'm trying to improve on.

    Step 1: Put this piece of code somewhere that the linkify plugin can read it (I put it in the linkify file).

    function FormatLink(a) {
        // Configurable settings
        var wbrPosition = 15;
        var hellipPosition = 30;
        var wbr = '<wbr></wbr>';
        var hellip = '&hellip;';
    
        // Put the data into a span, makes it so we can alter it without losing surrounding text.
        var link = $('<span>' + a + '</span>');
    
        // If no href, this is not a URL. Pass it back.
        if (link.find('a').attr('href') == undefined) {
            return a;
        }
    
        jQuery.each(link.find('a'), function () {
            var original = $(this).html() + '</a>';
            var updated = $(this);
            // Set length
            var length = updated.html().length;
    
            if (length > hellipPosition) {
                updated.html(updated.html().substr(0, hellipPosition) + hellip);
            }
    
            if (length > wbrPosition) {
                updated.html(updated.html().substr(0, wbrPosition) + wbr + updated.html().substr(wbrPosition, length));
            }
    
            if (link.html() !== null && link.find('a').html() !== null && original !== null && updated.html() !== null) {
                var changes = link.html().replace(original, updated.html() + '</a>');
                if (changes !== null && changes !== '') {
                    link.html(changes);
                }
            }
        });
    
        return link.html();
    }
    

    Step 2: Alter the linkify function. Replace this:

      linkifier = function ( html ) {
          return html
                      .replace( noProtocolUrl, '$1<a href="<``>://$2">$2</a>$3' )  // NOTE: we escape `"http` as `"<``>` to make sure `httpOrMailtoUrl` below doesn't find it as a false-positive
                      .replace( httpOrMailtoUrl, '$1<a href="$2">$2</a>$3' )
                      .replace( /"<``>/g, '"http' );  // reinsert `"http`
        },
    

    With this:

      linkifier = function (html) {
          return FormatLink(html
                      .replace(noProtocolUrl, '$1<a href="<``>://$2">$2</a>$3')  // NOTE: we escape `"http` as `"<``>` to make sure `httpOrMailtoUrl` below doesn't find it as a false-positive
                      .replace(httpOrMailtoUrl, '$1<a href="$2">$2</a>$3')
                      .replace(/"<``>/g, '"http'));  // reinsert `"http`
      },
    

    I've tested a few variations of code blocks and they all appear to work, so let me know if you run into an example that doesn't work.