Skip to content

Commit

Permalink
Allow manually created class pages (Qiskit#1382)
Browse files Browse the repository at this point in the history
This unblocks Qiskit/qiskit#12403. Without this
PR, we incorrectly treat the class as inline, which means it gets the
blue left bar and an h3 incorrectly added.

Without this PR, our test to check that the ToC and index page are in
sync also fails, even though things behave how we expect
(Qiskit/qiskit#12403 (review))
  • Loading branch information
Eric-Arellano authored May 15, 2024
1 parent 107cff5 commit 37f647e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
25 changes: 18 additions & 7 deletions scripts/lib/api/TocGrouping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,27 @@ async function getIndexModuleGroups(fp: string): Promise<ModuleGroup[]> {
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;
}
Expand Down
6 changes: 5 additions & 1 deletion scripts/lib/api/generateApiComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import remarkStringify from "remark-stringify";
import { ApiType } from "./Metadata";
import {
getLastPartFromFullIdentifier,
removeSuffix,
APOSTROPHE_HEX_CODE,
} from "../stringUtils";

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 37f647e

Please sign in to comment.