1

静的テキストへのリンクを追加するために「Linkify」を使用しています...これは私が使用しているものです:

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

<wbr>15文字の後に(単語区切り)を追加したいのですが&hellip;、30文字程度の後に追加したいのですが...(リンクが30文字未満の場合は、追加しないでください…)

したがって、リンクは次のようになります。https://github.com/mara<wbr></wbr>nomynet/linkify&hellip;

そのjquery.linkify-1.0.jsでvar"$2"を使用する必要があると思いますが、その方法について少し混乱しています...

どんな手掛かり?

ありがとう!

4

1 に答える 1

3

私は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 = '&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`
  },

私はコードブロックのいくつかのバリエーションをテストしましたが、それらはすべて機能しているように見えるので、機能しない例に遭遇した場合はお知らせください。

于 2011-05-06T21:18:49.803 に答える