1 /* eslint-disable max-len */
2 import { CommandWithArgs } from "./command.mjs";
3 import Logger from "../logger.mjs";
11 import { readFileSync } from "fs";
13 export enum DocumentationId {
17 runtimeInfrastructure = 3,
20 export const DOCUMENTATION_LABEL_BY_ID: string[] = [
23 "Networking Libraries",
24 "Runtime Infrastructure",
27 // eslint-disable-next-line @typescript-eslint/no-unused-vars
28 const DOCUMENTATION_URLS_BY_ID: string[] = [
29 //"https://raspberrypi.github.io/pico-sdk-doxygen/index.html",
30 "https://www.raspberrypi.com/documentation/pico-sdk/hardware.html",
31 "https://www.raspberrypi.com/documentation/pico-sdk/high_level.html",
32 "https://www.raspberrypi.com/documentation/pico-sdk/networking.html",
33 "https://www.raspberrypi.com/documentation/pico-sdk/runtime.html",
36 const LOCAL_DOCUMENTATION_URLS_BY_ID: string[] = [
37 "group__hardware.html",
38 "group__high__level.html",
39 "group__networking.html",
40 "group__runtime.html",
43 export default class OpenSdkDocumentationCommand extends CommandWithArgs {
44 private _logger: Logger = new Logger("OpenSdkDocumentationCommand");
46 public static readonly id = "openSdkDocumentation";
47 private _panel: WebviewPanel | undefined = undefined;
49 constructor(private readonly _extensionUri: Uri) {
50 super(OpenSdkDocumentationCommand.id);
53 async execute(docId?: DocumentationId, fileName?: string): Promise<void> {
55 if (typeof docId === "number") {
56 url = docId !== undefined ? LOCAL_DOCUMENTATION_URLS_BY_ID[docId] : "";
57 } else if (fileName !== undefined) {
60 const options: QuickPickItem[] = DOCUMENTATION_LABEL_BY_ID.map(
63 detail: LOCAL_DOCUMENTATION_URLS_BY_ID[index],
67 const result = await window.showQuickPick(options, {
68 placeHolder: "Select Category",
70 ignoreFocusOut: false,
73 if (result === undefined || result.detail === undefined) {
81 this._logger.info("Opening SDK documentation in browser...");
82 if (this._panel === undefined) {
83 Logger.log("New panel");
84 this._panel = window.createWebviewPanel(
85 "pico-sdk-documentation",
86 "Pico SDK Documentation",
87 { viewColumn: ViewColumn.Two, preserveFocus: false },
90 retainContextWhenHidden: true,
91 enableFindWidget: true,
92 localResourceRoots: [Uri.joinPath(this._extensionUri, "web", "docs")],
93 enableCommandUris: true,
98 const panel = this._panel;
100 panel.webview.html = this._getHtmlForWebview(
101 Uri.joinPath(this._extensionUri, "web", "docs", url).fsPath,
102 panel.webview.cspSource,
108 // dispose when hidden
109 panel.onDidDispose(() => {
110 this._panel = undefined;
114 private _getHtmlForWebview(
120 const regexa = /<a\s+(.*?)?href="([^"]+)"/g;
121 const regexsrc = /src="([^"]+)"/g;
122 const regexcss = /<link href="([^"]+)" rel="stylesheet"/g;
128 /*.replace(/{{jquery}}/g, jquery)
129 .replace(/{{jquery-ui}}/g, jqueryUi)
130 .replace(/{{jquery-tocify}}/g, jqueryTocify)
131 .replace(/{{nonce}}/g, nonce)*/
132 .replace(/{{cspSource}}/g, cspSource)
133 .replace(regexa, function (match, space: string, file: string) {
137 if (space === undefined) {
140 if (file.match("#") || file.match("https")) {
141 if (file.match("#")) {
142 file = `#${file.split("#")[1]}`;
144 const ret = `<a ${space}href="${file}"`;
148 const command = Uri.parse(
149 `command:raspberry-pi-pico.openSdkDocumentation?${encodeURIComponent(
150 JSON.stringify([undefined, file])
153 const ret = `<a ${space}href="${command}"`;
157 .replace(regexsrc, function (match, file: string) {
158 const ret = `src="${panel.webview
159 .asWebviewUri(Uri.joinPath(extensionUri, "web", "docs", file))
164 .replace(regexcss, function (match, file: string) {
165 const ret = `<link href="${panel.webview
166 .asWebviewUri(Uri.joinPath(extensionUri, "web", "docs", file))
167 .toString()}" rel="stylesheet"`;
171 .replace('<div class="navigation-toggle">', "<div hidden>")
172 .replace('<div id="main-nav">', '<div id="main-nav" hidden>')