Repository: facebook/docusaurus
File: packages/docusaurus-plugin-content-docs/src/client/docsClientUtils.ts (TypeScript)

Code before:
          exact: false,
          strict: false,
        }),
    );

  const activePlugin: ActivePlugin | undefined = activeEntry
    ? {pluginId: activeEntry[0], pluginData: activeEntry[1]}
    : undefined;

  if (!activePlugin && options.failfast) {
    throw new Error(
      `Can't find active docs plugin for "${pathname}" pathname, while it was expected to be found. Maybe you tried to use a docs feature that can only be used on a docs-related page? Existing docs plugin paths are: ${Object.values(
        allPluginData,
      )
        .map((plugin) => plugin.path)
        .join(', ')}`,
    );
  }

  return activePlugin;
}

export const getLatestVersion = (data: GlobalPluginData): GlobalVersion =>
  data.versions.find((version) => version.isLast)!;

export function sortVersionsByPathDepth(
  versions: GlobalVersion[],
): GlobalVersion[] {
  return [...versions].sort((a, b) => {
    if (a.path.includes(b.path)) {
      return -1;
    }
    if (b.path.includes(a.path)) {
      return 1;
    }

    return 0;
  });
}

export function getActiveVersion(
  data: GlobalPluginData,
  pathname: string,
): GlobalVersion | undefined {
  // Sort paths by depth, deepest first
  const orderedVersionsMetadata = sortVersionsByPathDepth(data.versions);

  return orderedVersionsMetadata.find(
    (version) =>
      !!matchPath(pathname, {
        path: version.path,

Review comment (suggestion):
This function is an implementation detail of `getActiveVersion` and probably doesn't make sense as a standalone feature.

I'd prefer to not export it, and to only test it through `getActiveVersion`

What matters overall is that we can reliably get the active version, no matter the version metadata ordering.

Code after:
          exact: false,
          strict: false,
        }),
    );

  const activePlugin: ActivePlugin | undefined = activeEntry
    ? {pluginId: activeEntry[0], pluginData: activeEntry[1]}
    : undefined;

  if (!activePlugin && options.failfast) {
    throw new Error(
      `Can't find active docs plugin for "${pathname}" pathname, while it was expected to be found. Maybe you tried to use a docs feature that can only be used on a docs-related page? Existing docs plugin paths are: ${Object.values(
        allPluginData,
      )
        .map((plugin) => plugin.path)
        .join(', ')}`,
    );
  }

  return activePlugin;
}

export const getLatestVersion = (data: GlobalPluginData): GlobalVersion =>
  data.versions.find((version) => version.isLast)!;

export function getActiveVersion(
  data: GlobalPluginData,
  pathname: string,
): GlobalVersion | undefined {
  // Sort paths so that a match-all version like /docs/* is matched last
  // Otherwise /docs/* would match /docs/1.0.0/* routes
  // This is simplified but similar to the core sortRoutes() logic
  const sortedVersions = [...data.versions].sort((a, b) => {
    if (a.path === b.path) {
      return 0;
    }
    if (a.path.includes(b.path)) {
      return -1;
    }
    if (b.path.includes(a.path)) {
      return 1;
    }
    return 0;
  });

  return sortedVersions.find(
    (version) =>
      !!matchPath(pathname, {
        path: version.path,
        exact: false,
        strict: false,