From 669293c0e38fedb8660258b445eb4d3ee643023f Mon Sep 17 00:00:00 2001 From: 0xPatrick Date: Wed, 17 Jul 2024 17:25:35 -0400 Subject: [PATCH 1/2] docs: bump typedoc to 0.26.4 - with 0.26, typedoc seems to have better support for Functions. currently, web pages crash when these are loaded --- package.json | 4 +- yarn.lock | 126 ++++++++++++++++++++++++++++++++++----------------- 2 files changed, 86 insertions(+), 44 deletions(-) diff --git a/package.json b/package.json index 012f98f52b9..7a4229cb738 100644 --- a/package.json +++ b/package.json @@ -32,8 +32,8 @@ "prettier-plugin-jsdoc": "^1.3.0", "prettier-plugin-sh": "^0.14.0", "type-coverage": "^2.27.1", - "typedoc": "^0.25.13", - "typedoc-plugin-markdown": "^3.17.1", + "typedoc": "^0.26.4", + "typedoc-plugin-markdown": "^4.2.1", "typescript": "^5.5.3", "typescript-eslint": "^7.15.0" }, diff --git a/yarn.lock b/yarn.lock index c97d65999a7..7a3bd13878d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3426,6 +3426,13 @@ estree-walker "^2.0.2" picomatch "^2.3.1" +"@shikijs/core@1.10.3": + version "1.10.3" + resolved "https://registry.yarnpkg.com/@shikijs/core/-/core-1.10.3.tgz#f01763b36f08ad3d2ef46cea7e61858d9d9947d6" + integrity sha512-D45PMaBaeDHxww+EkcDQtDAtzv00Gcsp72ukBtaLSmqRvh0WgGMq3Al0rl1QQBZfuneO75NXMIzEZGFitThWbg== + dependencies: + "@types/hast" "^3.0.4" + "@sinclair/typebox@^0.24.1": version "0.24.51" resolved "https://registry.yarnpkg.com/@sinclair/typebox/-/typebox-0.24.51.tgz#645f33fe4e02defe26f2f5c0410e1c094eac7f5f" @@ -3539,6 +3546,13 @@ dependencies: "@types/node" "*" +"@types/hast@^3.0.4": + version "3.0.4" + resolved "https://registry.yarnpkg.com/@types/hast/-/hast-3.0.4.tgz#1d6b39993b82cea6ad783945b0508c25903e15aa" + integrity sha512-WPs+bbQw5aCj+x6laNGWLH3wviHtoCv/P3+otBhbOhJgG8qtpdAMlTCxLtsTWA7LH1Oh/bFCHsBn0TPS5m30EQ== + dependencies: + "@types/unist" "*" + "@types/http-proxy@^1.17.8": version "1.17.9" resolved "https://registry.yarnpkg.com/@types/http-proxy/-/http-proxy-1.17.9.tgz#7f0e7931343761efde1e2bf48c40f02f3f75705a" @@ -4015,11 +4029,6 @@ ansi-regex@^6.0.1: resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-6.0.1.tgz#3183e38fae9a65d7cb5e53945cd5897d0260a06a" integrity sha512-n5M855fKb2SsfMIiFFoVrABHJC8QtHwVx+mHWP3QcEqBHYienj5dHSgjbxtC0WEZXYt4wcD6zrQElDPhFuZgfA== -ansi-sequence-parser@^1.1.0: - version "1.1.1" - resolved "https://registry.yarnpkg.com/ansi-sequence-parser/-/ansi-sequence-parser-1.1.1.tgz#e0aa1cdcbc8f8bb0b5bca625aac41f5f056973cf" - integrity sha512-vJXt3yiaUL4UU546s3rPXlsry/RnM730G1+HkpKE012AN0sx1eOrxSu95oKDIonskeLTijMgqWZ3uDEe3NFvyg== - ansi-styles@^2.2.1: version "2.2.1" resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-2.2.1.tgz#b432dd3358b634cf75e1e4664368240533c1ddbe" @@ -5666,6 +5675,11 @@ enquirer@~2.3.6: dependencies: ansi-colors "^4.1.1" +entities@^4.4.0: + version "4.5.0" + resolved "https://registry.yarnpkg.com/entities/-/entities-4.5.0.tgz#5d268ea5e7113ec74c4d033b79ea5a35a488fb48" + integrity sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw== + env-paths@^2.2.0: version "2.2.0" resolved "https://registry.yarnpkg.com/env-paths/-/env-paths-2.2.0.tgz#cdca557dc009152917d6166e2febe1f039685e43" @@ -7914,7 +7928,7 @@ json5@^2.2.1, json5@^2.2.2, json5@^2.2.3: resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.3.tgz#78cd6f1a19bdc12b73db5ad0c61efd66c1e29283" integrity sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg== -jsonc-parser@3.2.0, jsonc-parser@^3.2.0: +jsonc-parser@3.2.0: version "3.2.0" resolved "https://registry.yarnpkg.com/jsonc-parser/-/jsonc-parser-3.2.0.tgz#31ff3f4c2b9793f89c67212627c51c6394f88e76" integrity sha512-gfFQZrcTc8CnKXp6Y4/CBT3fTc0OVuDofpre4aEeEpSBPV5X5v4+Vmx+8snU7RLPrNHPKSgLxGo9YuQzz20o+w== @@ -8075,6 +8089,13 @@ lines-and-columns@~2.0.3: resolved "https://registry.yarnpkg.com/lines-and-columns/-/lines-and-columns-2.0.3.tgz#b2f0badedb556b747020ab8ea7f0373e22efac1b" integrity sha512-cNOjgCnLB+FnvWWtyRTzmB3POJ+cXxTA81LoW7u8JdmhfXzriropYwpjShnz1QLLWsQwY7nIxoDmcPTwphDK9w== +linkify-it@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/linkify-it/-/linkify-it-5.0.0.tgz#9ef238bfa6dc70bd8e7f9572b52d369af569b421" + integrity sha512-5aHCbzQRADcdP+ATqnDuhhJ/MRIqDkZX5pyjFHRRysS8vZ5AbqGEoFIb6pYHPZ+L/OC2Lc+xT8uHVVR5CAK/wQ== + dependencies: + uc.micro "^2.0.0" + load-json-file@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/load-json-file/-/load-json-file-4.0.0.tgz#2f5f45ab91e33216234fd53adab668eb4ec0993b" @@ -8351,10 +8372,17 @@ map-obj@^4.0.0: resolved "https://registry.yarnpkg.com/map-obj/-/map-obj-4.3.0.tgz#9304f906e93faae70880da102a9f1df0ea8bb05a" integrity sha512-hdN1wVrZbb29eBGiGjJbeP8JbKjq1urkHJ/LIP/NY48MZ1QVXUsQBV1G1zvYFHn1XE06cwjBsOI2K3Ulnj1YXQ== -marked@^4.3.0: - version "4.3.0" - resolved "https://registry.yarnpkg.com/marked/-/marked-4.3.0.tgz#796362821b019f734054582038b116481b456cf3" - integrity sha512-PRsaiG84bK+AMvxziE/lCFss8juXjNaWzVbN5tXAm4XjeaS9NAHhop+PjQxz2A9h8Q4M/xGmzP8vqNwy6JeK0A== +markdown-it@^14.1.0: + version "14.1.0" + resolved "https://registry.yarnpkg.com/markdown-it/-/markdown-it-14.1.0.tgz#3c3c5992883c633db4714ccb4d7b5935d98b7d45" + integrity sha512-a54IwgWPaeBCAAsv13YgmALOF1elABB08FxO9i+r4VFk5Vl4pKokRPeX8u5TCgSsPi6ec1otfLjdOpVcgbpshg== + dependencies: + argparse "^2.0.1" + entities "^4.4.0" + linkify-it "^5.0.0" + mdurl "^2.0.0" + punycode.js "^2.3.1" + uc.micro "^2.1.0" matcher@^5.0.0: version "5.0.0" @@ -8395,6 +8423,11 @@ mdast-util-to-string@^4.0.0: dependencies: "@types/mdast" "^4.0.0" +mdurl@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/mdurl/-/mdurl-2.0.0.tgz#80676ec0433025dd3e17ee983d0fe8de5a2237e0" + integrity sha512-Lf+9+2r+Tdp5wXDXC4PcIBjTDtq4UKjCPMQhKIuzpJNW0b96kVqSwW0bT7FhRSfmAiFYgP+SCRvdrDozfh0U5w== + media-typer@0.3.0: version "0.3.0" resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748" @@ -8763,7 +8796,7 @@ minimatch@5.1.0: dependencies: brace-expansion "^2.0.1" -"minimatch@6 || 7 || 8 || 9", minimatch@^9.0.1, minimatch@^9.0.3, minimatch@^9.0.4: +"minimatch@6 || 7 || 8 || 9", minimatch@^9.0.1, minimatch@^9.0.4: version "9.0.4" resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-9.0.4.tgz#8e49c731d1749cbec05050ee5145147b32496a51" integrity sha512-KqWh+VchfxcMNRAJjj2tnsSJdNbHsVgnkBhTNrW7AjVo6OvLtxw8zfT9oLw1JSohlFzJ8jCoTgaoXvJ+kHt6fw== @@ -8784,6 +8817,13 @@ minimatch@^5.0.1: dependencies: brace-expansion "^2.0.1" +minimatch@^9.0.5: + version "9.0.5" + resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-9.0.5.tgz#d74f9dd6b57d83d8e98cfb82133b03978bc929e5" + integrity sha512-G6T0ZX48xgozx7587koeX9Ys2NYy6Gmv//P89sEte9V9whIapMNF4idKxnW2QtCcLiTWlb/wfCabAtAFWhhBow== + dependencies: + brace-expansion "^2.0.1" + minimist-options@4.1.0: version "4.1.0" resolved "https://registry.yarnpkg.com/minimist-options/-/minimist-options-4.1.0.tgz#c0655713c53a8a2ebd77ffa247d342c40f010619" @@ -10109,6 +10149,11 @@ pump@^3.0.0: end-of-stream "^1.1.0" once "^1.3.1" +punycode.js@^2.3.1: + version "2.3.1" + resolved "https://registry.yarnpkg.com/punycode.js/-/punycode.js-2.3.1.tgz#6b53e56ad75588234e79f4affa90972c7dd8cdb7" + integrity sha512-uxFIHU0YlHYhDQtV4R9J6a52SLx28BCjT+4ieh7IGbgwVJWO+km431c4yRlREUAsAmt/uMjQUyQHNEPf0M39CA== + punycode@^2.1.0: version "2.1.1" resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec" @@ -10725,15 +10770,13 @@ shelljs@0.8.5: interpret "^1.0.0" rechoir "^0.6.2" -shiki@^0.14.7: - version "0.14.7" - resolved "https://registry.yarnpkg.com/shiki/-/shiki-0.14.7.tgz#c3c9e1853e9737845f1d2ef81b31bcfb07056d4e" - integrity sha512-dNPAPrxSc87ua2sKJ3H5dQ/6ZaY8RNnaAqK+t0eG7p0Soi2ydiqbGOTaZCqaYvA/uZYfS1LJnemt3Q+mSfcPCg== +shiki@^1.9.1: + version "1.10.3" + resolved "https://registry.yarnpkg.com/shiki/-/shiki-1.10.3.tgz#2276fb21a07043b28c5b16001e6a04fef99dbb8f" + integrity sha512-eneCLncGuvPdTutJuLyUGS8QNPAVFO5Trvld2wgEq1e002mwctAhJKeMGWtWVXOIEzmlcLRqcgPSorR6AVzOmQ== dependencies: - ansi-sequence-parser "^1.1.0" - jsonc-parser "^3.2.0" - vscode-oniguruma "^1.7.0" - vscode-textmate "^8.0.0" + "@shikijs/core" "1.10.3" + "@types/hast" "^3.0.4" side-channel@^1.0.4: version "1.0.4" @@ -11655,22 +11698,21 @@ typedarray@^0.0.6: resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" integrity sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA== -typedoc-plugin-markdown@^3.17.1: - version "3.17.1" - resolved "https://registry.yarnpkg.com/typedoc-plugin-markdown/-/typedoc-plugin-markdown-3.17.1.tgz#c33f42363c185adf842f4699166015f7fe0ed02b" - integrity sha512-QzdU3fj0Kzw2XSdoL15ExLASt2WPqD7FbLeaqwT70+XjKyTshBnUlQA5nNREO1C2P8Uen0CDjsBLMsCQ+zd0lw== - dependencies: - handlebars "^4.7.7" +typedoc-plugin-markdown@^4.2.1: + version "4.2.1" + resolved "https://registry.yarnpkg.com/typedoc-plugin-markdown/-/typedoc-plugin-markdown-4.2.1.tgz#b32fddc246c5bfe7e4879834fc3f8b1486de7286" + integrity sha512-7hQt/1WaW/VI4+x3sxwcCGsEylP1E1GvF6OTTELK5sfTEp6AeK+83jkCOgZGp1pI2DiOammMYQMnxxOny9TKsQ== -typedoc@^0.25.13: - version "0.25.13" - resolved "https://registry.yarnpkg.com/typedoc/-/typedoc-0.25.13.tgz#9a98819e3b2d155a6d78589b46fa4c03768f0922" - integrity sha512-pQqiwiJ+Z4pigfOnnysObszLiU3mVLWAExSPf+Mu06G/qsc3wzbuM56SZQvONhHLncLUhYzOVkjFFpFfL5AzhQ== +typedoc@^0.26.4: + version "0.26.4" + resolved "https://registry.yarnpkg.com/typedoc/-/typedoc-0.26.4.tgz#7e83047369a29a710d429dac20996680cae9a314" + integrity sha512-FlW6HpvULDKgc3rK04V+nbFyXogPV88hurarDPOjuuB5HAwuAlrCMQ5NeH7Zt68a/ikOKu6Z/0hFXAeC9xPccQ== dependencies: lunr "^2.3.9" - marked "^4.3.0" - minimatch "^9.0.3" - shiki "^0.14.7" + markdown-it "^14.1.0" + minimatch "^9.0.5" + shiki "^1.9.1" + yaml "^2.4.5" typescript-eslint@^7.15.0, typescript-eslint@^7.3.1: version "7.15.0" @@ -11696,6 +11738,11 @@ typescript@^5.5.3: resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.5.3.tgz#e1b0a3c394190838a0b168e771b0ad56a0af0faa" integrity sha512-/hreyEujaB0w76zKo6717l3L0o/qEUtRgdvUBvlkhoWeOVMjMuHNHk0BRBzikzuGDqNmPQbg5ifMEqsHLiIUcQ== +uc.micro@^2.0.0, uc.micro@^2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/uc.micro/-/uc.micro-2.1.0.tgz#f8d3f7d0ec4c3dea35a7e3c8efa4cb8b45c9e7ee" + integrity sha512-ARDJmphmdvUk6Glw7y9DQ2bFkKBHwQHLi2lsaH6PPmz/Ka9sFOBsBluozhDltWmnv9u/cF6Rt87znRTPV+yp/A== + uglify-js@^3.1.4: version "3.17.4" resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.17.4.tgz#61678cf5fa3f5b7eb789bb345df29afb8257c22c" @@ -11872,16 +11919,6 @@ vary@~1.1.2: resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc" integrity sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg== -vscode-oniguruma@^1.7.0: - version "1.7.0" - resolved "https://registry.yarnpkg.com/vscode-oniguruma/-/vscode-oniguruma-1.7.0.tgz#439bfad8fe71abd7798338d1cd3dc53a8beea94b" - integrity sha512-L9WMGRfrjOhgHSdOYgCt/yRMsXzLDJSL7BPrOZt73gU0iWO4mpqzqQzOz5srxqTvMBaR0XZTSrVWo4j55Rc6cA== - -vscode-textmate@^8.0.0: - version "8.0.0" - resolved "https://registry.yarnpkg.com/vscode-textmate/-/vscode-textmate-8.0.0.tgz#2c7a3b1163ef0441097e0b5d6389cd5504b59e5d" - integrity sha512-AFbieoL7a5LMqcnOF04ji+rpXadgOXnZsxQr//r83kLPr7biP7am3g9zbaZIaBGwBRWeSvoMD4mgPdX3e4NWBg== - walk-up-path@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/walk-up-path/-/walk-up-path-1.0.0.tgz#d4745e893dd5fd0dbb58dd0a4c6a33d9c9fec53e" @@ -12151,6 +12188,11 @@ yaml@^2.2.2, yaml@^2.3.4: resolved "https://registry.yarnpkg.com/yaml/-/yaml-2.4.2.tgz#7a2b30f2243a5fc299e1f14ca58d475ed4bc5362" integrity sha512-B3VqDZ+JAg1nZpaEmWtTXUlBneoGx6CPM9b0TENK6aoSu5t73dItudwdgmi6tHlIZZId4dZ9skcAQ2UbcyAeVA== +yaml@^2.4.5: + version "2.4.5" + resolved "https://registry.yarnpkg.com/yaml/-/yaml-2.4.5.tgz#60630b206dd6d84df97003d33fc1ddf6296cca5e" + integrity sha512-aBx2bnqDzVOyNKfsysjA2ms5ZlnjSAW2eG3/L5G/CSujfjLJTJsEw1bGw8kCf04KodQWk1pxlGnZ56CRxiawmg== + yargs-parser@20.2.4: version "20.2.4" resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-20.2.4.tgz#b42890f14566796f85ae8e3a25290d205f154a54" From 4037399ac008b4e8c5aca84bb87e2657c171d532 Mon Sep 17 00:00:00 2001 From: 0xPatrick Date: Wed, 17 Jul 2024 21:32:48 -0400 Subject: [PATCH 2/2] chore(docs): rename functions to funcs in typedoc output - avoids limitation in cloudflare pages, where we cannot publish html files to a top-level functions directory --- package.json | 7 +- scripts/update-typedoc-functions-path.cjs | 210 ++++++++++++++++++++++ 2 files changed, 215 insertions(+), 2 deletions(-) create mode 100644 scripts/update-typedoc-functions-path.cjs diff --git a/package.json b/package.json index 7a4229cb738..d964835cf10 100644 --- a/package.json +++ b/package.json @@ -48,8 +48,11 @@ "scripts": { "clean": "yarn lerna run --no-bail clean", "check-dependencies": "node ./scripts/check-mismatched-dependencies.cjs", - "docs": "typedoc --tsconfig tsconfig.build.json", - "docs:markdown-for-agoric-documentation-repo": "typedoc --plugin typedoc-plugin-markdown --tsconfig tsconfig.build.json", + "docs": "run-s docs:build docs:update-functions-path", + "docs:build": "typedoc --tsconfig tsconfig.build.json", + "docs:markdown-for-agoric-documentation-repo": "run-s docs:markdown-build 'docs:update-functions-path md'", + "docs:markdown-build": "typedoc --plugin typedoc-plugin-markdown --tsconfig tsconfig.build.json", + "docs:update-functions-path": "node ./scripts/update-typedoc-functions-path.cjs", "lerna": "lerna", "link-cli": "yarn run create-agoric-cli", "create-agoric-cli": "node ./scripts/create-agoric-cli.cjs", diff --git a/scripts/update-typedoc-functions-path.cjs b/scripts/update-typedoc-functions-path.cjs new file mode 100644 index 00000000000..734df479437 --- /dev/null +++ b/scripts/update-typedoc-functions-path.cjs @@ -0,0 +1,210 @@ +#!/usr/bin/env node + +/** + * Help us workaround a limitation in Cloudflare Pages that prevents us from + * publishing static files to a top-level `/functions` directory. (Cloudflare + * reserves this namespace for Worker Functions as of + * https://github.com/cloudflare/workers-sdk/pull/2103). + * + * This script - + * 1. renames `/functions` directory to `/function` + * 2. updates generated urls in html files to reference new url path + * 3. updates base64 encoded navigation state to reference new url path + * + * If an `md` argument is supplied - versus the optional default `html` document - + * a different set of logic will run to update paths are links for markdown files. + * + * See https://github.com/TypeStrong/typedoc/issues/2111 for more solutions + * on how to workaround this. + * + * See https://github.com/Agoric/agoric-sdk/issues/9729 for tracking of the + * issue in this project. If a different solution is arrived at, we can remove + * this file and the accompanying `yarn docs:update-functions-path`. + */ + +const fsp = require('fs').promises; +const path = require('path'); +const zlib = require('zlib'); +const process = require('process'); + +const config = { + oldDirName: 'functions', + newDirName: 'funcs', + apiDocsDir: path.join(__dirname, '..', 'api-docs'), + navigationFilePath: path.join( + __dirname, + '..', + 'api-docs', + 'assets', + 'navigation.js', + ), +}; + +// Decodes and decompresses the TypeDoc navigation data +function decodeTypeDocNavigation(encodedData) { + return new Promise((resolve, reject) => { + const base64Data = encodedData.replace( + /^data:application\/octet-stream;base64,/, + '', + ); + const buffer = Buffer.from(base64Data, 'base64'); + + zlib.gunzip(buffer, (err, decompressed) => { + if (err) { + reject(new Error(`Failed to decompress data: ${err.message}`)); + return; + } + + try { + const jsonData = JSON.parse(decompressed.toString('utf-8')); + resolve(jsonData); + } catch (parseError) { + reject(new Error(`Failed to parse JSON: ${parseError.message}`)); + } + }); + }); +} + +// Compresses and encodes the TypeDoc navigation data +function encodeTypeDocNavigation(jsonData) { + return new Promise((resolve, reject) => { + const jsonString = JSON.stringify(jsonData); + + zlib.gzip(jsonString, (err, compressed) => { + if (err) { + reject(new Error(`Failed to compress data: ${err.message}`)); + return; + } + + const base64Data = compressed.toString('base64'); + resolve(`data:application/octet-stream;base64,${base64Data}`); + }); + }); +} + +// Recursively updates URLs in the navigation data +function updateUrls(data, searchString, replaceString) { + if (typeof data === 'object' && data !== null) { + for (const key in data) { + if ( + typeof data[key] === 'string' && + data[key].includes(`${searchString}/`) + ) { + data[key] = data[key].replace( + new RegExp(`${searchString}/`, 'g'), + `${replaceString}/`, + ); + } else if (typeof data[key] === 'object') { + updateUrls(data[key], searchString, replaceString); + } + } + } + return data; +} + +// Updates js-based navigation state +async function updateNavigationFile() { + const fileContent = await fsp.readFile(config.navigationFilePath, 'utf8'); + const match = fileContent.match(/window\.navigationData = "(.*?)"/); + if (!match) { + throw new Error('Navigation data not found in file'); + } + const encodedData = match[1]; + + const decodedData = await decodeTypeDocNavigation(encodedData); + const updatedData = updateUrls( + decodedData, + config.oldDirName, + config.newDirName, + ); + const newEncodedData = await encodeTypeDocNavigation(updatedData); + const newFileContent = `window.navigationData = "${newEncodedData}"`; + await fsp.writeFile(config.navigationFilePath, newFileContent); + console.log('Navigation file updated successfully'); +} + +/** + * Updates files in a directory + * @param {string} dir - Directory to update + * @param {string} fileExtension - File extension to process + * @param {Function} updateFunction - Function to update file content + */ +async function updateFiles(dir, fileExtension, updateFunction) { + const files = await fsp.readdir(dir); + for (const file of files) { + const filePath = path.join(dir, file); + const stat = await fsp.stat(filePath); + if (stat.isDirectory()) { + if (file === config.oldDirName) { + const newPath = path.join(dir, config.newDirName); + await fsp.rename(filePath, newPath); + console.log(`Renamed directory: ${filePath} to ${newPath}`); + await updateFiles(newPath, fileExtension, updateFunction); + } else { + await updateFiles(filePath, fileExtension, updateFunction); + } + } else if (path.extname(file) === fileExtension) { + const content = await fsp.readFile(filePath, 'utf8'); + const updatedContent = updateFunction(content); + if (content !== updatedContent) { + await fsp.writeFile(filePath, updatedContent); + console.log(`Updated: ${filePath}`); + } + } + } +} + +/** + * Updates content in Markdown files + * @param {string} content - The Markdown content to update + * @returns {string} - The updated Markdown content + */ +function updateMarkdownContentLinks(content) { + return ( + content + // Update links like [text](functions/file.md) + .replace( + new RegExp(`\\[(.*?)\\]\\(${config.oldDirName}/`, 'g'), + `[$1](${config.newDirName}/`, + ) + // Update links like [text](../functions/file.md) + .replace( + new RegExp(`\\[(.*?)\\]\\(\\.\\./${config.oldDirName}/`, 'g'), + `[$1](../${config.newDirName}/`, + ) + ); +} + +/** + * Updates content in HTML files + * @param {string} content - The HTML content to update + * @returns {string} - The updated HTML content + */ +function updateHtmlContentLinks(content) { + return content.replace( + new RegExp(`/${config.oldDirName}/`, 'g'), + `/${config.newDirName}/`, + ); +} + +/** + * Main function to run the script + */ +async function main() { + const fileType = process.argv[2] || 'html'; + await null; + switch (fileType) { + case 'html': + await updateFiles(config.apiDocsDir, '.html', updateHtmlContentLinks); + return updateNavigationFile(); + case 'md': + return updateFiles(config.apiDocsDir, '.md', updateMarkdownContentLinks); + default: + throw new Error('Invalid file type. Use "html" or "md".'); + } +} + +main().catch(e => { + console.error(`Error: ${e.message}`); + process.exit(1); +});