From 8b039b3cd7987236e6f432274f4b1c04ad642ea4 Mon Sep 17 00:00:00 2001 From: thepaperpilot Date: Tue, 8 Mar 2022 01:03:11 -0600 Subject: [PATCH] Generate /api/ sidebar --- .vitepress/config.js | 39 +++++++++++++++++++++++++++++++++++++ .vitepress/theme/custom.css | 8 ++++++++ 2 files changed, 47 insertions(+) diff --git a/.vitepress/config.js b/.vitepress/config.js index 85a7e5fe..0e2ce437 100644 --- a/.vitepress/config.js +++ b/.vitepress/config.js @@ -1,3 +1,5 @@ +const fs = require("fs"); + module.exports = { lang: "en-US", title: 'Profectus', @@ -62,6 +64,43 @@ module.exports = { ] } ], + "/api/": generateAPISidebar() } } } + +function generateAPISidebar() { + const modules = fs.readdirSync("./api/modules").map(file => file.substr(0, file.length - 3)); + const folders = {}; + modules.forEach(file => { + // Split by _, but not break_eternity + const pieces = file.replace("break_eternity", "break~eternity").split(/_/).map(piece => piece === "break~eternity" ? "break_eternity" : piece); + const lineItem = { text: camelToTitle(pieces[pieces.length - 1]), link: `/api/modules/${file}` }; + pieces.slice(0, pieces.length - 1).reduce((acc, curr) => { + // console.log(acc, curr); + if (!acc[curr]) { + acc[curr] = { text: camelToTitle(curr), children: [] }; + } + return acc[curr].children; + }, folders).push(lineItem); + }); + return processFolders(folders); +} + +function camelToTitle(camel) { + if (camel === "break_eternity") { + return "Break Eternity"; + } + let title = camel.replace(/([A-Z])/g, " $1"); + title = title.charAt(0).toUpperCase() + title.slice(1); + return title; +} + +function processFolders(folders) { + return Object.values(folders).map(folder => { + if (folder.children) { + folder.children = processFolders(folder.children); + } + return folder; + }) +} diff --git a/.vitepress/theme/custom.css b/.vitepress/theme/custom.css index ce1b9556..d444f52f 100644 --- a/.vitepress/theme/custom.css +++ b/.vitepress/theme/custom.css @@ -14,3 +14,11 @@ tr:nth-child(2n) { background-color: var(--code-bg-color); } + +.sidebar > .sidebar-links > .sidebar-link > .sidebar-links > .sidebar-link > .sidebar-links > .sidebar-link > .sidebar-links > .sidebar-link > .sidebar-links > .sidebar-link > .sidebar-link-item { + display: block; + padding: 0.3rem 1.5rem 0.3rem 5rem; + line-height: 1.4; + font-size: 0.9rem; + font-weight: 400; +}