Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/core/event/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
38 changes: 38 additions & 0 deletions test/e2e/search.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down