fix: bugs rendering sidebars

This commit is contained in:
Araozu 2024-11-22 06:17:59 -05:00
parent 0343af8375
commit d4baa970d6
4 changed files with 32 additions and 32 deletions

View File

@ -2,17 +2,7 @@
import type { PageEntry } from "../layouts/PagesLayout.astro";
const entry: PageEntry = Astro.props.entry;
const basePath: string = Astro.props.basePath;
const entryPath = is_index_file(entry.path)? "": entry.path;
const entryUrl = basePath + entryPath + (entryPath.endsWith("/")? "" : "/");
function is_index_file(p) {
return p.endsWith("index")
|| p.endsWith("index.md")
|| p.endsWith("index.mdx")
}
const post_url = entry.url + (entry.url.endsWith("/") ? "" : "/");
---
{
@ -20,7 +10,7 @@ function is_index_file(p) {
<li>
<a
class="inline-block rounded-2xl w-full hover:bg-neutral-200 dark:hover:bg-neutral-800 transition-colors px-3 py-2"
href={entryUrl}
href={post_url}
>
{entry.title}
</a>
@ -38,7 +28,6 @@ function is_index_file(p) {
{entry.children!.map((nextEntry) => (
<Astro.self
entry={nextEntry}
basePath={`${basePath}${entry.path}`}
/>
))}
</ul>

View File

@ -25,29 +25,33 @@ export interface Frontmatter {
}
type Props = {
/** The directory where all the md/mdx files start from */
base_dir: string
/** Base url. It is used to later build a tree file system */
base_url: string,
frontmatter: Frontmatter;
headings: any;
posts: Array<AstroFile>;
};
const {
base_dir,
base_url,
frontmatter,
headings,
posts
}: Props = Astro.props;
const base_dir_end = base_dir.length;
const base_len = base_url.length;
const posts_2 = posts
.map(post => ({
.map(post => {
return {
...post,
relative_file: post.file.substring(base_dir_end),
path: post.file.substring(base_dir_end).replace(/\.mdx?$/, ""),
title: post.frontmatter.title,
}))
// this should be a path relative to the base url.
// e.g if base_url is `/spec`, then this instead of
// being `/spec/ast/tokens` would be `/ast/tokens`
path: post.url.substring(base_len),
}
})
.sort((p1, p2) => p1.frontmatter.order > p2.frontmatter.order? 1 : -1);
// build a hierarchy of the files
@ -65,7 +69,7 @@ for (const post of posts_2) {
}
second_level[folder_name].push(post);
}
else if (fragments.length === 2) {
else {
// add to root folder
second_level["_"]!.push(post);
}
@ -86,17 +90,24 @@ for (const levels_key in second_level) {
path: "",
title: levels_key,
children: posts,
url: "",
};
entries.push(parentEntry);
}
}
const index_page = entries.find(post => post.relative_file === "/index.md" || post.relative_file === "/index.mdx");
if (index_page === undefined && process.env.NODE_ENV !== "production") {
/*
const index_page = posts_2.find(post => post.relative_file === "/index.md" || post.relative_file === "/index.mdx");
if (index_page === undefined) {
console.error("\n\nBuilding without an index page");
console.error("Props base_dir:", base_dir);
console.error(import.meta.dirname);
console.error("base_path", base_dir);
console.error("entries len", entries.length);
entries.forEach(e => console.log("\t"+e.url));
throw new Error("No entries");
}
const basePath = index_page?.url ?? "/";
*/
---
<BaseLayout title={frontmatter.title}>
@ -113,7 +124,7 @@ const basePath = index_page?.url ?? "/";
<nav class="py-4 pr-2 overflow-x-scroll h-[calc(100vh-3rem)]">
{
entries.map((entry) => (
<Sidebar entry={entry} basePath={basePath} />
<Sidebar entry={entry} />
))
}
</nav>

View File

@ -6,12 +6,12 @@ const { frontmatter, headings } = Astro.props;
const posts = await Astro.glob("./**/*.{md,mdx}") as unknown as Array<AstroFile>;
// Current dir
const current_dir = import.meta.dirname;
// The base of every URL under this glob
const base_url = "/en/latest/learn"
---
<NewDocsLayout
base_dir={current_dir}
base_url={base_url}
frontmatter={frontmatter}
headings={headings}
posts={posts}

View File

@ -6,12 +6,12 @@ const { frontmatter, headings } = Astro.props;
const posts = await Astro.glob("./**/*.{md,mdx}") as unknown as Array<AstroFile>;
// Current dir
const current_dir = import.meta.dirname;
// The base of every URL under this glob
const base_url = "/spec"
---
<NewDocsLayout
base_dir={current_dir}
base_url={base_url}
frontmatter={frontmatter}
headings={headings}
posts={posts}