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

View File

@ -25,29 +25,33 @@ export interface Frontmatter {
} }
type Props = { type Props = {
/** The directory where all the md/mdx files start from */ /** Base url. It is used to later build a tree file system */
base_dir: string base_url: string,
frontmatter: Frontmatter; frontmatter: Frontmatter;
headings: any; headings: any;
posts: Array<AstroFile>; posts: Array<AstroFile>;
}; };
const { const {
base_dir, base_url,
frontmatter, frontmatter,
headings, headings,
posts posts
}: Props = Astro.props; }: Props = Astro.props;
const base_dir_end = base_dir.length; const base_len = base_url.length;
const posts_2 = posts const posts_2 = posts
.map(post => ({ .map(post => {
return {
...post, ...post,
relative_file: post.file.substring(base_dir_end),
path: post.file.substring(base_dir_end).replace(/\.mdx?$/, ""),
title: post.frontmatter.title, 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); .sort((p1, p2) => p1.frontmatter.order > p2.frontmatter.order? 1 : -1);
// build a hierarchy of the files // build a hierarchy of the files
@ -65,7 +69,7 @@ for (const post of posts_2) {
} }
second_level[folder_name].push(post); second_level[folder_name].push(post);
} }
else if (fragments.length === 2) { else {
// add to root folder // add to root folder
second_level["_"]!.push(post); second_level["_"]!.push(post);
} }
@ -86,17 +90,24 @@ for (const levels_key in second_level) {
path: "", path: "",
title: levels_key, title: levels_key,
children: posts, children: posts,
url: "",
}; };
entries.push(parentEntry); 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("\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 ?? "/"; const basePath = index_page?.url ?? "/";
*/
--- ---
<BaseLayout title={frontmatter.title}> <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)]"> <nav class="py-4 pr-2 overflow-x-scroll h-[calc(100vh-3rem)]">
{ {
entries.map((entry) => ( entries.map((entry) => (
<Sidebar entry={entry} basePath={basePath} /> <Sidebar entry={entry} />
)) ))
} }
</nav> </nav>

View File

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