Metalsmith plugin to sanitize special unicode chars in html

@param {Object or String} options (optional)
@return {Function}
15789
function plugin(options) {

    return function(files, metalsmith, done) {

        Object.keys(files).forEach(function(file) {
            var data = files[file];
            data.contents = new Buffer(special(data.contents.toString()));
        });
        done();
    }
}
907722
function sanitize(str, len) {
    len = len || 256;
    if (!str || typeof str === 'string' && str.length < 1)
        return str;
    if (typeof str !== 'string' || str.length > len)
        return null;
    return str.replace(/[\0\x08\x09\x1a\n\r"'\\\%]/g, function (char) {
        switch (char) {
            case "\0":
                return "\\0";
            case "\x08":
                return "\\b";
            case "\x09":
                return "\\t";
            case "\x1a":
                return "\\z";
            case "\n":
                return "\\n";
            case "\r":
                return "\\r";
            case "\"":
            case "'":
            case "\\":
            case "%":
                return "\\" + char; // prepends a backslash to backslash, percent,
            // and double/single quotes
        }
    });
}
1055519
function escapeJsonQuotes(json) {
      return JSON.stringify(json)
        .replace(/'/g, "&amp;apos;")
        .replace(/"/g, "&amp;quot;")
        /**
         * Codepen was unescaping &lt; (<) and &gt; (>) which caused, on some demos,
         * an unclosed elements (like <md-select>).
         * Used different unicode lookalike characters so it won't be considered as an element
         */
        .replace(/&amp;lt;/g, "&#x02C2;") // http://graphemica.com/%CB%82
        .replace(/&amp;gt;/g, "&#x02C3;"); // http://graphemica.com/%CB%83
    }
1312818
function( type, options, element ) {
				//check if options is character or has character
				options = typeof options != "object" ? {
					character: options
				} : options;

				//don't change the orignial
				options = h.extend({}, options)
				if ( options.character ) {
					h.extend(options, Syn.key.options(options.character, type));
					delete options.character;
				}

				options = h.extend({
					ctrlKey: !! Syn.key.ctrlKey,
					altKey: !! Syn.key.altKey,
					shiftKey: !! Syn.key.shiftKey,
					metaKey: !! Syn.key.metaKey
				}, options)

				return options;
			}
1264078
function stringify (value, replacer, spaces, escape) {
  // v8 checks arguments.length for optimizing simple call
  // https://bugs.chromium.org/p/v8/issues/detail?id=4730
  var json = replacer || spaces
    ? JSON.stringify(value, replacer, spaces)
    : JSON.stringify(value);

  if (escape) {
    json = json.replace(/[<>&]/g, function (c) {
      switch (c.charCodeAt(0)) {
        case 0x3c:
          return '\\u003c'
        case 0x3e:
          return '\\u003e'
        case 0x26:
          return '\\u0026'
        default:
          return c
      }
    })
  }

  return json
}
1351576
function normalize(value, options) {
  var settings = options || {}
  var allowApostrophes = settings.allowApostrophes
  var allowDashes = settings.allowDashes
  var result = (typeof value === 'string' ? value : toString(value))
    .toLowerCase()
    .replace(APOSTROPHE, QUOTE)

  if (allowApostrophes && allowDashes) {
    return result
  }

  if (allowApostrophes) {
    return result.replace(DASH, EMPTY)
  }

  if (allowDashes) {
    return result.replace(QUOTE, EMPTY)
  }

  return result.replace(ALL, EMPTY)
}
1081701
function processBlocks(textString, options, data) {

	var replacer = function replacer(match, $1, $2, $3, $4, $5, $6, offset, input) {
		var out = '',
			dataValue,
			type = String($2),
			key = String($3 || '').trim(),
			blockText = String($5),
			nestedBlocks;

		dataValue = _extract(key, data);

		if (options && typeof options === 'object' && options.strictKeys) {
			if(Number.isNaN(dataValue)) {
				return match;
			}
		}

		nestedBlocks = blockText.match(pattern);
		if (nestedBlocks) {
			blockText = blockText.replace(pattern, function(match, $1, $2, $3, $4, $5, $6, offset, input) {
				var index = match.indexOf($5);
				var length = $5.length;
				var body = $5.replace(/(?:\\*)\[(.+?)(?:\\*)\]/g, "\\[$1]");
				return match.substr(0, index) + body + match.substr(index + length);
			});
		}

		var obj = {}, proto = {}, KEY;
		if (typeof dataValue === 'object' && type === '#') {
			for(KEY in dataValue) {
				obj = {}, proto = {};
				proto.KEY = proto.INDEX = KEY;
				obj[KEY] = proto.VALUE = dataValue[KEY];
				obj.__proto__ = proto;
				if (typeof dataValue[KEY] === 'object' && !isNaN(parseInt(KEY))) {
					obj = dataValue[KEY];
					obj.__proto__ = proto;
				}
				out += renderData(blockText, options, obj, Boolean(options.prefix));
			}
			if (nestedBlocks) {
				out = processBlocks(out, options, data);
			}
		} else if (dataValue && (type === '~' || type === '#')) {
			// block logical truthy
			proto.KEY = proto.INDEX = key;
			proto.VALUE = Boolean(dataValue);
			obj = data;
			obj.__proto__ = proto;

			out += renderData(blockText, options, obj, Boolean(options.prefix));
			if (nestedBlocks) {
				out = processBlocks(out, options, data);
			}
		} else if (!dataValue && (type === '^' || type === '!')) {
			// block logical falsy
			proto.KEY = proto.INDEX = key;
			proto.VALUE = Boolean(dataValue);
			obj = data;
			obj.__proto__ = proto;

			out += renderData(blockText, options, obj, Boolean(options.prefix));
			if (nestedBlocks) {
				out = processBlocks(out, options, data);
			}
		}
		return out;
	};

	var pattern = new RegExp('(\\[\\s*(~|\\^|\\!|#)([^\\r\\n]+)\\s*\\])([\\s]?)([\\s\\S]*?)(\\[\\s*\\/\\3\\s*\\]\\4?)', 'mg');

	if(typeof data === 'object' && data !== null) {
		if (options && typeof options === 'object' && options.prefix) {
			pattern = new RegExp('(\\[\\s*(~|\\^|\\!|#)'+rxquote(String(options.prefix))+'[\\.\\-\\_]([^\\r\\n]+)\\s*\\])(\\s?)([\\s\\S]*?)(\\[\\s*\\/'+rxquote(String(options.prefix))+'[\\.\\-\\_]\\3\\s*\\]\\4?)', 'mg');
		}
		textString = textString.replace(pattern, replacer);
	}

	return textString;
}
96545
function _Markdown_formatOptions(options) {
    function toHighlight(code, lang) {
      if (!lang && elm$core$Maybe$isJust(options.defaultHighlighting)) {
        lang = options.defaultHighlighting.a;
      }

      if (
        typeof hljs !== "undefined" &&
        lang &&
        hljs.listLanguages().indexOf(lang) >= 0
      ) {
        return hljs.highlight(lang, code, true).value;
      }

      return code;
    }

    var gfm = options.githubFlavored.a;

    return {
      highlight: toHighlight,
      gfm: gfm,
      tables: gfm && gfm.tables,
      breaks: gfm && gfm.breaks,
      sanitize: options.sanitize,
      smartypants: options.smartypants
    };
  }
1350288
def markdown_to_safe_html(markdown_string):
  
  warning = ''
  # Convert to utf-8 whenever we have a binary input.
  if isinstance(markdown_string, six.binary_type):
    markdown_string_decoded = markdown_string.decode('utf-8')
    # Remove null bytes and warn if there were any, since it probably means
    # we were given a bad encoding.
    markdown_string = markdown_string_decoded.replace(u'\x00', u'')
    num_null_bytes = len(markdown_string_decoded) - len(markdown_string)
    if num_null_bytes:
      warning = ('<!-- WARNING: discarded %d null bytes in markdown string '
                 'after UTF-8 decoding -->\n') % num_null_bytes

  string_html = markdown.markdown(
      markdown_string, extensions=['markdown.extensions.tables'])
  string_sanitized = bleach.clean(
      string_html, tags=_ALLOWED_TAGS, attributes=_ALLOWED_ATTRIBUTES)
  return warning + string_sanitized
354623
function(file, options) {
	var data = fs.readFileSync(file, 'utf-8');

	// Remove UTF-8 BOM codes:
	data = data.trim();

	if (options.javascript) {
		return util.js.parse(data);
	}
	else {
		return JSON.parse(data);
	}
	return JSON.parse(data);
}
426676
def _normalize_options(options)
        _normalize_text(options)

        if options[:html]
          options[:html] = ERB::Util.html_escape(options[:html])
        end

        if options[:status]
          options[:status] = Rack::Utils.status_code(options[:status])
        end

        super
      end
89124
function sanitizeHtml(html){
    var separator = '/';

    if(html.constructor === Object){
        return map(html, function(value){
            var sanitizedHtml = value.split(separator)
                .map(sanitizeHtml)
                .join(separator);
            return sanitizedHtml;
        });
    } else if(html.constructor === Array){
        return html.map(sanitizeHtml);
    }

    var div = document.createElement('div');
    div.appendChild(document.createTextNode(html));
    var sanitizedHtml = div.innerHTML.replace(/&amp;/g, '&');

    if(html != sanitizedHtml){
        return ''; // Blank it
    }

    return sanitizedHtml;
}
1354915
function toStringLengthOptions(options) {
    var _options = {
      messageTemplates: {
        NOT_OF_TYPE: function NOT_OF_TYPE(value) {
          return "Value is not a String.  " + "Value type received: ".concat((0, _fjl.typeOf)(value), ".  ") + "Value received: \"".concat(value, "\".");
        }
      }
    };
    return (0, _lengthValidator.toLengthOptions)(options ? (0, _fjl.assignDeep)(_options, options) : _options);
  }
134294
function invalidChar(name, field = undefined) {
  let msg = `Invalid character in ${name}`;
  if (field !== undefined) {
    msg += ` ["${field}"]`;
  }
  return msg;
}
37008
function svgToString (filename) {
  if (!/\.svg$/i.test(filename)) {
    return through()
  }

  return through(function (buf, enc, next) {
    this.push('module.exports=' + JSON.stringify(buf.toString('utf8')))
    next()
  })
}
587602
function markdown(options) {
  return new Remarkable(extend({
    breaks: false,
    html: true,
    langPrefix: 'lang-',
    linkify: true,
    typographer: false,
    xhtmlOut: false
  }, options));
}
601189
function replace(regex, source, options) {
  var valueOrFn = getValueOrMatchFn(options.value)
  source.replace(regex, function () { return '' })
  return source.replace(regex, valueOrFn)
}