Skip to content

Commit fcb4c9c

Browse files
committed
feat(pat-navigationmarker): Rebase URLs to agains the portal URL.
Closes: #1217
1 parent c4890d6 commit fcb4c9c

2 files changed

Lines changed: 56 additions & 2 deletions

File tree

src/pat/navigationmarker/navigationmarker.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ class Pattern extends BasePattern {
2828
* url which all links are compared to.
2929
*/
3030
mark_items(url) {
31-
const current_url = this.cleanup_url(url || this.current_url());
31+
const current_url = this.rebase_url(
32+
this.cleanup_url(url || this.current_url()),
33+
this.portal_url,
34+
);
3235

3336
const nav_items = this.el.querySelectorAll("a");
3437

@@ -37,7 +40,10 @@ class Pattern extends BasePattern {
3740
? nav_item.closest(this.options.itemWrapper)
3841
: nav_item.parentElement;
3942

40-
const nav_url = this.cleanup_url(nav_item.href);
43+
const nav_url = this.rebase_url(
44+
this.cleanup_url(nav_item.href),
45+
this.portal_url,
46+
);
4147

4248
// We can exit early, if the nav_url is not part of the current URL.
4349
if (current_url.indexOf(nav_url) === -1) {
@@ -88,6 +94,15 @@ class Pattern extends BasePattern {
8894
}
8995
}
9096

97+
/**
98+
* Rebase URL agains a base URL. Do nothing,
99+
*
100+
* @param {String} url: The Url to clean up.
101+
*/
102+
rebase_url(url, base_url) {
103+
return new URL(url, base_url)?.href;
104+
}
105+
91106
/**
92107
* Cleanup the URL from parts, which use brittle and useless to compare.
93108
*

src/pat/navigationmarker/navigationmarker.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,45 @@ describe("Navigation Marker", function () {
487487
);
488488
expect(news_link.parentElement.classList.contains("inPath")).toBe(false);
489489
});
490+
491+
it("handles URL rebasing correctly", async function () {
492+
// Test absolute URLs that should work directly
493+
const canonical_link = document.querySelector('head link[rel="canonical"]');
494+
canonical_link.href = "http://example.com/site/news/article-1";
495+
496+
// Set a portal URL
497+
document.body.dataset.portalUrl = "http://example.com/site";
498+
499+
document.body.innerHTML = `
500+
<nav class="pat-navigationmarker">
501+
<ul>
502+
<li><a href="http://example.com/site">Home</a></li>
503+
<li><a href="http://example.com/site/news">News</a></li>
504+
<li><a href="http://example.com/site/news/article-1">Article 1</a></li>
505+
</ul>
506+
</nav>
507+
`;
508+
509+
this.nav_element = document.querySelector(".pat-navigationmarker");
510+
registry.scan(document.body);
511+
await utils.timeout(1);
512+
513+
// Test with absolute URLs that match the canonical structure
514+
const article_link = this.nav_element.querySelector(
515+
"a[href='http://example.com/site/news/article-1']",
516+
);
517+
const news_link = this.nav_element.querySelector(
518+
"a[href='http://example.com/site/news']",
519+
);
520+
const home_link = this.nav_element.querySelector(
521+
"a[href='http://example.com/site']",
522+
);
523+
524+
expect(article_link.classList.contains("current")).toBe(true);
525+
expect(news_link.parentElement.classList.contains("inPath")).toBe(true);
526+
// Home should not be in-path since we're on a sub-page
527+
expect(home_link.parentElement.classList.contains("inPath")).toBe(false);
528+
});
490529
});
491530

492531
describe("Navigation Marker - Portal URL Edge Cases", function () {

0 commit comments

Comments
 (0)