url.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.entityEscapedUrl = exports.createDefaultLocaleReplace = exports.isNextInternalUrl = exports.generateUrl = exports.isURL = exports.cleanPath = void 0;
  4. var cleanPath = function (text) {
  5. return text.replace(/([^:])(\/\/+)/g, '$1/');
  6. };
  7. exports.cleanPath = cleanPath;
  8. var isURL = function (text) {
  9. // old: /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/
  10. return /^https?:\/\//i.test(text);
  11. };
  12. exports.isURL = isURL;
  13. var generateUrl = function (baseUrl, slug) {
  14. return (0, exports.isURL)(slug) ? (0, exports.cleanPath)(slug) : (0, exports.cleanPath)("".concat(baseUrl, "/").concat(slug));
  15. };
  16. exports.generateUrl = generateUrl;
  17. /**
  18. * Checks whether a url is next.js specific or not
  19. * @param path path check
  20. */
  21. var isNextInternalUrl = function (path) {
  22. return new RegExp(/[^/]*^.[_]|^\/(404|500)$|\/_middleware$|(?:\[)/g).test(path);
  23. };
  24. exports.isNextInternalUrl = isNextInternalUrl;
  25. /**
  26. * Creates a replace function to replace the default locale
  27. * Avoids creating the same RegExp within each replace
  28. *
  29. * Replaces only if the path does not contain the locale as an actual valid path
  30. *
  31. * Given a default locale of en-US it replaces:
  32. * /en-US -> /
  33. * /en-US/home -> /home
  34. * /en-US/home/ -> /home/
  35. *
  36. * Does not replace if its actual page
  37. * /en-USA -> /en-USA
  38. * /en-USA/home -> /en-USA/home
  39. * /en-US-home -> /en-US-home
  40. *
  41. * @param defaultLocale defaultLocale as provided by i18n within next config
  42. */
  43. var createDefaultLocaleReplace = function (defaultLocale) {
  44. var defaultLocaleRegExp = new RegExp("^/".concat(defaultLocale, "($|/)"));
  45. return function (path) { return path.replace(defaultLocaleRegExp, '/'); };
  46. };
  47. exports.createDefaultLocaleReplace = createDefaultLocaleReplace;
  48. /**
  49. * Return UTF-8 encoded urls
  50. * @param path
  51. * @returns
  52. * @link https://developers.google.com/search/docs/advanced/sitemaps/build-sitemap#general-guidelines
  53. */
  54. var entityEscapedUrl = function (path) {
  55. return path
  56. .replace(/&/g, '&')
  57. .replace(/'/g, ''')
  58. .replace(/"/g, '"')
  59. .replace(/>/g, '>')
  60. .replace(/</g, '&lt;');
  61. };
  62. exports.entityEscapedUrl = entityEscapedUrl;