Skip to content

Commit

Permalink
Sidebar changes (#24)
Browse files Browse the repository at this point in the history
* sidebar into one json file, looks better

* sorting looks correct
  • Loading branch information
rambleraptor authored Sep 28, 2024
1 parent 10b3820 commit e4d1455
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 45 deletions.
4 changes: 1 addition & 3 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

Expand All @@ -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,
Expand Down
78 changes: 44 additions & 34 deletions scripts/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]> {
const entries = await fs.promises.readdir(dirPath, { withFileTypes: true });
Expand Down Expand Up @@ -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());
writeSidebar(sidebar, "sidebar.json");
19 changes: 11 additions & 8 deletions scripts/src/sidebar.ts
Original file line number Diff line number Diff line change
@@ -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: [
Expand All @@ -19,28 +19,31 @@ 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}))
'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}))
})
}
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)) {
sidebar[targetGroupIndex].items.push(...items);
} else {
sidebar[targetGroupIndex].items = items;
}
} else {
sidebar.push({'label': label, items: items})
}
return sidebar;
}
Expand Down

0 comments on commit e4d1455

Please sign in to comment.