-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathsafari-compat.ts
More file actions
48 lines (43 loc) · 1.31 KB
/
safari-compat.ts
File metadata and controls
48 lines (43 loc) · 1.31 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
38
39
40
41
42
43
44
45
46
47
48
/**
* Safari compatibility utilities
*/
/**
* Detect if the current browser is Safari
*/
const isSafari = (): boolean => {
if (typeof window === 'undefined' || typeof navigator === 'undefined') {
return false;
}
const ua = navigator.userAgent;
// Safari but not Chrome (Chrome UA also contains "Safari")
return (
ua.includes('Safari') &&
!ua.includes('Chrome') &&
!ua.includes('Chromium')
);
};
/**
* Test if the browser supports the regex features used by remark-gfm.
* remark-gfm uses unicode property escapes (\p{P}) which older Safari
* misinterprets as named capture groups, throwing "invalid group specifier name"
*/
const hasRemarkGfmRegexIssue = (): boolean => {
try {
// Test the pattern remark-gfm uses in transformGfmAutolinkLiterals
new RegExp('(?<=\\p{P})a', 'u');
return false; // Regex works fine
} catch {
return true; // Regex throws error
}
};
/**
* Returns true if remarkGfm should be disabled.
* Only disables for Safari browsers that have the regex issue.
*/
let shouldDisableCached: boolean | null = null;
export const getIsOldSafari = (): boolean => {
if (shouldDisableCached === null) {
shouldDisableCached = isSafari() && hasRemarkGfmRegexIssue();
}
return shouldDisableCached;
};