Skip to content

Commit

Permalink
Merge pull request #74 from complexdatacollective/fix-sidebar
Browse files Browse the repository at this point in the history
fix sidebar generation
  • Loading branch information
jthrilly authored Nov 21, 2024
2 parents b51bd43 + e6eadc9 commit f0e2ce7
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 86 deletions.
6 changes: 0 additions & 6 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
"typescript.enablePromptUseWorkspaceTsdk": true,
"cSpell.words": ["netcanvas", "Tipbox"],
"editor.defaultFormatter": "biomejs.biome",
"[typescriptreact]": {
"editor.defaultFormatter": "biomejs.biome"
},
"[typescript]": {
"editor.defaultFormatter": "biomejs.biome"
},
"editor.codeActionsOnSave": {
"source.organizeImports": "always",
"source.fixAll": "always"
Expand Down
41 changes: 26 additions & 15 deletions apps/documentation/lib/writeSidebarJson.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import "dotenv/config"; // This is essential here, because helper functions (below) use env variables, but they are not available in the Node.js environment without dotenv! This file is run directly in Node via tsc.
import matter from "gray-matter";
import { readFileSync, readdirSync, writeFileSync } from "node:fs";
import { readdirSync, readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { type Locale, type TSideBar, locales } from "~/app/types";
import { locales, type Locale, type SidebarLocaleDefinition, type TSideBar } from "~/app/types";
import {
createFolderEntry,
createPageEntry,
Expand All @@ -25,7 +25,7 @@ function generateSidebarData() {

// Set up initial structure for sidebar data.
const sidebarData: TSideBar = locales.reduce((acc, locale) => {
acc[locale] = {} as TSideBar[Locale];
acc[locale] = {} as SidebarLocaleDefinition;
return acc;
}, {} as TSideBar);

Expand All @@ -34,56 +34,65 @@ function generateSidebarData() {
for (const file of sortedFiles) {
if (file.isDirectory()) {
const metadata = getMetaDataForDirectory(join(file.path, file.name));
const currentLocales = Object.keys(sidebarData);

if (metadata.type === "project") {
for (const locale of Object.keys(sidebarData) as Locale[]) {
for (const l of currentLocales) {
const locale = l as Locale;

sidebarData[locale] = {
...sidebarData[locale],
[file.name]: createProjectEntry(file, locale, metadata),
};
}

return;
continue;
}

// If this is a folder, create a folder entry
if (metadata.type === "folder") {
const nestedPath = getNestedPath(file.path);

// Insert folder entry for each locale in the nested path
for (const locale of Object.keys(sidebarData) as Locale[]) {
for (const l of currentLocales) {
const locale = l as Locale;
set(sidebarData[locale], [...nestedPath, file.name], createFolderEntry(file, locale, metadata));
}

return;
continue;
}
}

// Only process files ending in .md or .mdx
if (!file.name.endsWith(".md") && !file.name.endsWith(".mdx")) return;
if (!file.name.endsWith(".md") && !file.name.endsWith(".mdx")) {
continue;
}

// Determine locale based on file name (format is `index.en.mdx` or `index.en.md`)
const locale = file.name.split(".")[1] as Locale | undefined;

// If there's no locale, or the locale isn't included in the type, ignore it.
if (!locale || !locales.includes(locale as Locale)) {
// eslint-disable-next-line no-console
console.warn(
`File ${file.name} is missing a locale or has a locale not defined in Locale. Locale is ${locale}. Skipping.`,
);
return;
continue;
}

// create a key based on the filename without the locale or extension
// biome-ignore lint/style/noNonNullAssertion: filename is known to have a value here
// Create a key based on the filename without the locale or extension
// biome-ignore lint/style/noNonNullAssertion: structure is known
const key = file.name.split(".")[0]!;

const nestedPath = getNestedPath(file.parentPath);
const nestedPath = getNestedPath(file.path);

const markdownFile = readFileSync(join(file.parentPath, file.name), "utf-8");
const markdownFile = readFileSync(join(file.path, file.name), "utf-8");
const matterResult = matter(markdownFile);

// If file has "hidden: true" in frontmatter, skip it
if (matterResult.data.hidden) return;
if (matterResult.data.hidden) {
continue;
}

set(sidebarData[locale], [...nestedPath, key], createPageEntry(file, matterResult));
}
Expand All @@ -94,7 +103,9 @@ function generateSidebarData() {
try {
const sidebarData = generateSidebarData();

writeFileSync(join(process.cwd(), "public", "sidebar.json"), JSON.stringify(sidebarData, null, 2), "utf-8");
writeFileSync(join(process.cwd(), "public", "sidebar.json"), JSON.stringify(sidebarData, null, "\t"), "utf-8");
} catch (e) {
// eslint-disable-next-line no-console
console.log("Error writing sidebar data!", e);
throw e;
}
2 changes: 1 addition & 1 deletion apps/documentation/public/sidebar.json
Original file line number Diff line number Diff line change
Expand Up @@ -422,4 +422,4 @@
}
}
}
}
}
Loading

0 comments on commit f0e2ce7

Please sign in to comment.