-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c8d089c
commit 9b8fdc4
Showing
36 changed files
with
652 additions
and
641 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
packages/fern-docs/bundle/src/app/[[...slug]]/llms-full.txt.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { DocsLoader } from "@/server/DocsLoader"; | ||
import { getMarkdownForPath } from "@/server/getMarkdownForPath"; | ||
import { getSectionRoot } from "@/server/getSectionRoot"; | ||
import { getDocsDomainEdge, getHostEdge } from "@/server/xfernhost/edge"; | ||
import { FernNavigation } from "@fern-api/fdr-sdk"; | ||
import { CONTINUE, SKIP } from "@fern-api/fdr-sdk/traversers"; | ||
import { isNonNullish } from "@fern-api/ui-core-utils"; | ||
import { getFeatureFlags } from "@fern-docs/edge-config"; | ||
import { addLeadingSlash, COOKIE_FERN_TOKEN } from "@fern-docs/utils"; | ||
import { uniqBy } from "es-toolkit/array"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
|
||
export async function handleLLMSFullTxt( | ||
req: NextRequest, | ||
{ params }: { params: { slug?: string[] } } | ||
): Promise<NextResponse> { | ||
const slug = params.slug ?? []; | ||
const path = addLeadingSlash(slug.join("/")); | ||
const domain = getDocsDomainEdge(req); | ||
const host = getHostEdge(req); | ||
const fern_token = req.cookies.get(COOKIE_FERN_TOKEN)?.value; | ||
const featureFlags = await getFeatureFlags(domain); | ||
const loader = DocsLoader.for(domain, host, fern_token).withFeatureFlags( | ||
featureFlags | ||
); | ||
|
||
const root = getSectionRoot(await loader.root(), path); | ||
|
||
if (root == null) { | ||
return NextResponse.json(null, { status: 404 }); | ||
} | ||
|
||
const nodes: FernNavigation.NavigationNodePage[] = []; | ||
|
||
FernNavigation.traverseDF(root, (node) => { | ||
if (FernNavigation.hasMetadata(node)) { | ||
if (node.hidden || node.authed) { | ||
return SKIP; | ||
} | ||
} | ||
|
||
if (FernNavigation.isPage(node)) { | ||
nodes.push(node); | ||
} | ||
|
||
return CONTINUE; | ||
}); | ||
|
||
const markdowns = ( | ||
await Promise.all( | ||
uniqBy( | ||
nodes, | ||
(a) => FernNavigation.getPageId(a) ?? a.canonicalSlug ?? a.slug | ||
).map(async (node) => { | ||
const markdown = await getMarkdownForPath(node, loader, featureFlags); | ||
if (markdown == null) { | ||
return undefined; | ||
} | ||
return markdown.content; | ||
}) | ||
) | ||
).filter(isNonNullish); | ||
|
||
if (markdowns.length === 0) { | ||
return NextResponse.json(null, { status: 404 }); | ||
} | ||
|
||
return new NextResponse(markdowns.join("\n\n"), { | ||
status: 200, | ||
headers: { | ||
"Content-Type": "text/plain; charset=utf-8", | ||
"X-Robots-Tag": "noindex", | ||
"Cache-Control": "s-maxage=60", | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { DocsLoader } from "@/server/DocsLoader"; | ||
import { | ||
getMarkdownForPath, | ||
getPageNodeForPath, | ||
} from "@/server/getMarkdownForPath"; | ||
import { getDocsDomainEdge, getHostEdge } from "@/server/xfernhost/edge"; | ||
import { getFeatureFlags } from "@fern-docs/edge-config"; | ||
import { addLeadingSlash, COOKIE_FERN_TOKEN } from "@fern-docs/utils"; | ||
import { notFound } from "next/navigation"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
|
||
/** | ||
* This endpoint returns the markdown content of any page in the docs by adding `.md` or `.mdx` to the end of any docs page. | ||
*/ | ||
|
||
export async function handleMarkdown( | ||
req: NextRequest, | ||
{ params }: { params: { slug?: string[] } } | ||
): Promise<NextResponse> { | ||
const slug = params.slug ?? []; | ||
const path = addLeadingSlash(slug.join("/")); | ||
const domain = getDocsDomainEdge(req); | ||
const host = getHostEdge(req); | ||
const fern_token = req.cookies.get(COOKIE_FERN_TOKEN)?.value; | ||
const featureFlags = await getFeatureFlags(domain); | ||
const loader = DocsLoader.for(domain, host, fern_token).withFeatureFlags( | ||
featureFlags | ||
); | ||
|
||
const node = getPageNodeForPath(await loader.root(), path); | ||
console.log(path, node); | ||
if (node == null) { | ||
notFound(); | ||
} | ||
|
||
// If the page is authed, but the user is not authed, return a 403 | ||
if (node.authed && !(await loader.isAuthed())) { | ||
return new NextResponse(null, { status: 403 }); | ||
} | ||
|
||
const markdown = await getMarkdownForPath(node, loader, featureFlags); | ||
if (markdown == null) { | ||
notFound(); | ||
} | ||
|
||
return new NextResponse(markdown.content, { | ||
status: 200, | ||
headers: { | ||
"Content-Type": `text/${markdown.contentType}`, | ||
"X-Robots-Tag": "noindex", // prevent search engines from indexing this page | ||
"Cache-Control": "s-maxage=60", // cannot guarantee that the content won't change, so we only cache for 60 seconds | ||
}, | ||
}); | ||
} | ||
|
||
export async function OPTIONS(): Promise<NextResponse> { | ||
return new NextResponse(null, { | ||
status: 200, | ||
headers: { | ||
"X-Robots-Tag": "noindex", | ||
Allow: "OPTIONS, GET", | ||
}, | ||
}); | ||
} |
Oops, something went wrong.