fix(content-docs): make getActivePlugin match plugin paths more exactly (#6435)

* fix(content-docs): make getActivePlugin match plugin IDs more exactly

* refactor...
This commit is contained in:
Joshua Chen 2022-01-22 13:36:56 +08:00 committed by GitHub
parent cbcbdaa9d3
commit 64909e7f14
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 10 deletions

View file

@ -6,14 +6,17 @@
*/
import {
type ActivePlugin,
getActivePlugin,
getLatestVersion,
getActiveDocContext,
getActiveVersion,
getDocVersionSuggestions,
} from '../docsClientUtils';
import type {GlobalPluginData, GlobalVersion} from '../../types';
import type {
GlobalPluginData,
GlobalVersion,
ActivePlugin,
} from '@docusaurus/plugin-content-docs/client';
import {shuffle} from 'lodash';
describe('docsClientUtils', () => {
@ -58,6 +61,34 @@ describe('docsClientUtils', () => {
expect(getActivePlugin(data, '/android')).toEqual(activePluginAndroid);
expect(getActivePlugin(data, '/android/')).toEqual(activePluginAndroid);
expect(getActivePlugin(data, '/android/ijk')).toEqual(activePluginAndroid);
// https://github.com/facebook/docusaurus/issues/6434
const onePluginAtRoot = {
pluginIosId: {
path: '/',
versions: [],
},
pluginAndroidId: {
path: '/android',
versions: [],
},
};
expect(getActivePlugin(onePluginAtRoot, '/android/foo').pluginId).toEqual(
'pluginAndroidId',
);
const onePluginAtRootReversed = {
pluginAndroidId: {
path: '/android',
versions: [],
},
pluginIosId: {
path: '/',
versions: [],
},
};
expect(
getActivePlugin(onePluginAtRootReversed, '/android/foo').pluginId,
).toEqual('pluginAndroidId');
});
test('getLatestVersion', () => {

View file

@ -27,14 +27,17 @@ export function getActivePlugin(
pathname: string,
options: GetActivePluginOptions = {},
): ActivePlugin | undefined {
const activeEntry = Object.entries(allPluginDatas).find(
([_id, pluginData]) =>
!!matchPath(pathname, {
path: pluginData.path,
exact: false,
strict: false,
}),
);
const activeEntry = Object.entries(allPluginDatas)
// A quick route sorting: '/android/foo' should match '/android' instead of '/'
.sort((a, b) => b[1].path.localeCompare(a[1].path))
.find(
([, pluginData]) =>
!!matchPath(pathname, {
path: pluginData.path,
exact: false,
strict: false,
}),
);
const activePlugin: ActivePlugin | undefined = activeEntry
? {pluginId: activeEntry[0], pluginData: activeEntry[1]}