forked from Qiskit/documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract markdown reading into dedicated file (Qiskit#1666)
We're going to reuse this logic in Qiskit#1651, so it's helpful to have in a common helper file.
- Loading branch information
1 parent
735b1d6
commit 5706804
Showing
4 changed files
with
86 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// 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 { markdownFromNotebook } from "./markdownReader"; | ||
|
||
test("markdownFromNotebook()", () => { | ||
const result = markdownFromNotebook(` | ||
{ | ||
"cells": [ | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Line 1.\\n", | ||
"Line 2." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Line 3." | ||
] | ||
} | ||
], | ||
"metadata": {} | ||
} | ||
`); | ||
expect(result).toBe("Line 1.\nLine 2.\n\nLine 3."); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// 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 { readFile } from "fs/promises"; | ||
import path from "node:path"; | ||
|
||
export async function readMarkdown(filePath: string): Promise<string> { | ||
const source = await readFile(filePath, { encoding: "utf8" }); | ||
return path.extname(filePath) === ".ipynb" | ||
? markdownFromNotebook(source) | ||
: source; | ||
} | ||
|
||
interface JupyterCell { | ||
cell_type: string; | ||
source: string[]; | ||
} | ||
|
||
export function markdownFromNotebook(rawContent: string): string { | ||
const cells = JSON.parse(rawContent).cells as JupyterCell[]; | ||
return cells | ||
.filter((cell) => cell.cell_type === "markdown") | ||
.map((cell) => cell.source.join("")) | ||
.join("\n\n"); | ||
} |