Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions apps/web/src/routes/_view/docs/$.tsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,47 @@
import { createFileRoute, notFound, redirect } from "@tanstack/react-router";
import { createFileRoute, redirect } from "@tanstack/react-router";
import { allDocs } from "content-collections";

import { DocLayout } from "./-components";
import { docsStructure } from "./structure";

export const Route = createFileRoute("/_view/docs/$")({
component: Component,
loader: async ({ params }) => {
beforeLoad: ({ params }) => {
const splat = params._splat || "";
let doc = allDocs.find((doc) => doc.slug === splat);
const normalizedSplat = splat.replace(/\/$/, "");

if (!doc) {
doc = allDocs.find((doc) => doc.slug === `${splat}/index`);
if (docsStructure.defaultPages[normalizedSplat]) {
throw redirect({
to: "/docs/$",
params: { _splat: docsStructure.defaultPages[normalizedSplat] },
});
}

let doc = allDocs.find((doc) => doc.slug === normalizedSplat);
if (!doc) {
const pathParts = splat.split("/");
const firstPart = pathParts[0];
const sectionName =
firstPart.charAt(0).toUpperCase() + firstPart.slice(1);
const docsInSection = allDocs
.filter((d) => d.section === sectionName && !d.isIndex)
.sort((a, b) => a.order - b.order);
doc = allDocs.find((doc) => doc.slug === `${normalizedSplat}/index`);
}

if (docsInSection.length > 0) {
throw redirect({
to: "/docs/$",
params: { _splat: docsInSection[0].slug },
});
if (!doc) {
if (normalizedSplat === "about/hello-world") {
return;
}
throw redirect({
to: "/docs/$",
params: { _splat: "about/hello-world" },
});
}
},
loader: async ({ params }) => {
const splat = params._splat || "";
const normalizedSplat = splat.replace(/\/$/, "");

throw notFound();
let doc = allDocs.find((doc) => doc.slug === normalizedSplat);
if (!doc) {
doc = allDocs.find((doc) => doc.slug === `${normalizedSplat}/index`);
}

return { doc };
return { doc: doc! };
},
head: ({ loaderData }) => {
const { doc } = loaderData!;
Expand Down
3 changes: 0 additions & 3 deletions apps/web/src/routes/_view/docs/-structure.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion apps/web/src/routes/_view/docs/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { allDocs } from "content-collections";
import { useMemo } from "react";

import { docsStructure } from "./-structure";
import { docsStructure } from "./structure";

export const Route = createFileRoute("/_view/docs")({
component: Component,
Expand Down
8 changes: 8 additions & 0 deletions apps/web/src/routes/_view/docs/structure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const docsStructure = {
sections: ["about", "developers", "pro"],
defaultPages: {
about: "about/hello-world",
developers: "developers/analytics",
pro: "pro/activation",
} as Record<string, string>,
};