Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename getTree function into getDocsTreeFromPath #172

Merged
merged 3 commits into from
Jul 11, 2024
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
4 changes: 2 additions & 2 deletions apps/frontpage/app/docs-all/sitemap.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MetadataRoute } from 'next';
import { generateDocsTree } from '../../lib/get-tree';
import { getDocsTreeFromPath } from '../../lib/get-docs-tree-from-path';
import { docsVersions } from '@repo/utils';
import {
FlatTreeNode,
Expand All @@ -12,7 +12,7 @@ export default function sitemap(): MetadataRoute.Sitemap {
const listofTrees = docsVersions.map((version) => {
return {
version: version,
tree: generateDocsTree(`content/docs/${version.id}`),
tree: getDocsTreeFromPath(`content/docs/${version.id}`),
};
});

Expand Down
16 changes: 8 additions & 8 deletions apps/frontpage/app/docs/[[...slug]]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { notFound, redirect } from 'next/navigation';
import Link from 'next/link';
import type { TreeProps } from '@repo/utils';
import { GLOBAL_SEARCH_META_KEYS, GLOBAL_SEARCH_IMPORTANCE } from '@repo/ui';
import { globalSearchMetaKeys, globalSearchImportance } from '@repo/ui';
import { latestVersion, cn } from '@repo/utils';
import { getVersion } from '../../../lib/get-version';
import { getPageData } from '../../../lib/get-page';
import { Renderers } from '../../../components/docs/renderers';
import { generateDocsTree } from '../../../lib/get-tree';
import { getDocsTreeFromPath } from '../../../lib/get-docs-tree-from-path';
import { DocsFooter } from '../../../components/docs/footer/footer';
import { Metadata } from 'next';
import { TableOfContent } from '../../../components/docs/table-of-content';
Expand All @@ -27,8 +27,8 @@ export async function generateMetadata({
description:
"Storybook is a frontend workshop for building UI components and pages in isolation. Thousands of teams use it for UI development, testing, and documentation. It's open source and free.",
other: {
[GLOBAL_SEARCH_META_KEYS.VERSION]: activeVersion.id,
[GLOBAL_SEARCH_META_KEYS.IMPORTANCE]: GLOBAL_SEARCH_IMPORTANCE.DOCS,
[globalSearchMetaKeys.version]: activeVersion.id,
[globalSearchMetaKeys.importance]: globalSearchImportance.docs,
},
};
}
Expand All @@ -37,7 +37,7 @@ const latestVersionId = latestVersion.id;

export const generateStaticParams = () => {
const result: { slug: string[] }[] = [];
const tree = generateDocsTree();
const tree = getDocsTreeFromPath();

const getSlugs = (data: TreeProps[]) => {
data.forEach((item) => {
Expand Down Expand Up @@ -86,10 +86,10 @@ export default async function Page({ params: { slug } }: PageProps) {

return (
<>
<div className="flex-1 w-full min-w-0 py-12">
<div className="w-full min-w-0 flex-1 py-12">
<main className="mx-auto max-w-[720px]">
{!isLatest && (
<div className="flex flex-col items-start gap-4 p-4 mb-8 text-sm text-red-900 bg-red-200 rounded-md md:flex-row md:items-center md:justify-between md:gap-6 md:py-3 md:pl-5 md:pr-3">
<div className="mb-8 flex flex-col items-start gap-4 rounded-md bg-red-200 p-4 text-sm text-red-900 md:flex-row md:items-center md:justify-between md:gap-6 md:py-3 md:pl-5 md:pr-3">
You are viewing documentation for a previous version of Storybook
<Link
href="/docs"
Expand All @@ -100,7 +100,7 @@ export default async function Page({ params: { slug } }: PageProps) {
</div>
)}
<h1
className="relative mt-0 mb-6 text-4xl font-bold text-black transition-colors duration-200 group-hover:text-blue-500 dark:text-white"
className="relative mb-6 mt-0 text-4xl font-bold text-black transition-colors duration-200 group-hover:text-blue-500 dark:text-white"
data-docs-heading
>
{page.title || 'Title is missing'}
Expand Down
7 changes: 3 additions & 4 deletions apps/frontpage/app/docs/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { Header, Footer, Container } from '@repo/ui';
import Image from 'next/image';
import { fetchGithubCount } from '@repo/utils';
import { Sidebar } from '../../components/docs/sidebar/sidebar';
import { TableOfContent } from '../../components/docs/table-of-content';
import { NavDocs } from '../../components/docs/sidebar/docs-nav';
import { generateDocsTree } from '../../lib/get-tree';
import { getDocsTreeFromPath } from '../../lib/get-docs-tree-from-path';
import { DocsProvider } from './provider';
import { Submenu } from '../../components/docs/submenu';
import { DocsMainNav } from '../../components/docs/sidebar/docs-main-nav';
Expand All @@ -18,7 +17,7 @@ export default async function Layout({ children }: { children: ReactNode }) {
const listofTrees = docsVersions.map((version) => {
return {
version: version.id,
tree: generateDocsTree(`content/docs/${version.id}`),
tree: getDocsTreeFromPath(`content/docs/${version.id}`),
};
});

Expand All @@ -31,7 +30,7 @@ export default async function Layout({ children }: { children: ReactNode }) {
/>
<Image
alt="Storybook Docs"
className="absolute top-0 left-0 w-full -z-10"
className="absolute left-0 top-0 -z-10 w-full"
height={339}
priority
src="/bubbles.png"
Expand Down
4 changes: 2 additions & 2 deletions apps/frontpage/app/docs/sitemap.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MetadataRoute } from 'next';
import { generateDocsTree } from '../../lib/get-tree';
import { getDocsTreeFromPath } from '../../lib/get-docs-tree-from-path';
import { docsVersions } from '@repo/utils';
import { getUrl } from '../../lib/get-url';
import { getFlatTreeSitemap } from '../../lib/get-flat-tree-sitemap';
Expand All @@ -8,7 +8,7 @@ export default function sitemap(): MetadataRoute.Sitemap {
const latestVersion = docsVersions[0];

// Generate docs tree for the latest version only
const tree = generateDocsTree(`content/docs/${latestVersion.id}`);
const tree = getDocsTreeFromPath(`content/docs/${latestVersion.id}`);

// We flatten the tree
const flatTree = getFlatTreeSitemap(tree);
Expand Down
10 changes: 5 additions & 5 deletions apps/frontpage/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Metadata } from 'next';
import { Nunito_Sans as nunitoSans } from 'next/font/google';
import {
GLOBAL_SEARCH_META_KEYS,
GLOBAL_SEARCH_AGNOSTIC,
GLOBAL_SEARCH_IMPORTANCE,
globalSearchMetaKeys,
globalSearchAgnostic,
globalSearchImportance,
} from '@repo/ui';
import { cn } from '@repo/utils';
import { Providers } from './providers';
Expand Down Expand Up @@ -32,8 +32,8 @@ export const metadata: Metadata = {
},
other: {
// Set the docsearch index facets defaults
[GLOBAL_SEARCH_META_KEYS.VERSION]: GLOBAL_SEARCH_AGNOSTIC,
[GLOBAL_SEARCH_META_KEYS.IMPORTANCE]: GLOBAL_SEARCH_IMPORTANCE.AGNOSTIC,
[globalSearchMetaKeys.version]: globalSearchAgnostic,
[globalSearchMetaKeys.importance]: globalSearchImportance.agnostic,
},
verification: {
google: 'HCBwa2qa52ztaOfGjt3FQGzTUCM3kT8IOjTSY6diI88',
Expand Down
4 changes: 2 additions & 2 deletions apps/frontpage/components/docs/footer/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { z } from 'zod';
import type { TreeProps } from '@repo/utils';
import { docsVersions } from '@repo/utils';
import { buildPathWithVersion } from '../../../lib/build-path-with-version';
import { generateDocsTree } from '../../../lib/get-tree';
import { getDocsTreeFromPath } from '../../../lib/get-docs-tree-from-path';

const siteUrl = process.env.VERCEL_ENV === 'production';

Expand Down Expand Up @@ -37,7 +37,7 @@ function addSlugs(tree: TreeProps[]) {
}

versions.forEach((v) => {
const tree = generateDocsTree(`content/docs/${v}`);
const tree = getDocsTreeFromPath(`content/docs/${v}`);
addSlugs(tree);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ function shouldParse(fileOrPath: string) {
return !fileOrPath.endsWith('/versions');
}

export const generateDocsTree = (pathToFiles?: string, docsRoot?: string) => {
export const getDocsTreeFromPath = (
pathToFiles?: string,
docsRoot?: string,
) => {
const newPath = pathToFiles || 'content/docs';
const newDocsRoot = docsRoot || newPath;

Expand All @@ -40,7 +43,7 @@ export const generateDocsTree = (pathToFiles?: string, docsRoot?: string) => {
const isDirectory = fs.lstatSync(filePath).isDirectory();

if (isDirectory) {
const childItems = generateDocsTree(filePath, newDocsRoot);
const childItems = getDocsTreeFromPath(filePath, newDocsRoot);

if (childItems) {
const indexFile = childItems.find(
Expand Down
4 changes: 2 additions & 2 deletions apps/frontpage/lib/get-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
Video,
YouTubeCallout,
} from '../components/docs/mdx';
import { generateDocsTree } from './get-tree';
import { getDocsTreeFromPath } from './get-docs-tree-from-path';

export const getPageData = async (
path: string[],
Expand Down Expand Up @@ -91,7 +91,7 @@ export const getPageData = async (
? `${rootPath}/${pathString}`.split('/').slice(0, -1).join('/')
: `${rootPath}/${pathString}`;

const parent = generateDocsTree(pathToFiles);
const parent = getDocsTreeFromPath(pathToFiles);

const sorted = parent.sort((a, b) =>
a.tab?.order && b.tab?.order ? a.tab.order - b.tab.order : 0,
Expand Down
14 changes: 7 additions & 7 deletions packages/ui/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export const GLOBAL_SEARCH_META_KEYS = {
VERSION: 'docsearch:version',
IMPORTANCE: 'docsearch:importance',
export const globalSearchMetaKeys = {
version: 'docsearch:version',
importance: 'docsearch:importance',
};

export const GLOBAL_SEARCH_AGNOSTIC = 'agnostic';
export const globalSearchAgnostic = 'agnostic';

export const GLOBAL_SEARCH_IMPORTANCE = {
DOCS: 0,
AGNOSTIC: 1,
export const globalSearchImportance = {
docs: 0,
agnostic: 1,
};
7 changes: 2 additions & 5 deletions packages/ui/src/search/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DocSearch } from '@docsearch/react';
import { cn, getVersion } from '@repo/utils';
import { useSelectedLayoutSegment } from 'next/navigation';
import type { HeaderProps } from '../header';
import { GLOBAL_SEARCH_AGNOSTIC } from '../constants';
import { globalSearchAgnostic } from '../constants';

interface SearchProps extends Pick<HeaderProps, 'algoliaApiKey' | 'variant'> {
className?: string;
Expand Down Expand Up @@ -113,10 +113,7 @@ export const Search: FC<SearchProps> = ({
* between the containing values
*/
[`tags:docs`, `tags:recipes`],
[
`version:${activeVersion.id}`,
`version:${GLOBAL_SEARCH_AGNOSTIC}`,
],
[`version:${activeVersion.id}`, `version:${globalSearchAgnostic}`],
],
}}
translations={{
Expand Down