<commit_before>'use strict';


var async = require('async');
var $     = require('nodeca.core/lib/parser/cheequery');


module.exports = function (N) {

  N.wire.once('init:parser', function medialink_plugin_init() {
    N.parse.addPlugin(
      'medialink',
      function (parser) {
        parser.bus.on('render', function render_medialinks(data, callback) {
          async.each(data.ast.find('.link[data-nd-auto]'), function (item, next) {
            var $tag = $(item);
            var data = { link: $tag.attr('href'), type: [ 'inline' ] };

            if (($tag.parent().prop('tagName') === 'P') && ($tag.parent().contents().length === 1)) {
              data.type.unshift('block');
            }

            N.wire.emit('internal:common.embed.ext', data, function (err) {
              if (err) {
                next(err);
                return;
              }

              // If block result - replace parent tag `P`
              if (data.type === 'block') {
                $tag.parent().replaceWith(data.html);

              // For inline result - just replace tag `A`
              } else {
                $tag.replaceWith(data.html);
              }

              next();
            });
          }, callback);
        });
      }<commit_msg>Fix error handling in medialinks<commit_after>'use strict';


var async = require('async');
var $     = require('nodeca.core/lib/parser/cheequery');


module.exports = function (N) {

  N.wire.once('init:parser', function medialink_plugin_init() {
    N.parse.addPlugin(
      'medialink',
      function (parser) {
        parser.bus.on('render', function render_medialinks(data, callback) {
          async.each(data.ast.find('.link[data-nd-auto]'), function (item, next) {
            var $tag = $(item);
            var data = { link: $tag.attr('href'), type: [ 'inline' ] };

            if (($tag.parent().prop('tagName') === 'P') && ($tag.parent().contents().length === 1)) {
              data.type.unshift('block');
            }

            N.wire.emit('internal:common.embed.ext', data, function (err) {
              // If any errors happen, ignore them and leave the link as is
              if (err) {
                next();
                return;
              }

              // If no result is returned, leave the link as is
              if (!data.html) {
                next();
                return;
              }

              // If block result - replace parent tag `P`
              if (data.type === 'block') {
                $tag.parent().replaceWith(data.html);

              // For inline result - just replace tag `A`
              } else {
                $tag.replaceWith(data.html);
              }

              next();
            });
          }, callback);
        });
      }
{'fraction_non_alphanumeric': 0.15753669889008234, 'fraction_numerical': 0.0007160759040458289, 'mean_word_length': 1.556267154620311, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6603068', 'n_tokens_mistral': 796, 'n_tokens_neox': 762, 'n_words': 170}