fix: update sidebar anchor activation logic (#2779)

This commit is contained in:
Luffy
2026-08-09 12:41:54 +08:00
committed by GitHub
parent ee7063cfd9
commit 030652c895
2 changed files with 42 additions and 6 deletions
+4 -6
View File
@@ -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');
+38
View File
@@ -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: {