Skip to content

Commit 030652c

Browse files
authored
fix: update sidebar anchor activation logic (docsifyjs#2779)
1 parent ee7063c commit 030652c

2 files changed

Lines changed: 42 additions & 6 deletions

File tree

‎src/core/event/index.js‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -504,12 +504,10 @@ export function Events(Base) {
504504
href = stripUrlExceptId(href);
505505

506506
const oldActive = dom.find(sidebar, 'li.active');
507-
const newActive = dom
508-
.find(
509-
sidebar,
510-
`a[href="${href}"], a[href="${decodeURIComponent(/** @type {string} */ (href))}"]`,
511-
)
512-
?.closest('li');
507+
const sidebarSelector = `.sidebar-nav a[href="${href}"], .sidebar-nav a[href="${decodeURIComponent(
508+
/** @type {string} */ (href),
509+
)}"]`;
510+
const newActive = dom.find(sidebar, sidebarSelector)?.closest('li');
513511

514512
if (newActive && newActive !== oldActive) {
515513
oldActive?.classList.remove('active');

‎test/e2e/search.test.js‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,44 @@ test.describe('Search Plugin Tests', () => {
140140
});
141141
});
142142

143+
test('updates the active sidebar anchor after clicking a heading result', async ({
144+
page,
145+
}) => {
146+
const docsifyInitConfig = {
147+
config: {
148+
subMaxLevel: 2,
149+
search: {
150+
paths: ['/'],
151+
},
152+
},
153+
markdown: {
154+
homepage: `
155+
# Home
156+
157+
## Installation
158+
159+
Searchable installation content.
160+
`,
161+
sidebar: '- [Home](/)',
162+
},
163+
scriptURLs: ['/dist/plugins/search.js'],
164+
};
165+
166+
const searchFieldElm = page.locator('input[type=search]');
167+
const resultsHeadingElm = page.locator('.results-panel .title');
168+
const installationSidebarItem = page
169+
.locator('.sidebar-nav a[href="#/?id=installation"]')
170+
.locator('..');
171+
172+
await docsifyInit(docsifyInitConfig);
173+
174+
await searchFieldElm.fill('installation');
175+
await expect(resultsHeadingElm).toHaveText('Installation');
176+
await page.click('.matching-post a');
177+
178+
await expect(installationSidebarItem).toHaveClass(/active/);
179+
});
180+
143181
test('search ignore title', async ({ page }) => {
144182
const docsifyInitConfig = {
145183
markdown: {

0 commit comments

Comments
 (0)