Skip to content

Commit

Permalink
UI: Remove source option
Browse files Browse the repository at this point in the history
  • Loading branch information
fuma-nama committed Sep 19, 2024
1 parent 8ef00dc commit 7a45a17
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 45 deletions.
1 change: 0 additions & 1 deletion apps/docs/app/layout.config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export const docsOptions: DocsLayoutProps = {
links: baseOptions.links?.slice(1),
sidebar: {
tabs: {
source,
transform(option, node) {
const meta = source.getNodeMeta(node);
if (!meta) return option;
Expand Down
38 changes: 19 additions & 19 deletions apps/docs/content/docs/headless/source-api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const source = loader({

With i18n enabled, loader will generate a page tree for every locale.

When looking for a page, it fallbacks to default locale if the page doesn't exist for the specified locale.
When looking for a page, it fallbacks to default locale if the page doesn't exist for specified locale.

## Output

Expand Down Expand Up @@ -121,6 +121,18 @@ source.pageTree;
source.pageTree['locale'];
```

### Get from Node

The page tree nodes contain references to their original file path.
You can find their original page or meta file from the tree nodes.

```ts
import { source } from '@/lib/source';

source.getNodePage(pageNode);
source.getNodeMeta(folderNode);
```

### Params

A function to generate output for Next.js `generateStaticParams`.
Expand Down Expand Up @@ -191,35 +203,23 @@ loader({
### Page Tree

The page tree is generated from your file system, using the **Page Tree Builder**.
It also filters out some unnecessary information (e.g. file path), which means after the generation process, you cannot access the file path of a page node.
It also filters out some unnecessary information (e.g. unused frontmatter properties).

To customise the process, use the `pageTree` option.
You can attach custom properties to page tree nodes, like customising the display name of pages and folders.

```ts
```tsx
import React from 'react';
import { loader } from 'fumadocs-core/source';

loader({
pageTree: {
attachFile(node, file) {
// you can access its file information
console.log(file.data);

return node;
},
},
});
```

JSX nodes are also allowed.

```tsx
import { loader } from 'fumadocs-core/source';

loader({
pageTree: {
attachFile(node) {
console.log(file?.data);
// JSX nodes are allowed
node.name = <>Some JSX Nodes here</>;

return node;
},
},
Expand Down
15 changes: 0 additions & 15 deletions apps/docs/content/docs/ui/blocks/layout.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -64,31 +64,16 @@ Add `description` to the meta file of root folder.
}
```

And pass your source object to sidebar.

```tsx
import { DocsLayout } from 'fumadocs-ui/layouts/docs';
import { source } from '@/lib/source';

<DocsLayout
sidebar={{
tabs: { source },
}}
/>;
```

#### Decoration

Change the icon/styles of tabs.

```tsx
import { DocsLayout } from 'fumadocs-ui/layouts/docs';
import { source } from '@/lib/source';

<DocsLayout
sidebar={{
tabs: {
source,
transform: (option, node) => ({
...option,
icon: 'my icon',
Expand Down
12 changes: 2 additions & 10 deletions packages/ui/src/utils/get-sidebar-tabs.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
import type { PageTree } from 'fumadocs-core/server';
import type { LoaderConfig, LoaderOutput } from 'fumadocs-core/source';
import type { Option } from '@/components/layout/root-toggle';

export interface TabOptions {
source?: LoaderOutput<LoaderConfig>;
transform?: (option: Option, node: PageTree.Folder) => Option;
}

export function getSidebarTabs(
pageTree: PageTree.Root,
{ source, transform }: TabOptions = {},
{ transform }: TabOptions = {},
): Option[] {
const options: Option[] = [];

function traverse(node: PageTree.Node): void {
if (node.type === 'folder' && node.root && node.index) {
const meta = source?.getNodeMeta(node);
const option: Option = {
url: node.index.url,
title: node.name,
icon: node.icon,
description:
meta &&
'description' in meta.data &&
typeof meta.data.description === 'string'
? meta.data.description
: undefined,
description: node.description,
};

options.push(transform ? transform(option, node) : option);
Expand Down

0 comments on commit 7a45a17

Please sign in to comment.