tiptapify 0.3.4 → 0.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/tiptapify.mjs +4263 -4259
- package/dist/tiptapify.umd.js +63 -63
- package/package.json +1 -1
- package/src/utils/validateHref.test.ts +25 -0
- package/src/utils/validateHref.ts +17 -13
package/package.json
CHANGED
|
@@ -30,6 +30,23 @@ describe('isValidHref', () => {
|
|
|
30
30
|
expect(isValidHref('https://example.com/a/b?q=1-2_3%40&r=99')).toBe(true)
|
|
31
31
|
})
|
|
32
32
|
|
|
33
|
+
it('accepts facebook post, photo and permalink URLs', () => {
|
|
34
|
+
expect(isValidHref('https://www.facebook.com/user.name/posts/pfbid02abcXYZ123')).toBe(true)
|
|
35
|
+
expect(isValidHref('https://www.facebook.com/photo/?fbid=123456&set=a.789')).toBe(true)
|
|
36
|
+
expect(isValidHref('https://m.facebook.com/permalink.php?story_fbid=pfbid0abc&id=100064')).toBe(true)
|
|
37
|
+
expect(isValidHref('https://fb.watch/abc_DEF12/')).toBe(true)
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it('accepts dots, underscores and other URL characters in the path', () => {
|
|
41
|
+
expect(isValidHref('https://example.com/docs/file_v2.pdf')).toBe(true)
|
|
42
|
+
expect(isValidHref('https://example.com/~user/a+b,c:d')).toBe(true)
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('accepts a hyphenated or non-latin domain', () => {
|
|
46
|
+
expect(isValidHref('https://my-site.com')).toBe(true)
|
|
47
|
+
expect(isValidHref('https://ліга.укр/новини')).toBe(true)
|
|
48
|
+
})
|
|
49
|
+
|
|
33
50
|
it('accepts mailto links', () => {
|
|
34
51
|
expect(isValidHref('mailto:test@example.com')).toBe(true)
|
|
35
52
|
})
|
|
@@ -62,6 +79,14 @@ describe('isValidHref', () => {
|
|
|
62
79
|
expect(isValidHref('https://exa mple.com')).toBe(false)
|
|
63
80
|
})
|
|
64
81
|
|
|
82
|
+
it('rejects an http URL without slashes after the protocol', () => {
|
|
83
|
+
expect(isValidHref('https:example.com')).toBe(false)
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
it('rejects a domain with an empty label', () => {
|
|
87
|
+
expect(isValidHref('https://example..com')).toBe(false)
|
|
88
|
+
})
|
|
89
|
+
|
|
65
90
|
it('rejects plain text', () => {
|
|
66
91
|
expect(isValidHref('not a url')).toBe(false)
|
|
67
92
|
})
|
|
@@ -1,21 +1,25 @@
|
|
|
1
1
|
const azAZ09 = 'a-zA-Z0-9'
|
|
2
2
|
|
|
3
|
-
const regexHrefProto = 'https?:\\/\\/'
|
|
4
|
-
const regexHrefAuth = `([${azAZ09}]+(:[${azAZ09}]+)?@)?`
|
|
5
|
-
const regexHrefDomain = `[${azAZ09}]+(\\.[${azAZ09}]+(-?[${azAZ09}]+)?)+`
|
|
6
|
-
const regexHrefPath = `(\\/[${azAZ09}\\-]+)*\\/?`
|
|
7
|
-
const regexHrefFragment = '(#[^\\s]*)?'
|
|
8
|
-
const regexHrefQueryParam = `(\\?[${azAZ09}\\-_]+((\\[[${azAZ09}]+\\])?=[${azAZ09}\\-_%]+)?)?`
|
|
9
|
-
const regexHrefQueryParamExtra = `(&[${azAZ09}\\-_]+((\\[[${azAZ09}]+\\])?=[${azAZ09}\\-_%]+)?)*`
|
|
10
|
-
const regexHref = `${regexHrefProto}${regexHrefAuth}${regexHrefDomain}${regexHrefPath}${regexHrefFragment}${regexHrefQueryParam}${regexHrefQueryParamExtra}`
|
|
11
|
-
|
|
12
3
|
const regexMailto = `mailto:\\w+@[${azAZ09}]+(\\.[${azAZ09}]+)*`
|
|
13
4
|
const regexTel = 'tel:\\+?[0-9]+'
|
|
14
5
|
|
|
15
|
-
const
|
|
6
|
+
const hrefRegex = new RegExp(`^(${regexMailto}|${regexTel})$`, 'i')
|
|
7
|
+
|
|
8
|
+
const httpPrefixRegex = /^https?:\/\//i
|
|
9
|
+
const hostnameRegex = /^[^.]+(\.[^.]+)+$/
|
|
10
|
+
|
|
11
|
+
function isValidHttpUrl(href: string): boolean {
|
|
12
|
+
if (!httpPrefixRegex.test(href) || /\s/.test(href)) {
|
|
13
|
+
return false
|
|
14
|
+
}
|
|
16
15
|
|
|
17
|
-
|
|
16
|
+
try {
|
|
17
|
+
return hostnameRegex.test(new URL(href).hostname)
|
|
18
|
+
} catch {
|
|
19
|
+
return false
|
|
20
|
+
}
|
|
21
|
+
}
|
|
18
22
|
|
|
19
23
|
export function isValidHref(href: string): boolean {
|
|
20
|
-
return hrefRegex.test(href || '')
|
|
21
|
-
}
|
|
24
|
+
return isValidHttpUrl(href || '') || hrefRegex.test(href || '')
|
|
25
|
+
}
|