mirror of
https://github.com/docsifyjs/docsify.git
synced 2026-10-27 18:57:10 +08:00
fix: update sidebar anchor activation logic (#2779)
This commit is contained in:
@@ -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');
|
||||
|
||||
@@ -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: {
|
||||
|
||||
Reference in New Issue
Block a user