From b998cdc8b9c0569fd3c489d888495a35cdff804f Mon Sep 17 00:00:00 2001 From: Eric Arellano <14852634+Eric-Arellano@users.noreply.github.com> Date: Fri, 5 Jan 2024 13:10:51 -0500 Subject: [PATCH] Add more tests for API generation script (#581) Part of https://github.com/Qiskit/documentation/issues/223. One way to improve the maintainability of our API generation is to test more. It gives us confidence we aren't breaking things and it makes it more obvious how the function behaves. This PR also makes some light refactoring like renaming things to be more clear. --------- Co-authored-by: Arnau Casau Co-authored-by: Arnau Casau <47946624+arnaucasau@users.noreply.github.com> --- docs/api/qiskit-ibm-provider/index.md | 2 - docs/api/qiskit-ibm-runtime/0.14/index.md | 2 - docs/api/qiskit-ibm-runtime/0.15/index.md | 2 - docs/api/qiskit-ibm-runtime/0.16/index.md | 2 - docs/api/qiskit-ibm-runtime/index.md | 2 - docs/api/qiskit/release-notes/0.45.md | 2 +- scripts/commands/updateApiDocs.ts | 14 +-- ...ts.test.ts => downloadCIArtifacts.test.ts} | 5 +- ...oadArtifacts.ts => downloadCIArtifacts.ts} | 2 +- scripts/lib/fs.test.ts | 26 +++++ scripts/lib/sphinx/addFrontMatter.test.ts | 97 +++++++++++++++++++ scripts/lib/sphinx/addFrontMatter.ts | 16 ++- ...unifiedParser.ts => commonParserConfig.ts} | 0 ...edupeIds.test.ts => dedupeHtmlIds.test.ts} | 33 +++---- .../sphinx/{dedupeIds.ts => dedupeHtmlIds.ts} | 8 +- scripts/lib/sphinx/flattenFolders.test.ts | 33 +++++++ .../{flatFolders.ts => flattenFolders.ts} | 4 +- scripts/lib/sphinx/mergeClassMembers.ts | 2 +- scripts/lib/sphinx/specialCaseResults.test.ts | 57 +++++++++++ scripts/lib/sphinx/specialCaseResults.ts | 22 ++--- scripts/lib/sphinx/sphinxHtmlToMarkdown.ts | 2 +- scripts/lib/sphinx/updateLinks.ts | 2 +- scripts/lib/stringUtils.test.ts | 51 ++++++++++ 23 files changed, 323 insertions(+), 63 deletions(-) rename scripts/lib/{downloadArtifacts.test.ts => downloadCIArtifacts.test.ts} (90%) rename scripts/lib/{downloadArtifacts.ts => downloadCIArtifacts.ts} (95%) create mode 100644 scripts/lib/fs.test.ts create mode 100644 scripts/lib/sphinx/addFrontMatter.test.ts rename scripts/lib/sphinx/{unifiedParser.ts => commonParserConfig.ts} (100%) rename scripts/lib/sphinx/{dedupeIds.test.ts => dedupeHtmlIds.test.ts} (58%) rename scripts/lib/sphinx/{dedupeIds.ts => dedupeHtmlIds.ts} (89%) create mode 100644 scripts/lib/sphinx/flattenFolders.test.ts rename scripts/lib/sphinx/{flatFolders.ts => flattenFolders.ts} (88%) create mode 100644 scripts/lib/sphinx/specialCaseResults.test.ts create mode 100644 scripts/lib/stringUtils.test.ts diff --git a/docs/api/qiskit-ibm-provider/index.md b/docs/api/qiskit-ibm-provider/index.md index 6f01462d1dd..fd5f3e44452 100644 --- a/docs/api/qiskit-ibm-provider/index.md +++ b/docs/api/qiskit-ibm-provider/index.md @@ -1,8 +1,6 @@ --- - title: Qiskit IBM Provider API Docs description: API documentation for qiskit-ibm-provider - --- # qiskit-ibm-provider API Reference diff --git a/docs/api/qiskit-ibm-runtime/0.14/index.md b/docs/api/qiskit-ibm-runtime/0.14/index.md index 3e1649a1f31..a8c367137ac 100644 --- a/docs/api/qiskit-ibm-runtime/0.14/index.md +++ b/docs/api/qiskit-ibm-runtime/0.14/index.md @@ -1,8 +1,6 @@ --- - title: Qiskit Runtime IBM Client API Docs description: API documentation for qiskit-ibm-runtime - --- # qiskit-ibm-runtime API reference diff --git a/docs/api/qiskit-ibm-runtime/0.15/index.md b/docs/api/qiskit-ibm-runtime/0.15/index.md index 3e1649a1f31..a8c367137ac 100644 --- a/docs/api/qiskit-ibm-runtime/0.15/index.md +++ b/docs/api/qiskit-ibm-runtime/0.15/index.md @@ -1,8 +1,6 @@ --- - title: Qiskit Runtime IBM Client API Docs description: API documentation for qiskit-ibm-runtime - --- # qiskit-ibm-runtime API reference diff --git a/docs/api/qiskit-ibm-runtime/0.16/index.md b/docs/api/qiskit-ibm-runtime/0.16/index.md index 3e1649a1f31..a8c367137ac 100644 --- a/docs/api/qiskit-ibm-runtime/0.16/index.md +++ b/docs/api/qiskit-ibm-runtime/0.16/index.md @@ -1,8 +1,6 @@ --- - title: Qiskit Runtime IBM Client API Docs description: API documentation for qiskit-ibm-runtime - --- # qiskit-ibm-runtime API reference diff --git a/docs/api/qiskit-ibm-runtime/index.md b/docs/api/qiskit-ibm-runtime/index.md index 3e1649a1f31..a8c367137ac 100644 --- a/docs/api/qiskit-ibm-runtime/index.md +++ b/docs/api/qiskit-ibm-runtime/index.md @@ -1,8 +1,6 @@ --- - title: Qiskit Runtime IBM Client API Docs description: API documentation for qiskit-ibm-runtime - --- # qiskit-ibm-runtime API reference diff --git a/docs/api/qiskit/release-notes/0.45.md b/docs/api/qiskit/release-notes/0.45.md index c1abd365d3a..a3193ed2734 100644 --- a/docs/api/qiskit/release-notes/0.45.md +++ b/docs/api/qiskit/release-notes/0.45.md @@ -1,6 +1,6 @@ --- title: Qiskit 0.45 release notes -description: Changes made to Qiskit +description: Changes made in Qiskit 0.45 in_page_toc_max_heading_level: 2 --- diff --git a/scripts/commands/updateApiDocs.ts b/scripts/commands/updateApiDocs.ts index 858c5dd0eb6..da399f6171e 100644 --- a/scripts/commands/updateApiDocs.ts +++ b/scripts/commands/updateApiDocs.ts @@ -23,17 +23,17 @@ import { saveImages } from "../lib/saveImages"; import { generateToc } from "../lib/sphinx/generateToc"; import { SphinxToMdResult } from "../lib/sphinx/SphinxToMdResult"; import { mergeClassMembers } from "../lib/sphinx/mergeClassMembers"; -import { flatFolders } from "../lib/sphinx/flatFolders"; +import flattenFolders from "../lib/sphinx/flattenFolders"; import { updateLinks } from "../lib/sphinx/updateLinks"; -import specialCaseResults from "../lib/sphinx/specialCaseResults"; -import { addFrontMatter } from "../lib/sphinx/addFrontMatter"; -import { dedupeResultIds } from "../lib/sphinx/dedupeIds"; +import { specialCaseResults } from "../lib/sphinx/specialCaseResults"; +import addFrontMatter from "../lib/sphinx/addFrontMatter"; +import { dedupeHtmlIdsFromResults } from "../lib/sphinx/dedupeHtmlIds"; import { removePrefix, removeSuffix } from "../lib/stringUtils"; import yargs from "yargs/yargs"; import { hideBin } from "yargs/helpers"; import { Pkg, PkgInfo, Link } from "../lib/sharedTypes"; import transformLinks from "transform-markdown-links"; -import { downloadCIArtifact } from "../lib/downloadArtifacts"; +import { downloadCIArtifact } from "../lib/downloadCIArtifacts"; import { findLegacyReleaseNotes, addNewReleaseNotes, @@ -272,10 +272,10 @@ async function convertHtmlToMarkdown( } results = await mergeClassMembers(results); - flatFolders(results); + flattenFolders(results); specialCaseResults(results); await updateLinks(results, pkg.transformLink); - await dedupeResultIds(results); + await dedupeHtmlIdsFromResults(results); addFrontMatter(results, pkg); for (const result of results) { diff --git a/scripts/lib/downloadArtifacts.test.ts b/scripts/lib/downloadCIArtifacts.test.ts similarity index 90% rename from scripts/lib/downloadArtifacts.test.ts rename to scripts/lib/downloadCIArtifacts.test.ts index 7fbf7c842cc..be7c4b15dab 100644 --- a/scripts/lib/downloadArtifacts.test.ts +++ b/scripts/lib/downloadCIArtifacts.test.ts @@ -11,9 +11,10 @@ // that they have been altered from the originals. import { describe, expect, test } from "@jest/globals"; -import { getArtifactID } from "./downloadArtifacts"; -describe("downloadArtifacts", () => { +import { getArtifactID } from "./downloadCIArtifacts"; + +describe("getArtifactID()", () => { test("Pass the full URL of the artifact", () => { const artifactID = getArtifactID( "https://github.com/Qiskit/qiskit/suites/17881600359/artifacts/1026798160", diff --git a/scripts/lib/downloadArtifacts.ts b/scripts/lib/downloadCIArtifacts.ts similarity index 95% rename from scripts/lib/downloadArtifacts.ts rename to scripts/lib/downloadCIArtifacts.ts index f0abab494a8..4bb138397be 100644 --- a/scripts/lib/downloadArtifacts.ts +++ b/scripts/lib/downloadCIArtifacts.ts @@ -11,7 +11,7 @@ // that they have been altered from the originals. import { $ } from "zx"; -import { pathExists, getRoot } from "../lib/fs"; +import { pathExists, getRoot } from "./fs"; import { mkdirp } from "mkdirp"; export function getArtifactID(url: string) { diff --git a/scripts/lib/fs.test.ts b/scripts/lib/fs.test.ts new file mode 100644 index 00000000000..f744a21be5b --- /dev/null +++ b/scripts/lib/fs.test.ts @@ -0,0 +1,26 @@ +// This code is a Qiskit project. +// +// (C) Copyright IBM 2024. +// +// This code is licensed under the Apache License, Version 2.0. You may +// obtain a copy of this license in the LICENSE file in the root directory +// of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. +// +// Any modifications or derivative works of this code must retain this +// copyright notice, and modified files need to carry a notice indicating +// that they have been altered from the originals. + +import { expect, test } from "@jest/globals"; + +import { getRoot, pathExists } from "./fs"; + +test("pathExists() with getRoot()", async () => { + const readme = await pathExists(`${getRoot()}/README.md`); + expect(readme).toBe(true); + + const dir = await pathExists(`${getRoot()}/docs/`); + expect(dir).toBe(true); + + const fake = await pathExists(`${getRoot()}/fake-file`); + expect(fake).toBe(false); +}); diff --git a/scripts/lib/sphinx/addFrontMatter.test.ts b/scripts/lib/sphinx/addFrontMatter.test.ts new file mode 100644 index 00000000000..06ddecf4418 --- /dev/null +++ b/scripts/lib/sphinx/addFrontMatter.test.ts @@ -0,0 +1,97 @@ +// This code is a Qiskit project. +// +// (C) Copyright IBM 2024. +// +// This code is licensed under the Apache License, Version 2.0. You may +// obtain a copy of this license in the LICENSE file in the root directory +// of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. +// +// Any modifications or derivative works of this code must retain this +// copyright notice, and modified files need to carry a notice indicating +// that they have been altered from the originals. + +import { expect, test } from "@jest/globals"; + +import addFrontMatter from "./addFrontMatter"; +import { Pkg } from "../sharedTypes"; +import { SphinxToMdResult } from "./SphinxToMdResult"; + +test("addFrontMatter()", () => { + const results: SphinxToMdResult[] = [ + { + markdown: "# Hardcoded!", + meta: { + hardcoded_frontmatter: "title: hardcoded\ndescription: Hello world!", + }, + images: [], + isReleaseNotes: false, + }, + { + markdown: "# Hardcoded with python_api_name!", + meta: { + hardcoded_frontmatter: + "title: hardcoded with python_api_name\ndescription: Hello world!", + python_api_name: "quantum_software.MyClass", + python_api_type: "class", + }, + images: [], + isReleaseNotes: true, + }, + { + markdown: "# Python API", + meta: { + python_api_name: "quantum_software.MyClass", + python_api_type: "class", + }, + images: [], + isReleaseNotes: false, + }, + { + markdown: "# Some release notes!", + meta: {}, + images: [], + isReleaseNotes: true, + }, + ]; + const pkg = { + versionWithoutPatch: "0.0", + hasSeparateReleaseNotes: true, + title: "My Quantum Software Project", + } as Pkg; + + addFrontMatter(results, pkg); + expect(results.map((result) => result.markdown)).toEqual([ + `--- +title: hardcoded +description: Hello world! +--- + +# Hardcoded! +`, + `--- +title: hardcoded with python_api_name +description: Hello world! +--- + +# Hardcoded with python_api_name! +`, + `--- +title: MyClass +description: API reference for quantum_software.MyClass +in_page_toc_min_heading_level: 1 +python_api_type: class +python_api_name: quantum_software.MyClass +--- + +# Python API +`, + `--- +title: My Quantum Software Project 0.0 release notes +description: Changes made in My Quantum Software Project 0.0 +in_page_toc_max_heading_level: 2 +--- + +# Some release notes! +`, + ]); +}); diff --git a/scripts/lib/sphinx/addFrontMatter.ts b/scripts/lib/sphinx/addFrontMatter.ts index 273b0a0d5f3..f5cd35648b2 100644 --- a/scripts/lib/sphinx/addFrontMatter.ts +++ b/scripts/lib/sphinx/addFrontMatter.ts @@ -14,7 +14,7 @@ import { getLastPartFromFullIdentifier } from "../stringUtils"; import { SphinxToMdResult } from "./SphinxToMdResult"; import { Pkg } from "../sharedTypes"; -export function addFrontMatter( +function addFrontMatter( results: T[], pkg: Pkg, ): void { @@ -39,11 +39,15 @@ python_api_name: ${result.meta.python_api_name} ${markdown} `; } else if (result.isReleaseNotes) { + const versionStr = pkg.hasSeparateReleaseNotes + ? ` ${pkg.versionWithoutPatch}` + : ""; + const descriptionSuffix = pkg.hasSeparateReleaseNotes + ? `in ${pkg.title}${versionStr}` + : `to ${pkg.title}`; result.markdown = `--- -title: ${pkg.title}${ - pkg.hasSeparateReleaseNotes ? " " + pkg.versionWithoutPatch : "" - } release notes -description: Changes made to ${pkg.title} +title: ${pkg.title}${versionStr} release notes +description: Changes made ${descriptionSuffix} in_page_toc_max_heading_level: 2 --- @@ -52,3 +56,5 @@ ${markdown} } } } + +export default addFrontMatter; diff --git a/scripts/lib/sphinx/unifiedParser.ts b/scripts/lib/sphinx/commonParserConfig.ts similarity index 100% rename from scripts/lib/sphinx/unifiedParser.ts rename to scripts/lib/sphinx/commonParserConfig.ts diff --git a/scripts/lib/sphinx/dedupeIds.test.ts b/scripts/lib/sphinx/dedupeHtmlIds.test.ts similarity index 58% rename from scripts/lib/sphinx/dedupeIds.test.ts rename to scripts/lib/sphinx/dedupeHtmlIds.test.ts index 905ad31d12f..4ac76c386d8 100644 --- a/scripts/lib/sphinx/dedupeIds.test.ts +++ b/scripts/lib/sphinx/dedupeHtmlIds.test.ts @@ -10,23 +10,22 @@ // copyright notice, and modified files need to carry a notice indicating // that they have been altered from the originals. -import { describe, expect, test } from "@jest/globals"; -import { dedupeIds } from "./dedupeIds"; +import { expect, test } from "@jest/globals"; -describe("dedupeIds", () => { - test("dedupeIds", async () => { - expect( - await dedupeIds(` - - - # foo - - `), - ).toMatchInlineSnapshot(` - " +import { dedupeHtmlIds } from "./dedupeHtmlIds"; + +test("dedupeHtmlIds()", async () => { + expect( + await dedupeHtmlIds(` + + + # foo + + `), + ).toMatchInlineSnapshot(` + " - # foo - " - `); - }); + # foo + " + `); }); diff --git a/scripts/lib/sphinx/dedupeIds.ts b/scripts/lib/sphinx/dedupeHtmlIds.ts similarity index 89% rename from scripts/lib/sphinx/dedupeIds.ts rename to scripts/lib/sphinx/dedupeHtmlIds.ts index 78faadf3b46..83c880fcff0 100644 --- a/scripts/lib/sphinx/dedupeIds.ts +++ b/scripts/lib/sphinx/dedupeHtmlIds.ts @@ -18,20 +18,20 @@ import remarkMdx from "remark-mdx"; import { Root } from "mdast"; import { visit } from "unist-util-visit"; import remarkStringify from "remark-stringify"; -import { remarkStringifyOptions } from "./unifiedParser"; +import { remarkStringifyOptions } from "./commonParserConfig"; import { toText } from "hast-util-to-text"; import Slugger from "github-slugger"; import { SphinxToMdResult } from "./SphinxToMdResult"; -export async function dedupeResultIds( +export async function dedupeHtmlIdsFromResults( results: SphinxToMdResult[], ): Promise { for (let result of results) { - result.markdown = await dedupeIds(result.markdown); + result.markdown = await dedupeHtmlIds(result.markdown); } } -export async function dedupeIds(md: string): Promise { +export async function dedupeHtmlIds(md: string): Promise { const output = await unified() .use(remarkParse) .use(remarkMath) diff --git a/scripts/lib/sphinx/flattenFolders.test.ts b/scripts/lib/sphinx/flattenFolders.test.ts new file mode 100644 index 00000000000..f8d96ec7cfa --- /dev/null +++ b/scripts/lib/sphinx/flattenFolders.test.ts @@ -0,0 +1,33 @@ +// This code is a Qiskit project. +// +// (C) Copyright IBM 2024. +// +// This code is licensed under the Apache License, Version 2.0. You may +// obtain a copy of this license in the LICENSE file in the root directory +// of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. +// +// Any modifications or derivative works of this code must retain this +// copyright notice, and modified files need to carry a notice indicating +// that they have been altered from the originals. + +import { expect, test } from "@jest/globals"; + +import flattenFolders from "./flattenFolders"; +import { SphinxToMdResultWithUrl } from "./SphinxToMdResult"; + +test("flattenFolders()", () => { + const results = [ + { url: "/api/my-pkg/apidoc/my_module" }, + { url: "/api/my-pkg/apidocs/my_module2" }, + { url: "/api/my-pkg/stubs/my_module.foo.Bar" }, + { url: "/api/my-pkg/release_notes" }, + ] as SphinxToMdResultWithUrl[]; + + flattenFolders(results); + expect(results.map((result) => result.url)).toEqual([ + "/api/my-pkg/my_module", + "/api/my-pkg/my_module2", + "/api/my-pkg/my_module.foo.Bar", + "/api/my-pkg/release_notes", + ]); +}); diff --git a/scripts/lib/sphinx/flatFolders.ts b/scripts/lib/sphinx/flattenFolders.ts similarity index 88% rename from scripts/lib/sphinx/flatFolders.ts rename to scripts/lib/sphinx/flattenFolders.ts index 4b1c64e0e99..e3635892eeb 100644 --- a/scripts/lib/sphinx/flatFolders.ts +++ b/scripts/lib/sphinx/flattenFolders.ts @@ -13,7 +13,7 @@ import { SphinxToMdResultWithUrl } from "./SphinxToMdResult"; import { removePart } from "../stringUtils"; -export function flatFolders(results: SphinxToMdResultWithUrl[]): void { +function flattenFolders(results: SphinxToMdResultWithUrl[]): void { for (const result of results) { result.url = omitRootFolders(result.url); } @@ -22,3 +22,5 @@ export function flatFolders(results: SphinxToMdResultWithUrl[]): void { function omitRootFolders(path: string): string { return removePart(path, "/", ["stubs", "apidocs", "apidoc"]); } + +export default flattenFolders; diff --git a/scripts/lib/sphinx/mergeClassMembers.ts b/scripts/lib/sphinx/mergeClassMembers.ts index 6493eb476f0..1b9b758aa40 100644 --- a/scripts/lib/sphinx/mergeClassMembers.ts +++ b/scripts/lib/sphinx/mergeClassMembers.ts @@ -20,7 +20,7 @@ import remarkStringify from "remark-stringify"; import { Content, Root } from "mdast"; import { visit } from "unist-util-visit"; import { SphinxToMdResultWithUrl } from "./SphinxToMdResult"; -import { remarkStringifyOptions } from "./unifiedParser"; +import { remarkStringifyOptions } from "./commonParserConfig"; export async function mergeClassMembers( results: T[], diff --git a/scripts/lib/sphinx/specialCaseResults.test.ts b/scripts/lib/sphinx/specialCaseResults.test.ts new file mode 100644 index 00000000000..fb933e35faf --- /dev/null +++ b/scripts/lib/sphinx/specialCaseResults.test.ts @@ -0,0 +1,57 @@ +// This code is a Qiskit project. +// +// (C) Copyright IBM 2024. +// +// This code is licensed under the Apache License, Version 2.0. You may +// obtain a copy of this license in the LICENSE file in the root directory +// of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. +// +// Any modifications or derivative works of this code must retain this +// copyright notice, and modified files need to carry a notice indicating +// that they have been altered from the originals. + +import { expect, test } from "@jest/globals"; + +import { + specialCaseResults, + PROVIDER_INDEX_META, + RUNTIME_INDEX_META, +} from "./specialCaseResults"; +import { SphinxToMdResultWithUrl } from "./SphinxToMdResult"; + +test("specialCaseResults()", () => { + const results = [ + { url: "/api/my-pkg/release_notes" }, + { url: "/api/my-pkg/terra" }, + { + url: "/api/my-pkg/ibm-runtime", + meta: {}, + }, + { + url: "/api/my-pkg/ibm-provider", + meta: {}, + }, + { url: "/api/my-pkg/other-file" }, + ] as SphinxToMdResultWithUrl[]; + + specialCaseResults(results); + expect(results).toEqual([ + { + url: "/api/my-pkg/release-notes", + }, + { + url: "/api/my-pkg/index", + }, + { + url: "/api/my-pkg/index", + meta: { hardcoded_frontmatter: RUNTIME_INDEX_META }, + }, + { + url: "/api/my-pkg/index", + meta: { hardcoded_frontmatter: PROVIDER_INDEX_META }, + }, + { + url: "/api/my-pkg/other-file", + }, + ]); +}); diff --git a/scripts/lib/sphinx/specialCaseResults.ts b/scripts/lib/sphinx/specialCaseResults.ts index fe74b1b6c85..70fd78430f0 100644 --- a/scripts/lib/sphinx/specialCaseResults.ts +++ b/scripts/lib/sphinx/specialCaseResults.ts @@ -12,10 +12,16 @@ import { SphinxToMdResultWithUrl } from "./SphinxToMdResult"; -function specialCaseResults(results: SphinxToMdResultWithUrl[]): void { +export const RUNTIME_INDEX_META = `title: Qiskit Runtime IBM Client API Docs +description: API documentation for qiskit-ibm-runtime`; + +export const PROVIDER_INDEX_META = `title: Qiskit IBM Provider API Docs +description: API documentation for qiskit-ibm-provider`; + +export function specialCaseResults(results: SphinxToMdResultWithUrl[]): void { for (let result of results) { // We use `-` rather than `_` as our delimiter. - if (result.url.endsWith("release_notes")) { + if (result.url.endsWith("/release_notes")) { result.url = result.url.replace(/\/release_notes$/g, "/release-notes"); } @@ -27,23 +33,15 @@ function specialCaseResults(results: SphinxToMdResultWithUrl[]): void { if (result.url.endsWith("/ibm-provider")) { result.url = result.url.replace(/\/ibm-provider$/g, "/index"); result.meta = { - hardcoded_frontmatter: ` -title: Qiskit IBM Provider API Docs -description: API documentation for qiskit-ibm-provider -`, + hardcoded_frontmatter: PROVIDER_INDEX_META, }; } if (result.url.endsWith("/ibm-runtime")) { result.url = result.url.replace(/\/ibm-runtime$/g, "/index"); result.meta = { - hardcoded_frontmatter: ` -title: Qiskit Runtime IBM Client API Docs -description: API documentation for qiskit-ibm-runtime -`, + hardcoded_frontmatter: RUNTIME_INDEX_META, }; } } } - -export default specialCaseResults; diff --git a/scripts/lib/sphinx/sphinxHtmlToMarkdown.ts b/scripts/lib/sphinx/sphinxHtmlToMarkdown.ts index 5dc83d4b3ae..659967bb21a 100644 --- a/scripts/lib/sphinx/sphinxHtmlToMarkdown.ts +++ b/scripts/lib/sphinx/sphinxHtmlToMarkdown.ts @@ -28,7 +28,7 @@ import { removePrefix, removeSuffix, } from "../stringUtils"; -import { remarkStringifyOptions } from "./unifiedParser"; +import { remarkStringifyOptions } from "./commonParserConfig"; import { MdxJsxFlowElement } from "mdast-util-mdx-jsx"; import { visit } from "unist-util-visit"; import { Root } from "mdast"; diff --git a/scripts/lib/sphinx/updateLinks.ts b/scripts/lib/sphinx/updateLinks.ts index 4979d51bde2..33f13fe3d2b 100644 --- a/scripts/lib/sphinx/updateLinks.ts +++ b/scripts/lib/sphinx/updateLinks.ts @@ -16,7 +16,7 @@ import { visit } from "unist-util-visit"; import isAbsoluteUrl from "is-absolute-url"; import { removePart, removePrefix } from "../stringUtils"; import { SphinxToMdResultWithUrl } from "./SphinxToMdResult"; -import { remarkStringifyOptions } from "./unifiedParser"; +import { remarkStringifyOptions } from "./commonParserConfig"; import { unified } from "unified"; import remarkParse from "remark-parse"; import remarkMath from "remark-math"; diff --git a/scripts/lib/stringUtils.test.ts b/scripts/lib/stringUtils.test.ts new file mode 100644 index 00000000000..3b42d11cef3 --- /dev/null +++ b/scripts/lib/stringUtils.test.ts @@ -0,0 +1,51 @@ +// This code is a Qiskit project. +// +// (C) Copyright IBM 2024. +// +// This code is licensed under the Apache License, Version 2.0. You may +// obtain a copy of this license in the LICENSE file in the root directory +// of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. +// +// Any modifications or derivative works of this code must retain this +// copyright notice, and modified files need to carry a notice indicating +// that they have been altered from the originals. + +import { expect, test } from "@jest/globals"; + +import { + removePart, + removePrefix, + removeSuffix, + getLastPartFromFullIdentifier, +} from "./stringUtils"; + +test("removePart()", () => { + const input = "a|b|b|x|z"; + expect(removePart(input, "|", ["a", "b"])).toEqual("x|z"); + expect(removePart(input, "|", [])).toEqual(input); + expect(removePart(input, "|", ["m"])).toEqual(input); +}); + +test("removePrefix()", () => { + const input = "hello world!"; + expect(removePrefix(input, "hello")).toEqual(" world!"); + expect(removePrefix(input, "hola")).toEqual(input); + expect(removePrefix(input, "ello")).toEqual(input); +}); + +test("removeSuffix()", () => { + const input = "hello world!"; + expect(removeSuffix(input, "world!")).toEqual("hello "); + expect(removeSuffix(input, "mundo!")).toEqual(input); + expect(removeSuffix(input, "worl")).toEqual(input); +}); + +test("getLastPartFromFullIdentifier", () => { + expect(getLastPartFromFullIdentifier("my_software.foo.Bar.method")).toEqual( + "method", + ); + expect(getLastPartFromFullIdentifier("my_software.foo.Bar")).toEqual("Bar"); + expect(getLastPartFromFullIdentifier("my_software.foo")).toEqual("foo"); + expect(getLastPartFromFullIdentifier("my_software")).toEqual("my_software"); + expect(getLastPartFromFullIdentifier("")).toEqual(""); +});