microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
source/playground/src/docs.tsx
57lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | import { IDocFile } from "qsharp-lang"; |
| 5 | import { Markdown } from "qsharp-lang/ux"; |
| 6 | |
| 7 | export function getNamespaces( |
| 8 | documentation: Map<string, string> | undefined, |
| 9 | ): string[] { |
| 10 | if (documentation) { |
| 11 | return Array.from(documentation.keys()); |
| 12 | } |
| 13 | return new Array<string>(); |
| 14 | } |
| 15 | |
| 16 | // Takes array of documents (containing data for each item in the standard library) |
| 17 | // and creates a documentation map, which maps from a namespace |
| 18 | // to the combined HTML-formatted documentation for all items in that namespace. |
| 19 | export function processDocumentFiles( |
| 20 | docFiles: IDocFile[], |
| 21 | ): Map<string, string> { |
| 22 | const contentByNamespace = new Map<string, string>(); |
| 23 | const regex = new RegExp( |
| 24 | "^qsharp\\.namespace: (Microsoft\\.Quantum|Std)\\.(.+)$", |
| 25 | "m", |
| 26 | ); |
| 27 | |
| 28 | for (const doc of docFiles) { |
| 29 | const match = regex.exec(doc.metadata); // Parse namespace out of metadata |
| 30 | if (match == null) { |
| 31 | continue; // Skip items with non-parsable metadata |
| 32 | } |
| 33 | // The next line contains "Zero-width space" unicode character |
| 34 | // to allow line breaks before the period. |
| 35 | const newNamespace = "… " + match[2].replace(".", "."); |
| 36 | |
| 37 | if (contentByNamespace.has(newNamespace)) { |
| 38 | const existingContent = contentByNamespace.get(newNamespace)!; |
| 39 | contentByNamespace.set( |
| 40 | newNamespace, |
| 41 | existingContent + "\n<br>\n<br>\n\n" + doc.contents, |
| 42 | ); |
| 43 | } else { |
| 44 | contentByNamespace.set(newNamespace, doc.contents); |
| 45 | } |
| 46 | } |
| 47 | return contentByNamespace; |
| 48 | } |
| 49 | |
| 50 | export function DocumentationDisplay(props: { |
| 51 | currentNamespace: string; |
| 52 | documentation: Map<string, string> | undefined; |
| 53 | }) { |
| 54 | const docsMd = props.documentation?.get(props.currentNamespace) ?? ""; |
| 55 | |
| 56 | return <Markdown markdown={docsMd} />; |
| 57 | } |
| 58 | |