API Endpoint: LLM Docs
Relevant source files
The following files were used as context for generating this wiki page:
The llms-docs.md.ts API endpoint is a server-side function designed to generate a single, consolidated plain-text file containing the entirety of the Railway documentation. Its primary purpose is to create a corpus of text that is optimized for consumption by Large Language Models (LLMs).
This endpoint programmatically traverses the documentation structure, reads each individual markdown file, cleans and formats the content by stripping out non-textual elements, and concatenates everything into a structured text file. This process ensures that an LLM can be trained or provided with a comprehensive and clean knowledge base of the Railway platform.
Endpoint Definition
The API is exposed as a standard Next.js API route.
| Property | Value | Description |
|---|---|---|
| Path | /api/llms-docs.md | The URL path to access the endpoint. |
| HTTP Method | GET | The endpoint responds to GET requests. |
| Response Type | text/plain | The Content-Type header is set to text/plain. |
Sources: src/pages/api/llms-docs.md.ts:8-11, src/middleware.ts:12-14
Core Logic and Data Flow
The handler executes a series of steps to aggregate and process the documentation content. The overall flow begins with reading the documentation structure and ends with sending the complete text file as a response.
The following diagram illustrates the high-level process of the API handler.
Sources: src/pages/api/llms-docs.md.ts:10-123
The sequence of interactions between the API handler and other system components is shown below.
Sources: src/pages/api/llms-docs.md.ts:60-65
Content Aggregation and Processing
Documentation Structure
The structure of the documentation is defined in src/data/sidebar.ts. This file exports a sidebarContent array, which is a hierarchical representation of all documentation sections and pages. It uses types like IPage, ISidebarSection, and ISubSection defined in src/types.ts to organize the content.
Sources: src/data/sidebar.ts:10, src/types.ts:5-22
Page Flattening and Grouping
-
Flattening: The
flattenSidebarContentutility function, imported fromsrc/layouts/DocsLayout.tsx, is used to convert the hierarchicalsidebarContentinto a flat array ofIPageobjects. This provides a simple list of all documentation pages.
Sources: src/pages/api/llms-docs.md.ts:14, src/layouts/DocsLayout.tsx:34-57 -
Grouping: The handler then groups these flattened pages by their top-level section title. It initializes an object with keys for each section title from
sidebarContentand populates the arrays with the correspondingIPageobjects.
Sources: src/pages/api/llms-docs.md.ts:21-49
Markdown File Processing
For each IPage object, the handler performs the following steps:
- Constructs the local file path to the markdown source file located in
src/docs. - Reads the file's content synchronously using
fs.readFileSync. - Uses the
gray-matterlibrary to parse the file content, separating the YAML frontmatter (liketitleanddescription) from the main markdown body. The structure of this frontmatter is defined incontentlayer.config.ts.
Sources: src/pages/api/llms-docs.md.ts:58-65, contentlayer.config.ts:6-20
Content Cleaning and Formatting
To make the content suitable for an LLM, several cleaning operations are performed on the raw markdown. The goal is to retain textual information while removing formatting and interactive elements.
| Pattern | Replacement | Purpose |
|---|---|---|
/```[\s\S]*?```/g | "" | Remove multi-line code blocks. |
/([^]+)/g ` | "$1" | Remove backticks from inline code. |
/\[([^\]]+)\]\(([^)]+)\)/g | "$1" | Remove markdown links, keeping only the link text. |
Sources: src/pages/api/llms-docs.md.ts:84-86, src/pages/api/llms-docs.md.ts:101-103
The content is also split by ## headings to process it section by section. After the main text content is cleaned and appended, the handler separately extracts all code blocks using a regex and appends them under a ## Code Examples heading. This preserves valuable code examples in a dedicated section for each page.
Sources: src/pages/api/llms-docs.md.ts:109-118
Summary
The llms-docs.md API endpoint is a crucial data-processing pipeline that transforms the project's human-readable documentation into a machine-readable format. By systematically collecting, cleaning, and structuring content from across the repository, it produces a single, comprehensive text file. This output serves as a foundational dataset for powering LLM-based features, such as AI-powered search, chatbots, or automated support, within the Railway ecosystem.