From a8d31b7fae114b82bf315114bd28daa6db3ac1b5 Mon Sep 17 00:00:00 2001 From: Alex Stephen Date: Sat, 28 Sep 2024 13:01:48 -0700 Subject: [PATCH 1/2] sidebar into one json file, looks better --- astro.config.mjs | 4 +-- scripts/generate.ts | 78 ++++++++++++++++++++++++------------------ scripts/src/sidebar.ts | 17 +++++---- 3 files changed, 55 insertions(+), 44 deletions(-) diff --git a/astro.config.mjs b/astro.config.mjs index c3d0435..c658c53 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -8,7 +8,6 @@ import { VitePluginRadar } from 'vite-plugin-radar'; let sidebar = JSON.parse(fs.readFileSync("generated/sidebar.json")); -let linter_sidebar = JSON.parse(fs.readFileSync("generated/linter_sidebar.json")); let redirects = JSON.parse(fs.readFileSync("generated/redirects.json")); let config = JSON.parse(fs.readFileSync("generated/config.json")); @@ -28,8 +27,7 @@ export default defineConfig({ social: { github: config.urls.repo, }, - // Google Analytics tag. - sidebar: sidebar.concat(linter_sidebar) + sidebar: sidebar }), tailwind({ applyBaseStyles: false, diff --git a/scripts/generate.ts b/scripts/generate.ts index de7e823..af29eba 100644 --- a/scripts/generate.ts +++ b/scripts/generate.ts @@ -7,8 +7,8 @@ import { type AEP, type ConsolidatedLinterRule, type GroupFile, type LinterRule, import { buildMarkdown, Markdown } from './src/markdown'; import { load, dump } from "js-yaml"; -const AEP_LOC = process.env.AEP_LOCATION!; -const AEP_LINTER_LOC = process.env.AEP_LINTER_LOC!; +const AEP_LOC = process.env.AEP_LOCATION || ""; +const AEP_LINTER_LOC = process.env.AEP_LINTER_LOC || ""; async function getFolders(dirPath: string): Promise { const entries = await fs.promises.readdir(dirPath, { withFileTypes: true }); @@ -272,48 +272,58 @@ function buildHomepage(): Markdown { return markdown; } -// Build config. -let config = loadConfigFiles("hero.yaml", "urls.yaml", "site.yaml"); -writeSidebar(config, "config.json"); +let sidebar: Sidebar = []; -// Build out AEPs. -let aeps = await assembleAEPs(); +if(AEP_LOC != "") { + // Build config. + let config = loadConfigFiles("hero.yaml", "urls.yaml", "site.yaml"); + writeSidebar(config, "config.json"); -// Build sidebar. -let sidebar = buildSidebar(aeps, readGroupFile(AEP_LOC)); + // Write assorted pages. + sidebar = await writePages(AEP_LOC, sidebar); -let full_aeps = buildFullAEPList(aeps); -writeSidebar(full_aeps, "full_aeps.json"); + // Build out AEPs. + let aeps = await assembleAEPs(); + // Build sidebar. + sidebar = buildSidebar(aeps, readGroupFile(AEP_LOC), sidebar); -// Write AEPs to files. -for (var aep of aeps) { - writeMarkdown(aep); -} + let full_aeps = buildFullAEPList(aeps); + writeSidebar(full_aeps, "full_aeps.json"); -// Write assorted pages. -sidebar = await writePages(AEP_LOC, sidebar); -// Write linter pages. -await writePage(AEP_LINTER_LOC, "README.md", "src/content/docs/tooling/linter/index.md", "Protobuf Linter") + // Write AEPs to files. + for (var aep of aeps) { + writeMarkdown(aep); + } -// Write site generator. -await writePage(".", "README.md", "src/content/docs/tooling/website/index.md", "") -writeSidebar(sidebar, "sidebar.json"); + buildIndexPage(aeps); + writeSidebar(buildRedirects(aeps), "redirects.json"); -// Write out linter rules. -let linter_rules = await assembleLinterRules(); -let consolidated_rules = consolidateLinterRule(linter_rules); -for (var rule of consolidated_rules) { - writeRule(rule); + let homePage = buildHomepage(); + fs.writeFileSync("src/content/docs/index.mdx", homePage.build()); +} else { + console.warn("AEP repo is not found.") } -var linter_sidebar = buildLinterSidebar(consolidated_rules); -addToSidebar(linter_sidebar, "Tooling", [{label: "Website", link: "tooling/website"}]); -writeSidebar(linter_sidebar, "linter_sidebar.json"); +if (AEP_LINTER_LOC != "") { + // Write linter pages. + await writePage(AEP_LINTER_LOC, "README.md", "src/content/docs/tooling/linter/index.md", "Protobuf Linter") + + // Write site generator. + await writePage(".", "README.md", "src/content/docs/tooling/website/index.md", "") -buildIndexPage(aeps); -writeSidebar(buildRedirects(aeps), "redirects.json"); + // Write out linter rules. + let linter_rules = await assembleLinterRules(); + let consolidated_rules = consolidateLinterRule(linter_rules); + for (var rule of consolidated_rules) { + writeRule(rule); + } + + sidebar = buildLinterSidebar(consolidated_rules, sidebar) + sidebar = addToSidebar(sidebar, "Tooling", [{label: "Website", link: "tooling/website"}]); +} else { + console.warn("Proto linter repo is not found.") +} -let homePage = buildHomepage(); -fs.writeFileSync("src/content/docs/index.mdx", homePage.build()); \ No newline at end of file +writeSidebar(sidebar, "sidebar.json"); diff --git a/scripts/src/sidebar.ts b/scripts/src/sidebar.ts index 6ceab42..3661346 100644 --- a/scripts/src/sidebar.ts +++ b/scripts/src/sidebar.ts @@ -1,7 +1,7 @@ import type { Sidebar, AEP, ConsolidatedLinterRule } from './types'; -function buildLinterSidebar(rules: ConsolidatedLinterRule[]): Sidebar { - return [ +function buildLinterSidebar(rules: ConsolidatedLinterRule[], sidebar: Sidebar): Sidebar { + let contents = [ { label: 'Tooling', items: [ @@ -19,21 +19,22 @@ function buildLinterSidebar(rules: ConsolidatedLinterRule[]): Sidebar { ] } ]; + return addToSidebar(sidebar, "Tooling", contents); } -function buildSidebar(aeps: AEP[], groups: any): Sidebar { - let response = [{'label': 'Overview', 'items':[]}]; - +function buildSidebar(aeps: AEP[], groups: any, sidebar: Sidebar): Sidebar { + let response = []; for (var group of groups.categories) { response.push({ 'label': group.title, 'items': aeps.filter((aep) => aep.category == group.code).sort((a1, a2) => a1.order > a2.order ? 1 : -1).map((aep) => ({label: `${aep.id}. ${aep.title}`, link: aep.slug})) }) } - return response as Sidebar; + + return addToSidebar(sidebar, "AEPs", response); } -function addToSidebar(sidebar: Sidebar, label: string, items: string[]): Sidebar { +function addToSidebar(sidebar: Sidebar, label: string, items): Sidebar { const targetGroupIndex = sidebar.findIndex(group => group.label === label); if (targetGroupIndex != -1) { if (Array.isArray(sidebar[targetGroupIndex].items)) { @@ -41,6 +42,8 @@ function addToSidebar(sidebar: Sidebar, label: string, items: string[]): Sidebar } else { sidebar[targetGroupIndex].items = items; } + } else { + sidebar.push({'label': label, items: items}) } return sidebar; } From 095fa72e69d20a4c1cb1a367f3672f0522c6b31f Mon Sep 17 00:00:00 2001 From: Alex Stephen Date: Sat, 28 Sep 2024 13:08:38 -0700 Subject: [PATCH 2/2] sorting looks correct --- scripts/src/sidebar.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/src/sidebar.ts b/scripts/src/sidebar.ts index 3661346..99ad353 100644 --- a/scripts/src/sidebar.ts +++ b/scripts/src/sidebar.ts @@ -27,7 +27,7 @@ function buildSidebar(aeps: AEP[], groups: any, sidebar: Sidebar): Sidebar { for (var group of groups.categories) { response.push({ 'label': group.title, - 'items': aeps.filter((aep) => aep.category == group.code).sort((a1, a2) => a1.order > a2.order ? 1 : -1).map((aep) => ({label: `${aep.id}. ${aep.title}`, link: aep.slug})) + 'items': aeps.filter((aep) => aep.category == group.code).sort((a1, a2) => a1.id > a2.id ? 1 : -1).map((aep) => ({label: `${aep.id}. ${aep.title}`, link: aep.slug})) }) }