-
Notifications
You must be signed in to change notification settings - Fork 40
feat: create llms.txt generator #254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
21bb6e2
feat: create llms.txt generator
araujogui 277f607
refactor: improvements
araujogui e585088
fix: typo
araujogui 32fb861
refactor: remove paragraphToString util
araujogui b074a7f
refactor: some improvements
araujogui 646b826
fix: doc api url path
araujogui 419dcd6
fix: doc api url path
araujogui ebc4828
refactor: some changes
araujogui 28aaa10
feat: add llm_description prop
araujogui f00fe97
test: add llm_description prop
araujogui 1bf3748
refacotr: remove template replace
araujogui 9904ecb
feat(linter): create llm description rule
araujogui ee63084
refactor(linter): remove for-of loop
araujogui c8d86c7
refactor(llms-txt): remove docs url suffix
araujogui b4dbc2d
fix: base url const
araujogui File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,3 @@ | ||
| export const ENTRY_IGNORE_LIST = ['doc/api/synopsis.md']; | ||
|
|
||
| export const LATEST_DOC_API_BASE_URL = 'https://nodejs.org/docs/latest'; | ||
This file contains hidden or 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,47 @@ | ||
| 'use strict'; | ||
|
|
||
| import { readFile, writeFile } from 'node:fs/promises'; | ||
| import { join } from 'node:path'; | ||
| import { generateDocEntry } from './utils/generateDocEntry.mjs'; | ||
| import { LATEST_DOC_API_BASE_URL } from './constants.mjs'; | ||
|
|
||
| /** | ||
| * @typedef {Array<ApiDocMetadataEntry>} Input | ||
| * | ||
| * @type {GeneratorMetadata<Input, string>} | ||
| */ | ||
| export default { | ||
| name: 'llms-txt', | ||
| version: '1.0.0', | ||
| description: 'Generates a llms.txt file of the API docs', | ||
| dependsOn: 'ast', | ||
|
|
||
| /** | ||
| * @param {Input} input The API documentation metadata | ||
| * @param {Partial<GeneratorOptions>} options Generator options | ||
| * @returns {Promise<string>} The generated documentation text | ||
| */ | ||
| async generate(input, options) { | ||
| const template = await readFile( | ||
| join(import.meta.dirname, 'template.txt'), | ||
| 'utf-8' | ||
| ); | ||
|
|
||
| const apiDocEntries = input.map(generateDocEntry).filter(Boolean); | ||
|
|
||
| const introductionEntries = [ | ||
| `- [About this documentation](${LATEST_DOC_API_BASE_URL}/api/documentation.md)`, | ||
araujogui marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| `- [Usage and examples](${LATEST_DOC_API_BASE_URL}/api/synopsis.md)`, | ||
| ]; | ||
|
|
||
| const filledTemplate = template | ||
| .replace('__INTRODUCTION__', introductionEntries.join('\n')) | ||
| .replace('__API_DOCS__', apiDocEntries.join('\n')); | ||
|
|
||
| if (options.output) { | ||
| await writeFile(join(options.output, 'llms.txt'), filledTemplate); | ||
| } | ||
|
|
||
| return filledTemplate; | ||
| }, | ||
| }; | ||
This file contains hidden or 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,9 @@ | ||
| # Node.js Documentation | ||
|
|
||
| > Node.js is an open-source, cross-platform JavaScript runtime environment that executes JavaScript code outside a web browser. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient for building scalable network applications. | ||
|
|
||
| ## Introduction | ||
araujogui marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| __INTRODUCTION__ | ||
|
|
||
| ## API Documentations | ||
| __API_DOCS__ | ||
This file contains hidden or 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,39 @@ | ||
| import { ENTRY_IGNORE_LIST, LATEST_DOC_API_BASE_URL } from '../constants.mjs'; | ||
| import { paragraphToString } from './paragraphToString.mjs'; | ||
|
|
||
| /** | ||
| * Generates a documentation entry string | ||
| * | ||
| * @param {ApiDocMetadataEntry} entry | ||
| * @returns {string} | ||
| */ | ||
| export function generateDocEntry(entry) { | ||
| if (ENTRY_IGNORE_LIST.includes(entry.api_doc_source)) { | ||
| return null; | ||
| } | ||
|
|
||
| if (entry.heading.depth !== 1) { | ||
| return null; | ||
| } | ||
|
|
||
| // Remove the leading /doc of string | ||
| const path = entry.api_doc_source.replace(/^doc\//, ''); | ||
|
|
||
| const entryLink = `[${entry.heading.data.name}](${LATEST_DOC_API_BASE_URL}/${path})`; | ||
|
|
||
| const descriptionNode = entry.content.children.find( | ||
| child => child.type === 'paragraph' | ||
| ); | ||
|
|
||
| if (!descriptionNode) { | ||
| console.warn(`No description found for entry: ${entry.api_doc_source}`); | ||
araujogui marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return `- ${entryLink}`; | ||
| } | ||
|
|
||
| const description = paragraphToString(descriptionNode).replace( | ||
| /[\r\n]+/g, | ||
| ' ' | ||
| ); | ||
|
|
||
| return `- ${entryLink}: ${description}`; | ||
| } | ||
This file contains hidden or 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,36 @@ | ||
| /** | ||
| * Extracts text content from a node recursively | ||
| * | ||
| * @param {import('mdast').Paragraph} node The AST node to extract text from | ||
| * @returns {string} The extracted text content | ||
| */ | ||
| function extractTextContent(node) { | ||
araujogui marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (!node) { | ||
| return ''; | ||
| } | ||
|
|
||
| if (node.type === 'text' || node.type === 'inlineCode') { | ||
| return node.value; | ||
| } | ||
|
|
||
| if (node.children && Array.isArray(node.children)) { | ||
| return node.children.map(extractTextContent).join(''); | ||
| } | ||
|
|
||
| return ''; | ||
| } | ||
|
|
||
| /** | ||
| * Extracts text from a paragraph node. | ||
| * | ||
| * @param {import('mdast').Paragraph} node The paragraph node to extract text from | ||
| * @returns {string} The extracted text content | ||
| * @throws {Error} If the node is not a paragraph | ||
| */ | ||
| export function paragraphToString(node) { | ||
araujogui marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (node.type !== 'paragraph') { | ||
| throw new Error('Node is not a paragraph'); | ||
| } | ||
|
|
||
| return node.children.map(extractTextContent).join(''); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.