-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
97 additions
and
70 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,56 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
|
||
// Define the directory where the Typedoc HTML files are located | ||
const typedocDir = path.resolve(__dirname, "../../static/typedoc_output"); | ||
import { copyDirectory } from "./utils"; | ||
|
||
// Define a recursive function to find all HTML files in a directory | ||
function findHtmlFiles(dir: string): string[] { | ||
if (!fs.existsSync(dir)) { | ||
return []; | ||
} | ||
const TYPEDOC_DIR = path.resolve(__dirname, "../../typedoc"); | ||
|
||
function updateMentionFiles(dirName: string) { | ||
const dir = path.join(TYPEDOC_DIR, dirName); | ||
const files = fs.readdirSync(dir); | ||
const list: string[] = []; | ||
|
||
files.forEach((file: string) => { | ||
if (fs.statSync(path.resolve(dir, file)).isDirectory()) { | ||
list.concat(findHtmlFiles(path.resolve(dir, file))); | ||
} else if (file.endsWith(".html")) { | ||
list.push(path.resolve(dir, file)); | ||
} | ||
files.forEach((file) => { | ||
const filename = path.join(dir, file); | ||
let content = fs.readFileSync(filename, "utf8"); | ||
content = content.replaceAll("../README.md", "../../introduction.md"); | ||
content = content.replaceAll("../modules.md", "../index.md"); | ||
fs.writeFileSync(filename, content); | ||
}); | ||
|
||
return list; | ||
} | ||
|
||
// Find all HTML files in the Typedoc directory | ||
const htmlFiles = findHtmlFiles(typedocDir); | ||
// Remove the README.md file if exists | ||
const readmeFile = path.join(TYPEDOC_DIR, "README.md"); | ||
if (fs.existsSync(readmeFile)) { | ||
fs.unlinkSync(readmeFile); | ||
} | ||
|
||
// Go through each HTML file and add the target="_parent" attribute to the external links | ||
htmlFiles.forEach((file: string) => { | ||
let content = fs.readFileSync(file, "utf8"); | ||
// Rename modules.md to index.md, and change the README.md mention to ../introduction.md | ||
const modulesFile = path.join(TYPEDOC_DIR, "modules.md"); | ||
if (fs.existsSync(modulesFile)) { | ||
let content = fs.readFileSync(modulesFile, "utf8"); | ||
content = content.replaceAll("README.md", "../introduction.md"); | ||
fs.writeFileSync(modulesFile, content); | ||
fs.renameSync(modulesFile, path.join(TYPEDOC_DIR, "index.md")); | ||
} | ||
|
||
// Add the target="_parent" attribute to the external links | ||
content = content.replace(/<a href="http/g, '<a target="_parent" href="http'); | ||
// Change all ../README.md mention to ../../introduction.md, and change all ../modeuls.md mention to ../index.md | ||
updateMentionFiles("classes"); | ||
updateMentionFiles("interfaces"); | ||
updateMentionFiles("modules"); | ||
|
||
// find the target moving directory | ||
const versionFile = path.resolve(__dirname, "../../versions.json"); | ||
let versionDir = ""; | ||
try { | ||
const versionContent = fs.readFileSync(versionFile, "utf8"); | ||
if (versionContent) { | ||
const versionContentJson: string[] = JSON.parse(versionContent) as string[]; | ||
versionDir = path.resolve(__dirname, `../../versioned_docs/version-${versionContentJson[0]}/typedoc`); | ||
} | ||
} catch (e) { | ||
versionDir = path.resolve(__dirname, "../../docs/typedoc"); | ||
} | ||
|
||
content = content.replace( | ||
/<a href="\.\//g, | ||
'<a target="_parent" href="https://github.com/privacy-scaling-explorations/maci/tree/dev/', | ||
); | ||
// move the typedoc/ directory to target directory | ||
copyDirectory(TYPEDOC_DIR, versionDir); | ||
|
||
fs.writeFileSync(file, content); | ||
}); | ||
fs.rmSync(TYPEDOC_DIR, { recursive: true, force: true }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
|
||
/** | ||
* Allow to copy a directory from source to target | ||
* @param source - the source directory | ||
* @param target - the target directory | ||
*/ | ||
export function copyDirectory(source: string, target: string): void { | ||
if (!fs.existsSync(target)) { | ||
fs.mkdirSync(target, { recursive: true }); | ||
} | ||
|
||
if (!fs.existsSync(source)) { | ||
return; | ||
} | ||
|
||
const files = fs.readdirSync(source); | ||
|
||
files.forEach((file: string) => { | ||
const sourcePath = path.join(source, file); | ||
const targetPath = path.join(target, file); | ||
|
||
if (fs.lstatSync(sourcePath).isDirectory()) { | ||
copyDirectory(sourcePath, targetPath); | ||
} else { | ||
fs.copyFileSync(sourcePath, targetPath); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters