gitea源码

1234567891011121314151617181920
  1. export function pathEscapeSegments(s: string): string {
  2. return s.split('/').map(encodeURIComponent).join('/');
  3. }
  4. /** Convert an absolute or relative URL to an absolute URL with the current origin. It only
  5. * processes absolute HTTP/HTTPS URLs or relative URLs like '/xxx' or '//host/xxx'. */
  6. export function toOriginUrl(urlStr: string) {
  7. try {
  8. if (urlStr.startsWith('http://') || urlStr.startsWith('https://') || urlStr.startsWith('/')) {
  9. const {origin, protocol, hostname, port} = window.location;
  10. const url = new URL(urlStr, origin);
  11. url.protocol = protocol;
  12. url.hostname = hostname;
  13. url.port = port || (protocol === 'https:' ? '443' : '80');
  14. return url.toString();
  15. }
  16. } catch {}
  17. return urlStr;
  18. }