# Fumadocs Core (the core library of Fumadocs): 協商
URL: /zh-TW/docs/headless/utils/negotiation
Source: https://raw.githubusercontent.com/fuma-nama/fumadocs/refs/heads/main/apps/docs/content/docs/headless/utils/negotiation.zh-TW.mdx

內容協商工具函式。

## 概覽 [#overview]

若您使用的 React 框架支援中介軟體，可以透過 Fumadocs 的協商 API 實作基本功能。
在 Waku 上，請在 `src/middleware` 的 [Hono 中介軟體](https://hono.dev/docs/guides/middleware)中使用它。
在 Astro 上，請在[中介軟體](https://docs.astro.build/en/guides/middleware)中使用它；此中介軟體只會在依請求算繪的頁面上執行。

### 接受 Markdown [#accept-markdown]

依據 `Accept` 標頭回傳 Markdown 或 HTML，可改善 AI 代理的使用體驗。

```ts tab="Next.js" title="proxy.ts"
import { NextRequest, NextResponse } from 'next/server';
import { isMarkdownPreferred, rewritePath } from 'fumadocs-core/negotiation';

const { rewrite: rewriteLLM } = rewritePath('/docs/*path', '/llms.mdx/*path');

export default function proxy(request: NextRequest) {
  if (isMarkdownPreferred(request)) {
    const result = rewriteLLM(request.nextUrl.pathname);

    if (result) {
      return NextResponse.rewrite(new URL(result, request.nextUrl));
    }
  }

  return NextResponse.next();
}
```

```tsx tab="React Router" title="app/root.tsx"
import type { Route } from './+types/root';
import { isMarkdownPreferred, rewritePath } from 'fumadocs-core/negotiation';

const { rewrite: rewriteLLM } = rewritePath('/docs/*path', '/llms.mdx/*path');

const serverMiddleware: Route.MiddlewareFunction = async ({ request }, next) => {
  const url = new URL(request.url);
  const result = isMarkdownPreferred(request) && rewriteLLM(url.pathname);

  // `Response.redirect()` 的 headers 無法修改，因此直接建立回應
  if (result)
    return new Response(null, {
      status: 302,
      headers: { Location: new URL(result, url).toString(), Vary: 'Accept' },
    });

  return next();
};

export const middleware = [serverMiddleware];
```

```ts tab="TanStack Start" title="src/start.ts"
import { createMiddleware, createStart } from '@tanstack/react-start';
import { redirect } from '@tanstack/react-router';
import { isMarkdownPreferred, rewritePath } from 'fumadocs-core/negotiation';

const { rewrite: rewriteLLM } = rewritePath('/docs/*path', '/llms.mdx/*path');

const llmMiddleware = createMiddleware().server(({ next, request }) => {
  const url = new URL(request.url);
  const result = isMarkdownPreferred(request) && rewriteLLM(url.pathname);

  // 重新導向至 Markdown URL
  if (result) throw redirect({ href: new URL(result, url).href, headers: { Vary: 'Accept' } });

  return next();
});

export const startInstance = createStart(() => ({
  requestMiddleware: [llmMiddleware],
}));
```