From afcde9a00e5c782b138aea55071b32c4c1dbe9dd Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Wed, 18 Oct 2023 16:12:11 +0200 Subject: [PATCH] Fix issue links to functions being resolved, if there are multiple function declarations with the same name. --- .github/workflows/build-typescript-docs.yml | 3 ++ config/gendoc.sh | 1 + config/post-process.js | 53 +++++++++++++++++++-- config/pre-process.js | 30 ++++++++++++ 4 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 config/pre-process.js diff --git a/.github/workflows/build-typescript-docs.yml b/.github/workflows/build-typescript-docs.yml index 675de0c4..c519890a 100644 --- a/.github/workflows/build-typescript-docs.yml +++ b/.github/workflows/build-typescript-docs.yml @@ -25,6 +25,9 @@ jobs: cd config api-extractor run --local --verbose + - name: Pre-Process API Extractor output + run: node pre-process.js + - name: Clone API Documenter fork run: git clone https://github.com/ericaxu/rushstack-obsidian.git diff --git a/config/gendoc.sh b/config/gendoc.sh index 0e87cba2..7d598bac 100644 --- a/config/gendoc.sh +++ b/config/gendoc.sh @@ -2,6 +2,7 @@ # 1. Run api-extractor api-extractor run --local --verbose +node pre-process.js # Remove everything under ../en/Reference/TypeScript API before doing step 2 rm -rf ../en/Reference/TypeScript\ API/* diff --git a/config/post-process.js b/config/post-process.js index 8ee8f6a8..fcfbe7c5 100644 --- a/config/post-process.js +++ b/config/post-process.js @@ -112,10 +112,6 @@ async function organizeIntoFolders() { const remainingFiles = await fs.readdir(sourceDir); for (const file of remainingFiles) { - if (!file.endsWith('.md')) { - continue; - } - const fileName = path.basename(file, '.md'); if (await folderExists(path.join(sourceDir, fileName))) { @@ -131,9 +127,56 @@ async function organizeIntoFolders() { } } +async function updateFileLinks(folderPath) { + try { + const files = await fs.readdir(folderPath); + + for (const file of files) { + if(!file.endsWith('.md')) { + const joined = path.join(sourceDir, file); + if(await folderExists(joined)) { + await updateFileLinks(joined); + } + continue; + } + + try { + let filePath = path.join(folderPath, file); + let fileContent = await fs.readFile(filePath, 'utf-8'); + const regex = /::(.*)::(.*)::/g; + const matches = fileContent.matchAll(regex); + for(const match of matches) { + let alias = match[2]; + if(match[2] === '') { + alias = match[1]; + } + let filename = match[1].split('.').pop().toLowerCase(); + + //prefer specific API class + if(filename === 'getleaf') { + filename = 'workspace.getleaf_1'; + } + //remove any backslashes in links. + filename = filename.replace('\\', ''); + alias = alias.replace('\\', ''); + + fileContent = fileContent.replaceAll(regex, `[${alias}](obsidian.${filename}.md)`); + } + await fs.writeFile(filePath, fileContent, 'utf-8'); + console.log(`Replaced files: ${filePath}`); + } catch (err) { + console.error(`Error processing file: ${err.message}`); + } + } + } catch (err) { + console.error(`Error reading directory: ${err.message}`); + } +} + async function main() { - await renameFiles(folderPath);; + await renameFiles(folderPath); await organizeIntoFolders(); + await updateFileLinks(folderPath); } main(); diff --git a/config/pre-process.js b/config/pre-process.js new file mode 100644 index 00000000..de56bb10 --- /dev/null +++ b/config/pre-process.js @@ -0,0 +1,30 @@ +const fs = require('fs/promises'); + +async function renameDuplicateFunctionReferences() { + const configFile = await fs.readFile('./input/obsidian.api.json', 'utf8'); + const configuration = JSON.parse(configFile); + await each(configuration.members); + fs.writeFile('./input/obsidian.api.json', JSON.stringify(configuration)); +} + +async function each(members) { + members.forEach(member => { + + if(member.docComment) { + if(member.docComment.includes('getLeaf')) { + const regex = /{@link Workspace.getLeaf(.*)}/g; + member.docComment = member.docComment.replace(regex, "::Workspace.getLeaf::$1::") + } + } + + if(member.members) { + each(member.members); + } + }) +} + +async function main() { + await renameDuplicateFunctionReferences(); +} + +main();