mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-13 17:17:28 +02:00
fix: highlight appropriate navItem based on active sidebar item (#6310)
* fix: highlight appropriate navItem based on active sidebar item * fix: try using location.pathname * fix: remote console.log * fix: include category generated indices in globalData * Add test * fix snap Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
This commit is contained in:
parent
6e75d035bb
commit
3e5944ef1f
5 changed files with 73 additions and 9 deletions
|
@ -9,6 +9,9 @@
|
||||||
{
|
{
|
||||||
"type": "category",
|
"type": "category",
|
||||||
"label": "Slugs",
|
"label": "Slugs",
|
||||||
|
"link": {
|
||||||
|
"type": "generated-index"
|
||||||
|
},
|
||||||
"items": [
|
"items": [
|
||||||
"rootAbsoluteSlug",
|
"rootAbsoluteSlug",
|
||||||
"rootRelativeSlug",
|
"rootRelativeSlug",
|
||||||
|
|
|
@ -23,6 +23,9 @@ Object {
|
||||||
"rootTryToEscapeSlug",
|
"rootTryToEscapeSlug",
|
||||||
],
|
],
|
||||||
"label": "Slugs",
|
"label": "Slugs",
|
||||||
|
"link": Object {
|
||||||
|
"type": "generated-index",
|
||||||
|
},
|
||||||
"type": "category",
|
"type": "category",
|
||||||
},
|
},
|
||||||
Object {
|
Object {
|
||||||
|
|
|
@ -46,8 +46,8 @@ Object {
|
||||||
"lastUpdatedAt": undefined,
|
"lastUpdatedAt": undefined,
|
||||||
"lastUpdatedBy": undefined,
|
"lastUpdatedBy": undefined,
|
||||||
"next": Object {
|
"next": Object {
|
||||||
"permalink": "/docs/rootAbsoluteSlug",
|
"permalink": "/docs/category/slugs",
|
||||||
"title": "rootAbsoluteSlug",
|
"title": "Slugs",
|
||||||
},
|
},
|
||||||
"permalink": "/docs/foo/bazSlug.html",
|
"permalink": "/docs/foo/bazSlug.html",
|
||||||
"previous": Object {
|
"previous": Object {
|
||||||
|
@ -196,7 +196,11 @@ Object {
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
"label": "Slugs",
|
"label": "Slugs",
|
||||||
"link": undefined,
|
"link": Object {
|
||||||
|
"permalink": "/docs/category/slugs",
|
||||||
|
"slug": "/category/slugs",
|
||||||
|
"type": "generated-index",
|
||||||
|
},
|
||||||
"type": "category",
|
"type": "category",
|
||||||
},
|
},
|
||||||
Object {
|
Object {
|
||||||
|
@ -317,6 +321,11 @@ Object {
|
||||||
"path": "/docs/tryToEscapeSlug",
|
"path": "/docs/tryToEscapeSlug",
|
||||||
"sidebar": undefined,
|
"sidebar": undefined,
|
||||||
},
|
},
|
||||||
|
Object {
|
||||||
|
"id": "/category/slugs",
|
||||||
|
"path": "/docs/category/slugs",
|
||||||
|
"sidebar": "docs",
|
||||||
|
},
|
||||||
],
|
],
|
||||||
"isLast": true,
|
"isLast": true,
|
||||||
"label": "Next",
|
"label": "Next",
|
||||||
|
@ -340,6 +349,21 @@ Object {
|
||||||
|
|
||||||
exports[`simple website content: data 1`] = `
|
exports[`simple website content: data 1`] = `
|
||||||
Object {
|
Object {
|
||||||
|
"category-docs-docs-category-slugs-0fe.json": "{
|
||||||
|
\\"title\\": \\"Slugs\\",
|
||||||
|
\\"slug\\": \\"/category/slugs\\",
|
||||||
|
\\"permalink\\": \\"/docs/category/slugs\\",
|
||||||
|
\\"navigation\\": {
|
||||||
|
\\"previous\\": {
|
||||||
|
\\"title\\": \\"baz pagination_label\\",
|
||||||
|
\\"permalink\\": \\"/docs/foo/bazSlug.html\\"
|
||||||
|
},
|
||||||
|
\\"next\\": {
|
||||||
|
\\"title\\": \\"rootAbsoluteSlug\\",
|
||||||
|
\\"permalink\\": \\"/docs/rootAbsoluteSlug\\"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}",
|
||||||
"site-docs-doc-with-space-md-e90.json": "{
|
"site-docs-doc-with-space-md-e90.json": "{
|
||||||
\\"unversionedId\\": \\"doc with space\\",
|
\\"unversionedId\\": \\"doc with space\\",
|
||||||
\\"id\\": \\"doc with space\\",
|
\\"id\\": \\"doc with space\\",
|
||||||
|
@ -413,8 +437,8 @@ Object {
|
||||||
\\"permalink\\": \\"/docs/foo/bar\\"
|
\\"permalink\\": \\"/docs/foo/bar\\"
|
||||||
},
|
},
|
||||||
\\"next\\": {
|
\\"next\\": {
|
||||||
\\"title\\": \\"rootAbsoluteSlug\\",
|
\\"title\\": \\"Slugs\\",
|
||||||
\\"permalink\\": \\"/docs/rootAbsoluteSlug\\"
|
\\"permalink\\": \\"/docs/category/slugs\\"
|
||||||
}
|
}
|
||||||
}",
|
}",
|
||||||
"site-docs-heading-as-title-md-c6d.json": "{
|
"site-docs-heading-as-title-md-c6d.json": "{
|
||||||
|
@ -800,7 +824,8 @@ Object {
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
\\"collapsible\\": true,
|
\\"collapsible\\": true,
|
||||||
\\"collapsed\\": true
|
\\"collapsed\\": true,
|
||||||
|
\\"href\\": \\"/docs/category/slugs\\"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
\\"type\\": \\"link\\",
|
\\"type\\": \\"link\\",
|
||||||
|
@ -1009,6 +1034,11 @@ Object {
|
||||||
"path": "/docs/tryToEscapeSlug",
|
"path": "/docs/tryToEscapeSlug",
|
||||||
"sidebar": undefined,
|
"sidebar": undefined,
|
||||||
},
|
},
|
||||||
|
Object {
|
||||||
|
"id": "/category/slugs",
|
||||||
|
"path": "/docs/category/slugs",
|
||||||
|
"sidebar": "docs",
|
||||||
|
},
|
||||||
],
|
],
|
||||||
"isLast": true,
|
"isLast": true,
|
||||||
"label": "Next",
|
"label": "Next",
|
||||||
|
@ -1090,6 +1120,15 @@ Array [
|
||||||
},
|
},
|
||||||
"path": "/docs/absoluteSlug",
|
"path": "/docs/absoluteSlug",
|
||||||
},
|
},
|
||||||
|
Object {
|
||||||
|
"component": "@theme/DocCategoryGeneratedIndexPage",
|
||||||
|
"exact": true,
|
||||||
|
"modules": Object {
|
||||||
|
"categoryGeneratedIndex": "~docs/category-docs-docs-category-slugs-0fe.json",
|
||||||
|
},
|
||||||
|
"path": "/docs/category/slugs",
|
||||||
|
"sidebar": "docs",
|
||||||
|
},
|
||||||
Object {
|
Object {
|
||||||
"component": "@theme/DocItem",
|
"component": "@theme/DocItem",
|
||||||
"exact": true,
|
"exact": true,
|
||||||
|
|
|
@ -9,7 +9,11 @@ import {mapValues} from 'lodash';
|
||||||
import {normalizeUrl} from '@docusaurus/utils';
|
import {normalizeUrl} from '@docusaurus/utils';
|
||||||
import type {Sidebars} from './sidebars/types';
|
import type {Sidebars} from './sidebars/types';
|
||||||
import {createSidebarsUtils} from './sidebars/utils';
|
import {createSidebarsUtils} from './sidebars/utils';
|
||||||
import type {DocMetadata, LoadedVersion} from './types';
|
import type {
|
||||||
|
CategoryGeneratedIndexMetadata,
|
||||||
|
DocMetadata,
|
||||||
|
LoadedVersion,
|
||||||
|
} from './types';
|
||||||
import type {
|
import type {
|
||||||
GlobalVersion,
|
GlobalVersion,
|
||||||
GlobalSidebar,
|
GlobalSidebar,
|
||||||
|
@ -24,6 +28,16 @@ export function toGlobalDataDoc(doc: DocMetadata): GlobalDoc {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function toGlobalDataGeneratedIndex(
|
||||||
|
doc: CategoryGeneratedIndexMetadata,
|
||||||
|
): GlobalDoc {
|
||||||
|
return {
|
||||||
|
id: doc.slug,
|
||||||
|
path: doc.permalink,
|
||||||
|
sidebar: doc.sidebar,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export function toGlobalSidebars(
|
export function toGlobalSidebars(
|
||||||
sidebars: Sidebars,
|
sidebars: Sidebars,
|
||||||
version: LoadedVersion,
|
version: LoadedVersion,
|
||||||
|
@ -56,7 +70,9 @@ export function toGlobalDataVersion(version: LoadedVersion): GlobalVersion {
|
||||||
isLast: version.isLast,
|
isLast: version.isLast,
|
||||||
path: version.versionPath,
|
path: version.versionPath,
|
||||||
mainDocId: version.mainDocId,
|
mainDocId: version.mainDocId,
|
||||||
docs: version.docs.map(toGlobalDataDoc),
|
docs: version.docs
|
||||||
|
.map(toGlobalDataDoc)
|
||||||
|
.concat(version.categoryGeneratedIndices.map(toGlobalDataGeneratedIndex)),
|
||||||
sidebars: toGlobalSidebars(version.sidebars, version),
|
sidebars: toGlobalSidebars(version.sidebars, version),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,10 +20,12 @@ export async function createCategoryGeneratedIndexRoutes({
|
||||||
version,
|
version,
|
||||||
actions,
|
actions,
|
||||||
docCategoryGeneratedIndexComponent,
|
docCategoryGeneratedIndexComponent,
|
||||||
|
aliasedSource,
|
||||||
}: {
|
}: {
|
||||||
version: LoadedVersion;
|
version: LoadedVersion;
|
||||||
actions: PluginContentLoadedActions;
|
actions: PluginContentLoadedActions;
|
||||||
docCategoryGeneratedIndexComponent: string;
|
docCategoryGeneratedIndexComponent: string;
|
||||||
|
aliasedSource: (str: string) => string;
|
||||||
}): Promise<RouteConfig[]> {
|
}): Promise<RouteConfig[]> {
|
||||||
const slugs = createSlugger();
|
const slugs = createSlugger();
|
||||||
|
|
||||||
|
@ -69,7 +71,7 @@ export async function createCategoryGeneratedIndexRoutes({
|
||||||
component: docCategoryGeneratedIndexComponent,
|
component: docCategoryGeneratedIndexComponent,
|
||||||
exact: true,
|
exact: true,
|
||||||
modules: {
|
modules: {
|
||||||
categoryGeneratedIndex: propData,
|
categoryGeneratedIndex: aliasedSource(propData),
|
||||||
},
|
},
|
||||||
// Same as doc, this sidebar route attribute permits to associate this subpage to the given sidebar
|
// Same as doc, this sidebar route attribute permits to associate this subpage to the given sidebar
|
||||||
...(sidebar && {sidebar}),
|
...(sidebar && {sidebar}),
|
||||||
|
@ -149,6 +151,7 @@ export async function createVersionRoutes({
|
||||||
version,
|
version,
|
||||||
actions,
|
actions,
|
||||||
docCategoryGeneratedIndexComponent,
|
docCategoryGeneratedIndexComponent,
|
||||||
|
aliasedSource,
|
||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue