diff --git a/src/core/event/index.js b/src/core/event/index.js index 839576143..ad194647c 100644 --- a/src/core/event/index.js +++ b/src/core/event/index.js @@ -504,12 +504,10 @@ export function Events(Base) { href = stripUrlExceptId(href); const oldActive = dom.find(sidebar, 'li.active'); - const newActive = dom - .find( - sidebar, - `a[href="${href}"], a[href="${decodeURIComponent(/** @type {string} */ (href))}"]`, - ) - ?.closest('li'); + const sidebarSelector = `.sidebar-nav a[href="${href}"], .sidebar-nav a[href="${decodeURIComponent( + /** @type {string} */ (href), + )}"]`; + const newActive = dom.find(sidebar, sidebarSelector)?.closest('li'); if (newActive && newActive !== oldActive) { oldActive?.classList.remove('active'); diff --git a/test/e2e/search.test.js b/test/e2e/search.test.js index 0344f0a21..cfcfdf99c 100644 --- a/test/e2e/search.test.js +++ b/test/e2e/search.test.js @@ -140,6 +140,44 @@ test.describe('Search Plugin Tests', () => { }); }); + test('updates the active sidebar anchor after clicking a heading result', async ({ + page, + }) => { + const docsifyInitConfig = { + config: { + subMaxLevel: 2, + search: { + paths: ['/'], + }, + }, + markdown: { + homepage: ` + # Home + + ## Installation + + Searchable installation content. + `, + sidebar: '- [Home](/)', + }, + scriptURLs: ['/dist/plugins/search.js'], + }; + + const searchFieldElm = page.locator('input[type=search]'); + const resultsHeadingElm = page.locator('.results-panel .title'); + const installationSidebarItem = page + .locator('.sidebar-nav a[href="#/?id=installation"]') + .locator('..'); + + await docsifyInit(docsifyInitConfig); + + await searchFieldElm.fill('installation'); + await expect(resultsHeadingElm).toHaveText('Installation'); + await page.click('.matching-post a'); + + await expect(installationSidebarItem).toHaveClass(/active/); + }); + test('search ignore title', async ({ page }) => { const docsifyInitConfig = { markdown: {