enclose-uri.js 526 B

123456789101112131415161718192021222324
  1. 'use strict';
  2. var count = require('ccount');
  3. module.exports = enclose;
  4. var re = /\s/;
  5. /* Wrap `url` in angle brackets when needed, or when
  6. * forced.
  7. * In links, images, and definitions, the URL part needs
  8. * to be enclosed when it:
  9. *
  10. * - has a length of `0`;
  11. * - contains white-space;
  12. * - has more or less opening than closing parentheses.
  13. */
  14. function enclose(uri, always) {
  15. if (always || uri.length === 0 || re.test(uri) || count(uri, '(') !== count(uri, ')')) {
  16. return '<' + uri + '>';
  17. }
  18. return uri;
  19. }