From 098ea3c316cabadf3c7bfa4e106f54420c2d6835 Mon Sep 17 00:00:00 2001 From: Eric Arellano <14852634+Eric-Arellano@users.noreply.github.com> Date: Tue, 14 May 2024 17:06:42 -0400 Subject: [PATCH] Allow manually created class pages --- scripts/lib/api/TocGrouping.test.ts | 25 +++++++++++++++++------- scripts/lib/api/generateApiComponents.ts | 6 +++++- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/scripts/lib/api/TocGrouping.test.ts b/scripts/lib/api/TocGrouping.test.ts index 6c1dd25a902..eee844bba3d 100644 --- a/scripts/lib/api/TocGrouping.test.ts +++ b/scripts/lib/api/TocGrouping.test.ts @@ -82,16 +82,27 @@ async function getIndexModuleGroups(fp: string): Promise { const result: ModuleGroup[] = []; let currentGroup: ModuleGroup = []; for (const line of rawIndex.split("\n")) { - if (line.startsWith("* ")) { - if (line.includes("qiskit.")) { - const module = extractModuleName(line); - currentGroup.push(module); + // Each ModuleGroup represents an unordered list of entries starting with `*`. + // So, when we stop encountering `*`, we need to start a new ModuleGroup. + if (!line.startsWith("* ")) { + if (currentGroup.length) { + result.push(currentGroup); + currentGroup = []; } continue; - } else if (currentGroup.length) { - result.push(currentGroup); - currentGroup = []; } + + // Certain classes like QuantumCircuit in Qiskit 1.1+ have manually + // created pages. Those pages show up in index.mdx as top-level entries, + // but they are not top-level entries in the left ToC. This is expected. + // So, we allow the index to diverge from the left ToC. + // + // This is looking for e.g. '[`QuantumCircuit` class](qiskit.circuit.QuantumCircuit)' + const isDedicatedClassPage = line.includes(" class]("); + if (isDedicatedClassPage) continue; + + const module = extractModuleName(line); + currentGroup.push(module); } return result; } diff --git a/scripts/lib/api/generateApiComponents.ts b/scripts/lib/api/generateApiComponents.ts index 1d58ac1ead0..0aa1124be41 100644 --- a/scripts/lib/api/generateApiComponents.ts +++ b/scripts/lib/api/generateApiComponents.ts @@ -19,6 +19,7 @@ import remarkStringify from "remark-stringify"; import { ApiType } from "./Metadata"; import { getLastPartFromFullIdentifier, + removeSuffix, APOSTROPHE_HEX_CODE, } from "../stringUtils"; @@ -142,7 +143,10 @@ function prepareClassOrExceptionProps( modifiers, }; - const pageHeading = $dl.siblings("h1").text(); + let pageHeading = $dl.siblings("h1").text(); + // Manually created class pages like Qiskit 1.1+'s `QuantumCircuit` + // sometimes have ' class' in their h1. + pageHeading = removeSuffix(pageHeading, " class"); if (id.endsWith(pageHeading) && pageHeading != "") { // Page is already dedicated to the class return {