-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinlineLinks.ts
More file actions
37 lines (33 loc) · 1.56 KB
/
inlineLinks.ts
File metadata and controls
37 lines (33 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* inlineLinks.ts :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rstancu <rstancu@student.42madrid.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/04/16 22:23:55 by rstancu #+# #+# */
/* Updated: 2026/04/16 22:23:56 by rstancu ### ########.fr */
/* */
/* ************************************************************************** */
/**
* Normalizes inline link targets produced by editor UI inputs.
* Bare domains become `https://...`, protocol-relative URLs become
* explicit HTTPS, and already-qualified or custom-scheme URLs are preserved.
*/
export function normalizeInlineLinkHref(href: string) {
const cleanHref = href.trim();
if (!cleanHref) {
return cleanHref;
}
if (/^[a-z][a-z\d+.-]*:/i.test(cleanHref) || cleanHref.startsWith("//")) {
return cleanHref.startsWith("//") ? `https:${cleanHref}` : cleanHref;
}
if (
/^[^\s/]+\.[^\s/]+(?:[/?#].*)?$/i.test(cleanHref) &&
!cleanHref.startsWith("/") &&
!cleanHref.startsWith("#")
) {
return `https://${cleanHref}`;
}
return cleanHref;
}