1 /* eslint-disable max-len */
2 import { CommandWithArgs } from "./command.mjs";
3 import Logger from "../logger.mjs";
4 import { type QuickPickItem, ViewColumn, window, Uri, type WebviewPanel } from "vscode";
5 import { readFileSync } from "fs";
7 export enum DocumentationId {
11 runtimeInfrastructure = 3,
14 export const DOCUMENTATION_LABEL_BY_ID: string[] = [
17 "Networking Libraries",
18 "Runtime Infrastructure",
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",
30 const LOCAL_DOCUMENTATION_URLS_BY_ID: string[] = [
31 "group__hardware.html",
32 "group__high__level.html",
33 "group__networking.html",
34 "group__runtime.html",
37 export default class OpenSdkDocumentationCommand extends CommandWithArgs {
38 private _logger: Logger = new Logger("OpenSdkDocumentationCommand");
40 public static readonly id = "openSdkDocumentation";
42 constructor(private readonly _extensionUri: Uri) {
43 super(OpenSdkDocumentationCommand.id);
46 async execute(docId?: DocumentationId, fileName?: string): Promise<void> {
48 if (typeof docId === "number") {
49 url = docId !== undefined ? LOCAL_DOCUMENTATION_URLS_BY_ID[docId] : "";
50 } else if (fileName !== undefined) {
53 const options: QuickPickItem[] = DOCUMENTATION_LABEL_BY_ID.map(
56 detail: LOCAL_DOCUMENTATION_URLS_BY_ID[index],
60 const result = await window.showQuickPick(options, {
61 placeHolder: "Select Category",
63 ignoreFocusOut: false,
66 if (result === undefined || result.detail === undefined) {
74 this._logger.info("Opening SDK documentation in browser...");
75 const panel = window.createWebviewPanel(
76 "pico-sdk-documentation",
77 "Pico SDK Documentation",
78 { viewColumn: ViewColumn.Two, preserveFocus: false },
81 retainContextWhenHidden: true,
82 enableFindWidget: true,
83 localResourceRoots: [Uri.joinPath(this._extensionUri, "web", "docs")],
84 enableCommandUris: true,
88 panel.webview.html = this._getHtmlForWebview(
89 Uri.joinPath(this._extensionUri, "web", "docs", url).fsPath,
92 Uri.joinPath(this._extensionUri, "web", "docs", "docstyle.css")
95 panel.webview.cspSource,
102 private _getHtmlForWebview(
109 const regexa = /<a\s+(.*?)?href="([^"]+)"/g;
110 const regexsrc = /src="([^"]+)"/g;
111 const regexcss = /<link href="([^"]+)" rel="stylesheet"/g;
117 .replace(/{{docstyle}}/g, docstyle)
118 /*.replace(/{{jquery}}/g, jquery)
119 .replace(/{{jquery-ui}}/g, jqueryUi)
120 .replace(/{{jquery-tocify}}/g, jqueryTocify)
121 .replace(/{{nonce}}/g, nonce)*/
122 .replace(/{{cspSource}}/g, cspSource)
123 .replace(regexa, function (match, space: string, file: string) {
127 if (space === undefined) {
130 if (file.match('#') || file.match('https')) {
131 if (file.match('#')) {
132 file = `#${file.split('#')[1]}`;
134 const ret = `<a ${space}href="${file}"`;
138 const command = Uri.parse(`command:raspberry-pi-pico.openSdkDocumentation?${
139 encodeURIComponent(JSON.stringify([undefined, file]))
141 const ret = `<a ${space}href="${command}"`;
145 .replace(regexsrc, function (match, file: string) {
149 Uri.joinPath(extensionUri, "web", "docs", file)
155 .replace(regexcss, function (match, file: string) {
156 const ret = `<link href="${
159 Uri.joinPath(extensionUri, "web", "docs", file)
161 .toString()}" rel="stylesheet"`;
166 '<div class="navigation-toggle">',
170 '<div id="main-nav">',
171 '<div id="main-nav" hidden>'