Urara-Blog/node_modules/.pnpm-store/v3/files/64/5cdae19e402e67d74db5666c0ed3614a6249ea98799f4f88a1297761e2fa8dd64b4c0a15ea51282df434f8f8d3546f2141ef4932a20e45e789788580028887
2022-08-14 01:14:53 +08:00

23 lines
815 B
Text

/**
* Make a value safe for injection as a URL.
*
* This encodes unsafe characters with percent-encoding and skips already
* encoded sequences (see `normalizeUri` below).
* Further unsafe characters are encoded as character references (see
* `micromark-util-encode`).
*
* Then, a regex of allowed protocols can be given, in which case the URL is
* sanitized.
* For example, `/^(https?|ircs?|mailto|xmpp)$/i` can be used for `a[href]`,
* or `/^https?$/i` for `img[src]`.
* If the URL includes an unknown protocol (one not matched by `protocol`, such
* as a dangerous example, `javascript:`), the value is ignored.
*
* @param {string|undefined} url
* @param {RegExp} [protocol]
* @returns {string}
*/
export function sanitizeUri(
url: string | undefined,
protocol?: RegExp | undefined
): string