From f6db6fd2d2f0050fff127e4d8c803fc1bef1ccac Mon Sep 17 00:00:00 2001 From: 0xPatrick Date: Fri, 4 Oct 2024 14:17:05 -0400 Subject: [PATCH] fix: functions are not searchable on typedoc site - perform same transformation applied to navigation.js to search.js - closes: #9894 --- scripts/update-typedoc-functions-path.cjs | 58 +++++++++++++---------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/scripts/update-typedoc-functions-path.cjs b/scripts/update-typedoc-functions-path.cjs index 734df479437..0942b4c67d1 100644 --- a/scripts/update-typedoc-functions-path.cjs +++ b/scripts/update-typedoc-functions-path.cjs @@ -7,9 +7,9 @@ * https://github.com/cloudflare/workers-sdk/pull/2103). * * This script - - * 1. renames `/functions` directory to `/function` + * 1. renames `/functions` directory to `/funcs` * 2. updates generated urls in html files to reference new url path - * 3. updates base64 encoded navigation state to reference new url path + * 3. updates base64 encoded navigation and search data 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. @@ -31,17 +31,20 @@ const config = { oldDirName: 'functions', newDirName: 'funcs', apiDocsDir: path.join(__dirname, '..', 'api-docs'), - navigationFilePath: path.join( - __dirname, - '..', - 'api-docs', - 'assets', - 'navigation.js', - ), + dataFiles: [ + { + path: path.join(__dirname, '..', 'api-docs', 'assets', 'navigation.js'), + windowKey: 'navigationData', + }, + { + path: path.join(__dirname, '..', 'api-docs', 'assets', 'search.js'), + windowKey: 'searchData', + }, + ], }; -// Decodes and decompresses the TypeDoc navigation data -function decodeTypeDocNavigation(encodedData) { +// Decodes and decompresses the TypeDoc data +function decodeTypeDocData(encodedData) { return new Promise((resolve, reject) => { const base64Data = encodedData.replace( /^data:application\/octet-stream;base64,/, @@ -65,8 +68,8 @@ function decodeTypeDocNavigation(encodedData) { }); } -// Compresses and encodes the TypeDoc navigation data -function encodeTypeDocNavigation(jsonData) { +// Compresses and encodes the TypeDoc data +function encodeTypeDocData(jsonData) { return new Promise((resolve, reject) => { const jsonString = JSON.stringify(jsonData); @@ -82,7 +85,7 @@ function encodeTypeDocNavigation(jsonData) { }); } -// Recursively updates URLs in the navigation data +// Recursively updates URLs in the data function updateUrls(data, searchString, replaceString) { if (typeof data === 'object' && data !== null) { for (const key in data) { @@ -102,25 +105,27 @@ function updateUrls(data, 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 = "(.*?)"/); +// Updates js-based data file (navigation or search) +async function updateDataFile(filePath, windowKey) { + const fileContent = await fsp.readFile(filePath, 'utf8'); + const match = fileContent.match( + new RegExp(`window\\.${windowKey} = "(.*?)"`), + ); if (!match) { - throw new Error('Navigation data not found in file'); + throw new Error(`${windowKey} data not found in file`); } const encodedData = match[1]; - const decodedData = await decodeTypeDocNavigation(encodedData); + const decodedData = await decodeTypeDocData(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'); + const newEncodedData = await encodeTypeDocData(updatedData); + const newFileContent = `window.${windowKey} = "${newEncodedData}"`; + await fsp.writeFile(filePath, newFileContent); + console.log(`${windowKey} updated successfully`); } /** @@ -196,7 +201,10 @@ async function main() { switch (fileType) { case 'html': await updateFiles(config.apiDocsDir, '.html', updateHtmlContentLinks); - return updateNavigationFile(); + for (const dataFile of config.dataFiles) { + await updateDataFile(dataFile.path, dataFile.windowKey); + } + return; case 'md': return updateFiles(config.apiDocsDir, '.md', updateMarkdownContentLinks); default: