Skip to content

Commit e9b9bf3

Browse files
committed
Fix documentation pages redirect to github.io issues
1 parent 2c9360e commit e9b9bf3

File tree

2 files changed

+186
-9
lines changed

2 files changed

+186
-9
lines changed

_layouts/default.html

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,17 @@
44
<meta name="description" content="Documentation for Procore developers" />
55
<meta name="viewport" content="width=device-width, initial-scale=1">
66
<meta charset="utf-8">
7+
78
<title>{{ page.title }}</title>
8-
<base target="_parent">
9+
<script>
10+
// Only set base target="_parent" if NOT in an iframe
11+
// This allows iframed pages to navigate within the iframe
12+
if (window.self === window.top) {
13+
var base = document.createElement('base');
14+
base.target = '_parent';
15+
document.head.appendChild(base);
16+
}
17+
</script>
918
<link rel="stylesheet" href="{{ 'assets/css/main.css' | relative_url }}">
1019
<link rel="stylesheet" href="{{ site.baseurl }}/assets/css/nav.css"/>
1120
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css">
@@ -23,7 +32,7 @@
2332
<dl class="col_content">
2433
{% for item in section.items %}
2534
<dd{% if item.url == page.url %} class="active"{% endif %}>
26-
<a href="{% if item.absolute != true %}{{ site.url }}{{ site.baseurl }}{% endif %}{{ item.url }}">
35+
<a href="{% if item.absolute == true %}{{ site.url }}{{ site.baseurl }}{{ item.url }}{% else %}{{ item.url | relative_url }}{% endif %}">
2736
<!-- <a href="https://b601-208-127-107-52.ngrok-free.app/documentation{{ item.url }}"> -->
2837
{{ item.title }}
2938
</a>

assets/js/nav.js

Lines changed: 175 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,42 @@
4747
});
4848
// Ensure search results stay on developers.procore.com under /documentation
4949
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+
5086
var sjs = SimpleJekyllSearch({
5187
searchInput: document.getElementById("search-input"),
5288
resultsContainer: document.getElementById("results-container"),
@@ -71,26 +107,158 @@
71107
}
72108
return value;
73109
},
110+
noResultsText: "No results found",
111+
limit: 10,
112+
fuzzy: false
74113
});
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
76129
$("#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)) {
81156
try {
82-
var u = new URL(href);
157+
var u = new URL(currentHref);
83158
if (u.origin !== window.location.origin) {
84159
e.preventDefault();
85160
var path = u.pathname;
86161
if (!path.startsWith(basePath)) {
87162
path = basePath.replace(/\/$/, "") + "/" + path.replace(/^\//, "");
88163
}
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;
90167
}
91168
} catch (err) {
92169
// ignore malformed URLs
93170
}
94171
}
95172
});
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+
}
96264
})();

0 commit comments

Comments
 (0)