]> Git Repo - pico-vscode.git/blob - src/commands/openSdkDocumentation.mts
New example pico project command + custom create from example ui
[pico-vscode.git] / src / commands / openSdkDocumentation.mts
1 /* eslint-disable max-len */
2 import { CommandWithArgs } from "./command.mjs";
3 import Logger from "../logger.mjs";
4 import { type QuickPickItem, ViewColumn, window, Uri } from "vscode";
5 import { readFileSync } from "fs";
6
7 export enum DocumentationId {
8   hardware = 0,
9   highLevel = 1,
10   networking = 2,
11   runtimeInfrastructure = 3,
12 }
13
14 export const DOCUMENTATION_LABEL_BY_ID: string[] = [
15   "Hardware APIs",
16   "High Level APIs",
17   "Networking Libraries",
18   "Runtime Infrastructure",
19 ];
20
21 // eslint-disable-next-line @typescript-eslint/no-unused-vars
22 const DOCUMENTATION_URLS_BY_ID: string[] = [
23   //"https://raspberrypi.github.io/pico-sdk-doxygen/index.html",
24   "https://www.raspberrypi.com/documentation/pico-sdk/hardware.html",
25   "https://www.raspberrypi.com/documentation/pico-sdk/high_level.html",
26   "https://www.raspberrypi.com/documentation/pico-sdk/networking.html",
27   "https://www.raspberrypi.com/documentation/pico-sdk/runtime.html",
28 ];
29
30 const LOCAL_DOCUMENTATION_URLS_BY_ID: string[] = [
31   "hardware.html",
32   "high_level.html",
33   "networking.html",
34   "runtime.html",
35 ];
36
37 export default class OpenSdkDocumentationCommand extends CommandWithArgs {
38   private _logger: Logger = new Logger("OpenSdkDocumentationCommand");
39
40   public static readonly id = "openSdkDocumentation";
41
42   constructor(private readonly _extensionUri: Uri) {
43     super(OpenSdkDocumentationCommand.id);
44   }
45
46   async execute(docId?: DocumentationId): Promise<void> {
47     let url = docId !== undefined ? LOCAL_DOCUMENTATION_URLS_BY_ID[docId] : "";
48     if (docId === undefined) {
49       const options: QuickPickItem[] = DOCUMENTATION_LABEL_BY_ID.map(
50         (label, index) => ({
51           label,
52           detail: LOCAL_DOCUMENTATION_URLS_BY_ID[index],
53         })
54       );
55
56       const result = await window.showQuickPick(options, {
57         placeHolder: "Select Category",
58         canPickMany: false,
59         ignoreFocusOut: false,
60       });
61
62       if (result === undefined || result.detail === undefined) {
63         return;
64       }
65
66       url = result.detail;
67     }
68
69     // show webview
70     this._logger.info("Opening SDK documentation in browser...");
71     const panel = window.createWebviewPanel(
72       "pico-sdk-documentation",
73       "Pico SDK Documentation",
74       { viewColumn: ViewColumn.Two, preserveFocus: false },
75       {
76         enableScripts: true,
77         retainContextWhenHidden: true,
78         enableFindWidget: true,
79         localResourceRoots: [Uri.joinPath(this._extensionUri, "web", "docs")],
80         enableCommandUris: true,
81       }
82     );
83
84     panel.webview.html = this._getHtmlForWebview(
85       Uri.joinPath(this._extensionUri, "web", "docs", url).fsPath,
86       panel.webview
87         .asWebviewUri(
88           Uri.joinPath(this._extensionUri, "web", "docs", "docstyle.css")
89         )
90         .toString(),
91       panel.webview.cspSource
92     );
93     panel.reveal();
94   }
95
96   private _getHtmlForWebview(
97     url: string,
98     docstyle: string,
99     cspSource: string
100   ): string {
101     const regex = /<a\s+(.*?)\s?href=".+(#[^"]+)"/g;
102
103     // load from file
104     return (
105       readFileSync(url)
106         .toString("utf-8")
107         .replace(/{{docstyle}}/g, docstyle)
108         /*.replace(/{{jquery}}/g, jquery)
109       .replace(/{{jquery-ui}}/g, jqueryUi)
110       .replace(/{{jquery-tocify}}/g, jqueryTocify)
111       .replace(/{{nonce}}/g, nonce)*/
112         .replace(/{{cspSource}}/g, cspSource)
113         .replace(regex, '<a $1 href="$2"')
114     );
115   }
116 }
This page took 0.035614 seconds and 4 git commands to generate.