Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
Almost there
Browse files Browse the repository at this point in the history
  • Loading branch information
ptgott committed Jul 3, 2024
1 parent 0e8ab23 commit fd8195f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
43 changes: 31 additions & 12 deletions server/pages-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,43 +119,62 @@ export const generateNavPaths = (fs, dirPath) => {
}
firstLvlFiles.add(fullPath);
});
let sectionIntros = new Set();

// Map category pages to the directories they introduce so we can can add a
// sidebar entry for the category page, then traverse the directory.
let sectionIntros = new Map();
firstLvlDirs.forEach((d: string) => {
sectionIntros.add(categoryPagePathForDir(fs, d));
sectionIntros.set(categoryPagePathForDir(fs, d), d);
});

// Add files with no corresponding directory to the navigation first. Section
// introductions, by convention, have a filename that corresponds to the
// subdirectory containing pages in the section, or have the name
// "introduction.mdx".
firstLvlFiles.forEach((f) => {
// Handle section intros separately
if (sectionIntros.has(f)) {
return;
}
result.push(getEntryForPath(fs, f));
});

sectionIntros.forEach((si: string) => {
const { slug, title } = getEntryForPath(fs, si);
sectionIntros.forEach((dirPath, categoryPagePath) => {
const { slug, title } = getEntryForPath(fs, categoryPagePath);
const section = {
title: title,
slug: slug,
entries: [],
};
const sectionDir = dirname(si);
const secondLvl = fs.readdirSync(sectionDir, "utf8");
const secondLvl = new Set(fs.readdirSync(dirPath, "utf8"));

console.log("secondLvl:", secondLvl);
// Find all second-level category pages first so we don't
// repeat them in the sidebar.
secondLvl.forEach((f2) => {
let fullPath2 = join(sectionDir, f2);
let fullPath2 = join(dirPath, f2);
const stat = fs.statSync(fullPath2);

// List category pages on the second level, but not their contents.
if (stat.isDirectory()) {
fullPath2 = categoryPagePathForDir(fs, fullPath2);
// This is a category page for the parent directory, which we have already
// listed, so skip it.
} else if (fullPath2 == categoryPagePathForDir(fs, dirname(fullPath2))) {
if (!stat.isDirectory()) {
return;
}
const catPath = categoryPagePathForDir(fs, fullPath2);
fullPath2 = catPath;
secondLvl.delete(f2);

// Delete the category page from the set so we don't add it again
// when we add individual files.
secondLvl.delete(parse(catPath).base);
section.entries.push(getEntryForPath(fs, fullPath2));
});

secondLvl.forEach((f2) => {
let fullPath2 = join(dirPath, f2);
const stat = fs.statSync(fullPath2);
section.entries.push(getEntryForPath(fs, fullPath2));
});

section.entries.sort(sortByTitle);
result.push(section);
});
Expand Down
2 changes: 1 addition & 1 deletion uvu-tests/config-docs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ title: Get Started with DB RBAC

const vol = Volume.fromJSON(files);
const fs = createFsFromVolume(vol);
let actual = generateNavPaths(fs, "/docs/pages/database-access/guides");
let actual = generateNavPaths(fs, "/docs/pages/database-access");
assert.equal(actual, expected);
}
);
Expand Down

0 comments on commit fd8195f

Please sign in to comment.