|
47 | 47 | }); |
48 | 48 | // Ensure search results stay on developers.procore.com under /documentation |
49 | 49 | var basePath = "/documentation"; |
| 50 | + |
| 51 | + // Function to rewrite search result links for iframe display |
| 52 | + function rewriteSearchResultLinks() { |
| 53 | + if (window.self !== window.top) { |
| 54 | + try { |
| 55 | + var parentOrigin = window.location.ancestorOrigins && window.location.ancestorOrigins.length > 0 |
| 56 | + ? window.location.ancestorOrigins[0] |
| 57 | + : document.referrer ? new URL(document.referrer).origin : null; |
| 58 | + |
| 59 | + if (parentOrigin) { |
| 60 | + $("#results-container a").each(function() { |
| 61 | + var $link = $(this); |
| 62 | + var originalHref = $link.attr("href"); |
| 63 | + |
| 64 | + // Skip if already processed or special links |
| 65 | + if ($link.data("original-href") || !originalHref || originalHref.startsWith("#") || originalHref.startsWith("mailto:")) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + // Get the path from the href |
| 70 | + var path = originalHref; |
| 71 | + if (!path.startsWith("/")) { |
| 72 | + path = "/" + path; |
| 73 | + } |
| 74 | + |
| 75 | + // Store original and set to parent origin for hover display |
| 76 | + $link.data("original-href", originalHref); |
| 77 | + $link.attr("href", parentOrigin + path); |
| 78 | + }); |
| 79 | + } |
| 80 | + } catch (err) { |
| 81 | + console.warn("Could not rewrite search result links:", err); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
50 | 86 | var sjs = SimpleJekyllSearch({ |
51 | 87 | searchInput: document.getElementById("search-input"), |
52 | 88 | resultsContainer: document.getElementById("results-container"), |
|
71 | 107 | } |
72 | 108 | return value; |
73 | 109 | }, |
| 110 | + noResultsText: "No results found", |
| 111 | + limit: 10, |
| 112 | + fuzzy: false |
74 | 113 | }); |
75 | | - // Defensive: if a result link is absolute and points off-site, rewrite it to this host |
| 114 | + |
| 115 | + // Use MutationObserver to rewrite search result links when they're added |
| 116 | + if (window.self !== window.top) { |
| 117 | + var resultsContainer = document.getElementById("results-container"); |
| 118 | + if (resultsContainer) { |
| 119 | + var observer = new MutationObserver(function(mutations) { |
| 120 | + rewriteSearchResultLinks(); |
| 121 | + }); |
| 122 | + observer.observe(resultsContainer, { |
| 123 | + childList: true, |
| 124 | + subtree: true |
| 125 | + }); |
| 126 | + } |
| 127 | + } |
| 128 | + // Handle clicks on search result links - navigate within iframe if in one |
76 | 129 | $("#results-container").on("click", "a", function (e) { |
77 | | - var href = $(this).attr("href"); |
78 | | - if (!href) return; |
79 | | - // Only act on absolute URLs |
80 | | - if (/^https?:\/\//i.test(href)) { |
| 130 | + var $link = $(this); |
| 131 | + var currentHref = $link.attr("href"); |
| 132 | + var originalHref = $link.data("original-href"); |
| 133 | + |
| 134 | + if (!currentHref) return; |
| 135 | + |
| 136 | + // If in iframe and link points to parent origin, navigate within iframe |
| 137 | + if (window.self !== window.top) { |
| 138 | + try { |
| 139 | + var parentOrigin = window.location.ancestorOrigins && window.location.ancestorOrigins.length > 0 |
| 140 | + ? window.location.ancestorOrigins[0] |
| 141 | + : document.referrer ? new URL(document.referrer).origin : null; |
| 142 | + |
| 143 | + if (parentOrigin && currentHref.startsWith(parentOrigin)) { |
| 144 | + e.preventDefault(); |
| 145 | + var path = new URL(currentHref).pathname + new URL(currentHref).search + new URL(currentHref).hash; |
| 146 | + window.location.href = path; |
| 147 | + return false; |
| 148 | + } |
| 149 | + } catch (err) { |
| 150 | + // ignore errors |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + // Fallback: handle absolute URLs pointing off-site |
| 155 | + if (/^https?:\/\//i.test(currentHref)) { |
81 | 156 | try { |
82 | | - var u = new URL(href); |
| 157 | + var u = new URL(currentHref); |
83 | 158 | if (u.origin !== window.location.origin) { |
84 | 159 | e.preventDefault(); |
85 | 160 | var path = u.pathname; |
86 | 161 | if (!path.startsWith(basePath)) { |
87 | 162 | path = basePath.replace(/\/$/, "") + "/" + path.replace(/^\//, ""); |
88 | 163 | } |
89 | | - window.location.href = path + (u.search || "") + (u.hash || ""); |
| 164 | + var newUrl = path + (u.search || "") + (u.hash || ""); |
| 165 | + window.location.href = newUrl; |
| 166 | + return false; |
90 | 167 | } |
91 | 168 | } catch (err) { |
92 | 169 | // ignore malformed URLs |
93 | 170 | } |
94 | 171 | } |
95 | 172 | }); |
| 173 | + |
| 174 | + // If in an iframe, rewrite link hrefs for hover display but intercept clicks to navigate within iframe |
| 175 | + if (window.self !== window.top) { |
| 176 | + try { |
| 177 | + var parentOrigin = window.location.ancestorOrigins && window.location.ancestorOrigins.length > 0 |
| 178 | + ? window.location.ancestorOrigins[0] |
| 179 | + : document.referrer ? new URL(document.referrer).origin : null; |
| 180 | + |
| 181 | + if (parentOrigin) { |
| 182 | + // Function to get the actual path from a href |
| 183 | + function getPathFromHref(href) { |
| 184 | + if (!href || href.startsWith("#") || href.startsWith("mailto:") || href.startsWith("javascript:")) { |
| 185 | + return null; |
| 186 | + } |
| 187 | + try { |
| 188 | + if (href.match(/^https?:\/\//)) { |
| 189 | + return new URL(href).pathname + new URL(href).search + new URL(href).hash; |
| 190 | + } else { |
| 191 | + // Relative URL |
| 192 | + return href.startsWith("/") ? href : "/" + href; |
| 193 | + } |
| 194 | + } catch (e) { |
| 195 | + return href.startsWith("/") ? href : "/" + href; |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + // Rewrite navigation links for hover display |
| 200 | + $("nav a").each(function() { |
| 201 | + var $link = $(this); |
| 202 | + var originalHref = $link.attr("href"); |
| 203 | + var path = getPathFromHref(originalHref); |
| 204 | + |
| 205 | + if (path && !path.startsWith("#") && !path.startsWith("mailto:")) { |
| 206 | + // Store original href in data attribute |
| 207 | + $link.data("original-href", originalHref); |
| 208 | + // Set href to parent origin for hover display |
| 209 | + $link.attr("href", parentOrigin + path); |
| 210 | + } |
| 211 | + }); |
| 212 | + |
| 213 | + // Rewrite links in main content area for hover display |
| 214 | + $("main a").each(function() { |
| 215 | + var $link = $(this); |
| 216 | + var originalHref = $link.attr("href"); |
| 217 | + var path = getPathFromHref(originalHref); |
| 218 | + |
| 219 | + if (path && !path.startsWith("#") && !path.startsWith("mailto:")) { |
| 220 | + // Store original href in data attribute |
| 221 | + $link.data("original-href", originalHref); |
| 222 | + // Set href to parent origin for hover display |
| 223 | + $link.attr("href", parentOrigin + path); |
| 224 | + } |
| 225 | + }); |
| 226 | + |
| 227 | + // Intercept clicks on navigation and content links to navigate within iframe |
| 228 | + $(document).on("click", "nav a, main a", function(e) { |
| 229 | + var $link = $(this); |
| 230 | + var originalHref = $link.data("original-href"); |
| 231 | + var currentHref = $link.attr("href"); |
| 232 | + |
| 233 | + // If we have a stored original href, use it; otherwise check current href |
| 234 | + var hrefToUse = originalHref || currentHref; |
| 235 | + |
| 236 | + // Skip external links, anchors, and special protocols |
| 237 | + if (!hrefToUse || hrefToUse.startsWith("#") || hrefToUse.startsWith("mailto:") || hrefToUse.startsWith("javascript:")) { |
| 238 | + return; // Let default behavior handle these |
| 239 | + } |
| 240 | + |
| 241 | + // If current href points to parent origin (for display), navigate using the path within iframe |
| 242 | + if (currentHref && currentHref.startsWith(parentOrigin)) { |
| 243 | + e.preventDefault(); |
| 244 | + var path = new URL(currentHref).pathname + new URL(currentHref).search + new URL(currentHref).hash; |
| 245 | + window.location.href = path; |
| 246 | + return false; |
| 247 | + } else if (hrefToUse.match(/^https?:\/\//) && !hrefToUse.startsWith(window.location.origin) && !hrefToUse.startsWith(parentOrigin)) { |
| 248 | + // External link (not parent origin, not current origin) - let it open normally |
| 249 | + return; |
| 250 | + } else if (originalHref && originalHref.startsWith(parentOrigin)) { |
| 251 | + // Original href was parent origin - navigate within iframe |
| 252 | + e.preventDefault(); |
| 253 | + var path = new URL(originalHref).pathname + new URL(originalHref).search + new URL(originalHref).hash; |
| 254 | + window.location.href = path; |
| 255 | + return false; |
| 256 | + } |
| 257 | + // For relative links, let default behavior work (navigate within iframe) |
| 258 | + }); |
| 259 | + } |
| 260 | + } catch (err) { |
| 261 | + console.warn("Could not rewrite links for parent origin:", err); |
| 262 | + } |
| 263 | + } |
96 | 264 | })(); |
0 commit comments