@openstack_dev/gatsby-theme-marketing-oif-core 1.0.33-beta.1 → 1.0.33-beta.3
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/package.json +1 -1
- package/src/components/Link.js +92 -25
package/package.json
CHANGED
package/src/components/Link.js
CHANGED
|
@@ -1,45 +1,112 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
import { Link as GatsbyLink } from "gatsby";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
|
|
4
|
+
/* global __PATH_PREFIX__ */
|
|
5
|
+
|
|
6
|
+
const normalizePrefix = (p) => {
|
|
7
|
+
if (!p) return "";
|
|
8
|
+
const trimmed = String(p).replace(/\/+$/, ""); // remove trailing slash
|
|
9
|
+
return trimmed === "/" ? "" : trimmed; // Gatsby uses "" for root
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const isAbsoluteUrl = (url = "") => /^(https?:)?\/\//i.test(url);
|
|
13
|
+
const isSingleSlashPath = (url = "") => /^\/(?!\/)/.test(url);
|
|
14
|
+
|
|
15
|
+
const stripPrefix = (path, prefix) => {
|
|
16
|
+
if (!prefix) return path;
|
|
17
|
+
if (path === prefix) return "/";
|
|
18
|
+
if (path.startsWith(`${prefix }/`)) return path.slice(prefix.length) || "/";
|
|
19
|
+
return path;
|
|
20
|
+
};
|
|
21
|
+
|
|
6
22
|
function Link({
|
|
7
|
-
children,
|
|
23
|
+
children,
|
|
24
|
+
to = "",
|
|
25
|
+
activeClassName,
|
|
26
|
+
partiallyActive,
|
|
27
|
+
...other
|
|
8
28
|
}) {
|
|
9
|
-
// Tailor the following test to your environment.
|
|
10
|
-
// This example assumes that any internal link (intended for Gatsby)
|
|
11
|
-
// will start with exactly one slash, and that anything else is external.
|
|
12
|
-
const internal = /^\/(?!\/)/.test(to);
|
|
13
|
-
// Use Gatsby Link for internal links, and <a> for others
|
|
14
29
|
const email = /\S+@\S+\.\S+/.test(to);
|
|
30
|
+
const hash = to.startsWith("#");
|
|
15
31
|
|
|
16
|
-
|
|
32
|
+
const rawPrefix =
|
|
33
|
+
typeof __PATH_PREFIX__ !== "undefined" ? __PATH_PREFIX__ : "";
|
|
34
|
+
const prefix = normalizePrefix(rawPrefix);
|
|
35
|
+
|
|
36
|
+
// Email
|
|
37
|
+
if (email) {
|
|
38
|
+
const href = /^mailto:/.test(to) ? to : `mailto:${to}`;
|
|
17
39
|
return (
|
|
18
|
-
<
|
|
19
|
-
to={to}
|
|
20
|
-
activeClassName={activeClassName}
|
|
21
|
-
partiallyActive={partiallyActive}
|
|
22
|
-
// eslint-disable-next-line react/jsx-props-no-spreading
|
|
23
|
-
{...other}
|
|
24
|
-
>
|
|
40
|
+
<a href={href} {...other}>
|
|
25
41
|
{children}
|
|
26
|
-
</
|
|
42
|
+
</a>
|
|
27
43
|
);
|
|
28
44
|
}
|
|
29
|
-
|
|
30
|
-
|
|
45
|
+
|
|
46
|
+
// Absolute URL -> new tab
|
|
47
|
+
if (isAbsoluteUrl(to)) {
|
|
31
48
|
return (
|
|
32
|
-
|
|
33
|
-
|
|
49
|
+
<a href={to} target="_blank" rel="noreferrer" {...other}>
|
|
50
|
+
{children}
|
|
51
|
+
</a>
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Hash -> same tab
|
|
56
|
+
if (hash) {
|
|
57
|
+
return (
|
|
58
|
+
<a href={to} {...other}>
|
|
59
|
+
{children}
|
|
60
|
+
</a>
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Not a normal "/path" -> default anchor same tab
|
|
65
|
+
if (!isSingleSlashPath(to)) {
|
|
66
|
+
return (
|
|
67
|
+
<a href={to} {...other}>
|
|
34
68
|
{children}
|
|
35
69
|
</a>
|
|
36
70
|
);
|
|
37
71
|
}
|
|
72
|
+
|
|
73
|
+
// If we are running with a prefix (/__PATH_PREFIX__):
|
|
74
|
+
// - Links already under /__PATH_PREFIX__ are INTERNAL, but must be de-prefixed for GatsbyLink
|
|
75
|
+
// - Links outside /__PATH_PREFIX__ (e.g. /software) should be plain <a> to avoid prefixing
|
|
76
|
+
if (prefix) {
|
|
77
|
+
const isPrefixed = to === prefix || to.startsWith(`${prefix}/`);
|
|
78
|
+
if (isPrefixed) {
|
|
79
|
+
const normalizedTo = stripPrefix(to, prefix); // "/marketplace/distros" -> "/distros"
|
|
80
|
+
return (
|
|
81
|
+
<GatsbyLink
|
|
82
|
+
to={normalizedTo}
|
|
83
|
+
activeClassName={activeClassName}
|
|
84
|
+
partiallyActive={partiallyActive}
|
|
85
|
+
{...other}
|
|
86
|
+
>
|
|
87
|
+
{children}
|
|
88
|
+
</GatsbyLink>
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// base site path
|
|
93
|
+
return (
|
|
94
|
+
<a href={to} {...other}>
|
|
95
|
+
{children}
|
|
96
|
+
</a>
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// No prefix site: normal Gatsby internal
|
|
38
101
|
return (
|
|
39
|
-
|
|
40
|
-
|
|
102
|
+
<GatsbyLink
|
|
103
|
+
to={to}
|
|
104
|
+
activeClassName={activeClassName}
|
|
105
|
+
partiallyActive={partiallyActive}
|
|
106
|
+
{...other}
|
|
107
|
+
>
|
|
41
108
|
{children}
|
|
42
|
-
</
|
|
109
|
+
</GatsbyLink>
|
|
43
110
|
);
|
|
44
111
|
}
|
|
45
112
|
|