gitea/web_src/js/utils/url.ts
silverwind f4d7701189
Change function description comments to tsdoc style (#35185)
1. change function comments to the minimal [tsdoc](https://tsdoc.org/)
style. This has the benefit of making editors show the doc string in
tooltips when the function is hovered:

<img width="521" height="110" alt="image"
src="https://github.com/user-attachments/assets/b966f4f1-8239-433a-a456-5bd5c05d69ef"
/>

2. disable eslint `multiline-comment-style` as it conflicts with tsdoc.

---------

Signed-off-by: silverwind <me@silverwind.io>
2025-07-31 15:59:34 -07:00

32 lines
1020 B
TypeScript

export function pathEscapeSegments(s: string): string {
return s.split('/').map(encodeURIComponent).join('/');
}
function stripSlash(url: string): string {
return url.endsWith('/') ? url.slice(0, -1) : url;
}
export function isUrl(url: string): boolean {
try {
return stripSlash((new URL(url).href)).trim() === stripSlash(url).trim();
} catch {
return false;
}
}
/** Convert an absolute or relative URL to an absolute URL with the current origin. It only
* processes absolute HTTP/HTTPS URLs or relative URLs like '/xxx' or '//host/xxx'. */
export function toOriginUrl(urlStr: string) {
try {
if (urlStr.startsWith('http://') || urlStr.startsWith('https://') || urlStr.startsWith('/')) {
const {origin, protocol, hostname, port} = window.location;
const url = new URL(urlStr, origin);
url.protocol = protocol;
url.hostname = hostname;
url.port = port || (protocol === 'https:' ? '443' : '80');
return url.toString();
}
} catch {}
return urlStr;
}