<commit_before>// Link parser plugin
//

'use strict';


var _        = require('lodash');
var $        = require('../cheequery');
var beautify = require('../beautify_url');


module.exports = function (N) {

  return function (parser) {
    var defaultRendererOpen = parser.md.renderer.rules.link_open || function (tokens, idx, options, env, _self) {
      return _self.renderToken(tokens, idx, options);
    };

    var defaultRendererClose = parser.md.renderer.rules.link_close || function (tokens, idx, options, env, _self) {
      return _self.renderToken(tokens, idx, options);
    };

    parser.md.renderer.rules.link_open = function (tokens, idx, options, env, _self) {
      tokens[idx].tag = 'msg-link';

      var href = tokens[idx].attrs[tokens[idx].attrIndex('href')][1];

      if (N.router.match(href) !== null) {
        tokens[idx].attrPush([ 'data-nd-internal', true ]);
      }

      if (tokens[idx].info === 'auto') {
        tokens[idx].attrPush([ 'data-nd-auto', true ]);
      }

      return defaultRendererOpen(tokens, idx, options, env, _self);
    };

    parser.md.renderer.rules.link_close = function (tokens, idx, options, env, _self) {
      tokens[idx].tag = 'msg-link';

      return defaultRendererClose(tokens, idx, options, env, _self);
    };

    var intTpl = _.template('<a href="<%- href %>" class="link link-int<%- auto ? " link-auto" : "" %>"' +
                            ' title="<%- title %>"></a>');
    var extTpl = _.template('<a href="<%- href %>" class="link link-ext<%- auto ? " link-auto" : "" %>"' +
                            ' title="<%- title %>" target="_blank" rel="nofollow"></a>');

    // Add more classes, and target=_blank for external links
    //
    parser.bus.after('render', function render_links(data) {
      data.ast.find('msg-link').each(function () {
        var $this = $(this);

        // remove empty links like this: [](link)
        if ($this.contents().length === 0) {
          $this.remove();
        }

        var fn = ($this.data('nd-internal') ? intTpl : extTpl);
        var replacement = $(fn({
          href:  $this.attr('href'),
          title: $this.attr('title'),
          auto:  $this.data('nd-auto')
        }));

        if ($this.data('nd-auto')) {
          replacement.append(beautify($this.attr('href'), 50));
        } else {
          replacement.append($this.contents());
        }

        $this.replaceWith(replacement);
      });
    });

    parser.md.enable([
      'link',
      'reference',
      'autolink',
      'linkify'
    ]);

    parser.md.set({
      linkify: true
    });

    parser.md.linkify.set({
      fuzzyIP: false,
      fuzzyLink: false
    });
  };
};<commit_msg>No autolinking for patterns without protocol<commit_after>// Link parser plugin
//

'use strict';


var _        = require('lodash');
var $        = require('../cheequery');
var beautify = require('../beautify_url');


module.exports = function (N) {

  return function (parser) {
    var defaultRendererOpen = parser.md.renderer.rules.link_open || function (tokens, idx, options, env, _self) {
      return _self.renderToken(tokens, idx, options);
    };

    var defaultRendererClose = parser.md.renderer.rules.link_close || function (tokens, idx, options, env, _self) {
      return _self.renderToken(tokens, idx, options);
    };

    parser.md.renderer.rules.link_open = function (tokens, idx, options, env, _self) {
      tokens[idx].tag = 'msg-link';

      var href = tokens[idx].attrs[tokens[idx].attrIndex('href')][1];

      if (N.router.match(href) !== null) {
        tokens[idx].attrPush([ 'data-nd-internal', true ]);
      }

      if (tokens[idx].info === 'auto') {
        tokens[idx].attrPush([ 'data-nd-auto', true ]);
      }

      return defaultRendererOpen(tokens, idx, options, env, _self);
    };

    parser.md.renderer.rules.link_close = function (tokens, idx, options, env, _self) {
      tokens[idx].tag = 'msg-link';

      return defaultRendererClose(tokens, idx, options, env, _self);
    };

    var intTpl = _.template('<a href="<%- href %>" class="link link-int<%- auto ? " link-auto" : "" %>"' +
                            ' title="<%- title %>"></a>');
    var extTpl = _.template('<a href="<%- href %>" class="link link-ext<%- auto ? " link-auto" : "" %>"' +
                            ' title="<%- title %>" target="_blank" rel="nofollow"></a>');

    // Add more classes, and target=_blank for external links
    //
    parser.bus.after('render', function render_links(data) {
      data.ast.find('msg-link').each(function () {
        var $this = $(this);

        // remove empty links like this: [](link)
        if ($this.contents().length === 0) {
          $this.remove();
        }

        var fn = ($this.data('nd-internal') ? intTpl : extTpl);
        var replacement = $(fn({
          href:  $this.attr('href'),
          title: $this.attr('title'),
          auto:  $this.data('nd-auto')
        }));

        if ($this.data('nd-auto')) {
          replacement.append(beautify($this.attr('href'), 50));
        } else {
          replacement.append($this.contents());
        }

        $this.replaceWith(replacement);
      });
    });

    parser.md.enable([
      'link',
      'reference',
      'autolink',
      'linkify'
    ]);

    parser.md.set({
      linkify: true
    });

    parser.md.linkify.set({
      fuzzyIP: false,
      fuzzyLink: false
    });

    // Don't detect links without protocol - too many false positives.
    parser.md.linkify.add('//', null);
  };
};
{'fraction_non_alphanumeric': 0.18303249097472923, 'fraction_numerical': 0.001444043321299639, 'mean_word_length': 2.352087114337568, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6784294', 'n_tokens_mistral': 1775, 'n_tokens_neox': 1717, 'n_words': 364}