Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: nodejs/nodejs.org
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 32e70b258943b2766581165b97dea5c5f53efd64
Choose a base ref
..
head repository: nodejs/nodejs.org
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: cf3438e84d3befd0a066bef6ea77f3d67e5dd719
Choose a head ref
Showing with 25 additions and 15 deletions.
  1. +25 −15 apps/site/components/Common/Search/index.tsx
40 changes: 25 additions & 15 deletions apps/site/components/Common/Search/index.tsx
Original file line number Diff line number Diff line change
@@ -89,40 +89,50 @@ const SearchButton: FC = () => {
return href;
}

const currentPageHost = `${window.location.protocol}//${window.location.host}`;
const docsBaseUrl = `${currentPageHost}/docs`;
const lowerCaseHref = href.toLowerCase();
const baseURLObject = new URL(BASE_URL);
const baseURLHostName = baseURLObject.hostname;

// Keep original link if URL should not have locale (different host / same host but path starts with docs/)
const searchBoxURLObject = new URL(href);
const searchBoxURLHostName = searchBoxURLObject.hostname;
const serachBoxURLPathName = searchBoxURLObject.pathname;

// We do not want to add the locale to the url for external links and docs links
if (
!lowerCaseHref.startsWith(currentPageHost) ||
lowerCaseHref.startsWith(docsBaseUrl)
baseURLHostName !== searchBoxURLHostName ||
serachBoxURLPathName.startsWith('/docs/')
) {
return href;
}

const before = href.slice(0, currentPageHost.length);
const after = href.slice(currentPageHost.length);
return `${before}/${locale}${after}`;
const URLWithLocale = new URL(
`${locale}${searchBoxURLObject.pathname}`,
searchBoxURLObject.origin
);

return URLWithLocale.href;
}}
onAnswerSourceClick={event => {
event.preventDefault();

const currentPageHost = `${window.location.protocol}//${window.location.host}`;
const baseURLObject = new URL(BASE_URL);

const { path } = event.detail.source;

const finalURL = path.startsWith('docs/')
? `${currentPageHost}/${path}`
: `${currentPageHost}/${locale}/${path}`;
const finalPath = path.startsWith('docs/')
? path
: `${locale}/${path}`;

const finalURL = new URL(finalPath, baseURLObject);

window.open(finalURL, '_blank');
}}
onSearchResultClick={event => {
event.preventDefault();

const currentPageHost = `${window.location.protocol}//${window.location.host}`;
const fullURLObject = new URL(event.detail.result.path, BASE_URL);

router.push(`${currentPageHost}/${event.detail.result.path}`, {
// result.path already contains LOCALE. Locale is set to undefined here so router does not add it once again.
router.push(fullURLObject.href, {
locale: undefined,
});
}}