Skip to content

Commit 2b95efe

Browse files
authored
Merge pull request #4819 from Blargian/fix_anchor_links_issue
Bug fix: take into account anchor tag in search results
2 parents c2526ea + f1b3741 commit 2b95efe

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/theme/SearchBar/utils/searchConfig.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,10 @@ export function transformSearchItems(items, options) {
9696
// For English, baseUrl is /docs/, so we want /tutorial
9797

9898
try {
99-
// Parse the URL to safely extract the pathname
99+
// Parse the URL to safely extract the pathname and hash
100100
const urlObj = new URL(url);
101101
const pathname = urlObj.pathname;
102+
const hash = urlObj.hash;
102103

103104
if (currentLocale !== 'en') {
104105
// Remove /docs/{locale} prefix, keeping the leading slash
@@ -119,6 +120,9 @@ export function transformSearchItems(items, options) {
119120
url = pathname;
120121
}
121122
}
123+
124+
// Append the hash back to the URL
125+
url += hash;
122126
} catch (e) {
123127
// If URL parsing fails, assume it's already a relative path
124128
// and use the original transformation logic as fallback

src/theme/SearchPage/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,12 @@ function SearchPageContent() {
215215
// For non-English locales, the baseUrl is /docs/{locale}/
216216
// Algolia returns URLs like /docs/{locale}/path
217217
// We need to make them relative: path (without leading slash)
218-
// Parse the URL to safely extract the pathname
218+
// Parse the URL to safely extract the pathname and hash
219219
let processedUrl = url;
220220
try {
221221
const urlObj = new URL(url);
222222
const pathname = urlObj.pathname;
223+
const hash = urlObj.hash;
223224

224225
if (currentLocale !== 'en') {
225226
const prefix = `/docs/${currentLocale}`;
@@ -236,6 +237,9 @@ function SearchPageContent() {
236237
processedUrl = pathname;
237238
}
238239
}
240+
241+
// Append the hash back to the URL
242+
processedUrl += hash;
239243
} catch (e) {
240244
// Fallback to original logic if parsing fails
241245
if (currentLocale !== 'en') {

0 commit comments

Comments
 (0)