添加和长链接的省略号(用 jquery.linkify 链接)

发布于 2024-11-05 06:03:52 字数 536 浏览 0 评论 0原文

我正在使用“Linkify”将链接添加到静态文本...这就是我正在使用的:

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

我想添加一个 < ;wbr> (分词)在 15 个字符之后,以及 在 30 个左右之后...(如果链接小于 30 个字符,则不要添加...)

所以,链接将类似于: https://github.com/maranomynet/linkify&hellip;

我想我必须使用 var jquery.linkify-1.0.js 中的“$2”,但我对如何做到这一点有点困惑......

有任何线索吗?

谢谢!

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 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…

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

緦唸λ蓇 2024-11-12 06:03:52

我并不假装自己是 JavaScript/jQuery 大师,但这是我想出的似乎可行的方法。如果有人有更好的方法来执行某些功能,我会洗耳恭听——我更喜欢 C#,所以 Javascript/jQuery 是我正在努力改进的薄弱环节。

第 1 步: 将这段代码放在 linkify 插件可以读取的位置(我将其放在 linkify 文件中)。

function FormatLink(a) {
    // Configurable settings
    var wbrPosition = 15;
    var hellipPosition = 30;
    var wbr = '<wbr></wbr>';
    var 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();
}

第 2 步:更改 linkify 函数。替换为: 替换

  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`
    },

为:

  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 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 = '…';

    // 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文