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

Add option to truncate long URLs #16

Closed
euangoddard opened this issue Mar 4, 2014 · 13 comments
Closed

Add option to truncate long URLs #16

euangoddard opened this issue Mar 4, 2014 · 13 comments
Assignees
Labels
Milestone

Comments

@euangoddard
Copy link

It would be helpful to be able to pass an option to limit the number characters of the output link text inside the element to prevent overflow issue when people paste very long link.

If this is not possible, a post-linkification hook would be useful so that a 3rd party could perform such an action.

@nfrasser nfrasser self-assigned this Mar 4, 2014
@nfrasser
Copy link
Collaborator

nfrasser commented Mar 4, 2014

Both truncate and the hook are good features to have. Might be tricky to add to the current implementation. I'll see if I can get around it by the end of this week.

@euangoddard
Copy link
Author

I took a look at the source and I guess you'd need a step after the first regex replace with since it would be impossible to do it in a single step.

My other thought was to re-check all links which had the class that the linkify plugin sets, but I thought this might be quite inefficient.

@nfrasser
Copy link
Collaborator

nfrasser commented Mar 4, 2014

What's good about the current implementation is that it does it all in one go. To extract the individual URLs, the string might have to be split up and have some sort of lexical analysis performed on it.

And yeah, rechecking all the links would not be ideal but it would probably be the quickest solution.

@brennanmceachran
Copy link

Possible also to add a class on the link and set the width and ellipsis in css:
http://css-tricks.com/snippets/css/truncate-string-with-ellipsis/

@nfrasser
Copy link
Collaborator

nfrasser commented Mar 4, 2014

Or we could potentially build <a> tags in terms of DOM fragments rather than a string of HTML.

@euangoddard
Copy link
Author

I looked into a CSS approach, but this is unreliable cross-browser, hence the idea to alter the text node in the DOM.

@nfrasser
Copy link
Collaborator

Fixed in #51, with the format option.

Example:

$('p').linkify({
  format: function (value, type) { 
    // value is the link, type is 'url', 'email', etc.
    if (type === 'url' && value.length > 50) { 
      value = value.slice(0, 50) + '…'; 
    } 
    return value; 
  }
});

@simonferndriger
Copy link

Thank you very much!

@simonferndriger
Copy link

Not, I get the following error message Error: Cannot linkify [object Text] - Invalid DOM Node type

It worked before with version 1.1.6

@nfrasser
Copy link
Collaborator

What selector are you passing in?

@nfrasser nfrasser reopened this Jun 16, 2015
@deathg0d
Copy link

+1 for this feature
(Also to make links shorter and visually appealing, is there a way to hide http://, https:// and www etc?)

@nfrasser
Copy link
Collaborator

@deathg0d dedicated protocol and www. removal (though I wouldn't recommend removing www.) sounds like a pretty good idea. For now you can use the format option:

$('p').linkify({
  format: function (value, type) {
    if (type === 'url') {
      value = value.replace(/^((http|ftp)s?:\/\/|:\/\/)?(www\.)?/, '');
    }
    return value;
  }
});

Which is available in the latest pre-release.

@nfrasser
Copy link
Collaborator

Closing this off as I have not been able to reproduce @simonferndriger 's error. A separate issue can be filed if the error reoccurs.

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

No branches or pull requests

5 participants