# Fumadocs (Framework Mode, Astro): Generating Pages
URL: /docs/astro/integrations/asyncapi/generate-files
Source: https://raw.githubusercontent.com/fuma-nama/fumadocs/refs/heads/main/apps/docs/content/docs/(framework)/[framework]/integrations/asyncapi/generate-files.mdx

Generate pages from AsyncAPI schema.

`staticSource()` and `dynamicSource()` generate pages for your [`loader()`](/docs/astro/integrations/asyncapi#generate-pages), while `generateFiles()` writes them into your content directory as MDX files. They accept the same options.

`<AsyncAPIPage />` still renders the content of pages. The generator only arranges the operations of each page.

## Options [#options]

### `per` [#per]

Customize the content of pages, default to `operation`.

<Callout>
Operation refers to an AsyncAPI operation with a specific action like
`receiveLightMeasurement:receive`.
</Callout>

| mode      | content                        | file path           |
| --------- | ------------------------------ | ------------------- |
| tag       | operations with same tag       | `{tag_name}.mdx`    |
| file      | operations in same schema file | `{file_name}.mdx`   |
| operation | each operation                 | `{operationId}.mdx` |
| custom    | see below                      | N/A                 |

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

await asyncapi.staticSource({
  per: 'tag',
});
```

When set to `custom`, you can pass a function to fully customize the generation process:

```ts title="Example"
import { asyncapi } from '@/lib/asyncapi';

await asyncapi.staticSource({
  per: 'custom',
  toPages(builder) {
    for (const [id, item] of Object.entries(builder.document.operations ?? {})) {
      const operation = builder.dereferenceShallow(item);

      builder.create({
        type: 'operation',
        schemaId: builder.id,
        item: { id, action: operation.action },
        path: `${id}.mdx`,
        info: {
          title: operation.title ?? id,
          description: operation.description,
        },
      });
    }
  },
});
```

When set to `tag`, tags are matched by name. Define them in `info.tags` to give their pages a description, and `x-displayName` to control the title. Operations without tags are under `unknown`.

```yaml title="asyncapi.yaml"
info:
  tags:
    - name: lighting
      description: Operations related to streetlights.
      x-displayName: Lighting
```

### `groupBy` [#groupby]

In `operation` mode, you can group output files with folders.

| value          | output                            |
| -------------- | --------------------------------- |
| tag            | `{tag}/{operationId}.mdx`         |
| channel        | `{channel key}/{operationId}.mdx` |
| none (default) | `{operationId}.mdx`               |

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

await asyncapi.staticSource({
  per: 'operation',
  groupBy: 'tag',
});
```

### `name` [#name]

A function that controls the file name of generated pages.

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

await asyncapi.staticSource({
  name(output) {
    if (output.type === 'operation') {
      const operation = this.dereferenceShallow(this.document.operations?.[output.item.id]);
      console.log(operation);

      return `my-dir/${output.item.id}`;
    }

    return 'my-dir/filename';
  },
});
```

You can also use the `algorithm` option:

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

await asyncapi.staticSource({
  // v2 (default): use operation id
  // v1: use `{operationId}/{action}`
  name: { algorithm: 'v1' },
});
```

### `meta` [#meta]

Generate [`meta.json`](/docs/astro/page-conventions#meta) files automatically.

You can always define the `meta.json` files manually for maximal flexibility.

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

await asyncapi.staticSource({
  meta: true,
});
```

Or customize how folders are represented:

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

await asyncapi.staticSource({
  meta: {
    folderStyle: 'separator',
  },
});
```

## MDX Files [#mdx-files]

To write pages as MDX files, create a script:

```ts title="scripts/generate-docs.ts"
import { generateFiles } from '@fumadocs/asyncapi';
import { asyncapi } from '@/lib/asyncapi';

void generateFiles({
  input: asyncapi,
  output: './content/docs',
  // we recommend to enable it
  // make sure your operation description doesn't break MDX syntax.
  includeDescription: true,
});
```

Generate docs with the script:

```bash
bun ./scripts/generate-docs.ts
```

Add the `AsyncAPIPage` component to your MDX components. Generated files use the `<AsyncAPIPage />` tag.

```tsx title="src/components/mdx.tsx"
import { AsyncAPIPage } from '@/components/api-page'; // [!code ++]

export default getMDXComponents({
  wrapper: DocsBody,
  // [!code ++]
  AsyncAPIPage: (props) => <AsyncAPIPage {...props} preloaded={{ docs: {} }} />,
});
```

The MDX components module is shared by every page, so it can't preload documents per page. Pass [`loadDocument`](/docs/astro/integrations/asyncapi/server#loading-documents-once) to load them instead.

Add the plugin to your `loader()`:

```ts title="lib/source.ts"
import { asyncapi } from '@/lib/asyncapi';
import { loader } from 'fumadocs-core/source';

export const source = loader({
  // [!code ++:2] optional: adds the direction of messages to each page item in page tree
  plugins: [asyncapi.loaderPlugin()],
});
```

`generateFiles()` also accepts the options below.

### `index` [#index]

Generate index files with cards linking to generated pages.

```ts
import { generateFiles } from '@fumadocs/asyncapi';

void generateFiles({
  index: {
    // for generating `href`
    url: {
      baseUrl: '/docs',
      contentDir: './content/docs',
    },
    items: [
      {
        // output path
        path: 'index.mdx',
        // optional: restrict the input files (identical to the `input` field in server)
        only: ['streetlights.yaml'],
        // optional: set frontmatter
        description: 'All available pages',
      },
    ],
  },
});
```

### `imports` [#imports]

Add custom imports to generated MDX files. This is useful for including components, utilities, or other dependencies in your generated documentation.

```ts
import { generateFiles } from '@fumadocs/asyncapi';

void generateFiles({
  imports: [
    {
      names: ['API_BASE_URL'],
      from: '@/constants',
    },
  ],
});
```

This will add the following imports to all generated MDX files:

```ts
import { API_BASE_URL } from '@/constants';
```

### `frontmatter` [#frontmatter]

Customize the frontmatter of MDX files.

By default, it includes:

| property      | description                        |
| ------------- | ---------------------------------- |
| `title`       | Page title                         |
| `description` | Page description                   |
| `full`        | Always true, added for Fumadocs UI |
| `_asyncapi`   | Internal Properties                |

```ts
import { generateFiles } from '@fumadocs/asyncapi';

void generateFiles({
  frontmatter: (title, description) => ({
    myProperty: 'hello',
  }),
});
```

### `addGeneratedComment` [#addgeneratedcomment]

Add a comment to the top of generated files indicating they are auto-generated.

```ts
import { generateFiles } from '@fumadocs/asyncapi';

void generateFiles({
  // Add default comment
  addGeneratedComment: true,

  // Or provide a custom comment
  addGeneratedComment: 'Custom auto-generated comment',

  // Or disable comments
  addGeneratedComment: false,
});
```