@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openstack_dev/gatsby-theme-marketing-oif-core",
3
- "version": "1.0.33-beta.1",
3
+ "version": "1.0.33-beta.3",
4
4
  "description": "Base theme for Marketing Sites",
5
5
  "author": "smarcet",
6
6
  "keywords": [
@@ -1,45 +1,112 @@
1
1
  import React from "react";
2
2
  import { Link as GatsbyLink } from "gatsby";
3
- // Since DOM elements <a> cannot receive activeClassName
4
- // and partiallyActive, destructure the prop here and
5
- // pass it only to GatsbyLink
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, to, activeClassName, partiallyActive, ...other
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
- if (internal) {
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
- <GatsbyLink
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
- </GatsbyLink>
42
+ </a>
27
43
  );
28
44
  }
29
- if (email) {
30
- const href = /^mailto:/.test(to) ? to : `mailto:${to}`;
45
+
46
+ // Absolute URL -> new tab
47
+ if (isAbsoluteUrl(to)) {
31
48
  return (
32
- // eslint-disable-next-line react/jsx-props-no-spreading
33
- <a href={href} {...other}>
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
- // eslint-disable-next-line react/jsx-props-no-spreading
40
- <a href={to} target="_blank" rel="noreferrer" {...other}>
102
+ <GatsbyLink
103
+ to={to}
104
+ activeClassName={activeClassName}
105
+ partiallyActive={partiallyActive}
106
+ {...other}
107
+ >
41
108
  {children}
42
- </a>
109
+ </GatsbyLink>
43
110
  );
44
111
  }
45
112