index.d.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. /**
  2. * Make a value safe for injection as a URL.
  3. *
  4. * This encodes unsafe characters with percent-encoding and skips already
  5. * encoded sequences (see `normalizeUri`).
  6. * Further unsafe characters are encoded as character references (see
  7. * `micromark-util-encode`).
  8. *
  9. * A regex of allowed protocols can be given, in which case the URL is
  10. * sanitized.
  11. * For example, `/^(https?|ircs?|mailto|xmpp)$/i` can be used for `a[href]`, or
  12. * `/^https?$/i` for `img[src]` (this is what `github.com` allows).
  13. * If the URL includes an unknown protocol (one not matched by `protocol`, such
  14. * as a dangerous example, `javascript:`), the value is ignored.
  15. *
  16. * @param {string | null | undefined} url
  17. * URI to sanitize.
  18. * @param {RegExp | null | undefined} [protocol]
  19. * Allowed protocols.
  20. * @returns {string}
  21. * Sanitized URI.
  22. */
  23. export function sanitizeUri(
  24. url: string | null | undefined,
  25. protocol?: RegExp | null | undefined
  26. ): string
  27. /**
  28. * Normalize a URL.
  29. *
  30. * Encode unsafe characters with percent-encoding, skipping already encoded
  31. * sequences.
  32. *
  33. * @param {string} value
  34. * URI to normalize.
  35. * @returns {string}
  36. * Normalized URI.
  37. */
  38. export function normalizeUri(value: string): string