-
Notifications
You must be signed in to change notification settings - Fork 9
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 1 commit
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
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,121 @@ | ||
'use strict'; | ||
|
||
import { writeFile } from 'node:fs/promises'; | ||
import { join } from 'node:path'; | ||
import { DOC_API_LATEST_BASE_URL } from '../../constants.mjs'; | ||
|
||
const IGNORE_LIST = ['doc/api/synopsis.md']; | ||
|
||
/** | ||
* 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) { | ||
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 | ||
*/ | ||
function paragraphToString(node) { | ||
if (node.type !== 'paragraph') { | ||
throw new Error('Node is not a paragraph'); | ||
} | ||
|
||
return node.children.map(extractTextContent).join(''); | ||
} | ||
|
||
/** | ||
* Generates a documentation entry string | ||
* | ||
* @param {ApiDocMetadataEntry} entry | ||
* @returns {string} | ||
*/ | ||
function generateDocEntry(entry) { | ||
if (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}](${DOC_API_LATEST_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}`); | ||
return `- ${entryLink}`; | ||
} | ||
|
||
const description = paragraphToString(descriptionNode).replace( | ||
/[\r\n]+/g, | ||
' ' | ||
); | ||
|
||
return `- ${entryLink}: ${description}`; | ||
} | ||
araujogui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* @typedef {Array<ApiDocMetadataEntry>} Input | ||
* | ||
* @type {GeneratorMetadata<Input, string>} | ||
*/ | ||
export default { | ||
name: 'llms-txt', | ||
version: '0.1.0', | ||
araujogui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 output = [ | ||
'# Node.js Documentation', | ||
araujogui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'> 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.
Show resolved
Hide resolved
|
||
`- [About this documentation](${DOC_API_LATEST_BASE_URL}/api/documentation.md)`, | ||
`- [Usage and example](${DOC_API_LATEST_BASE_URL}/api/synopsis.md)`, | ||
'## API Documentation', | ||
araujogui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
]; | ||
araujogui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const docEntries = input.map(generateDocEntry).filter(Boolean); | ||
|
||
output.push(...docEntries); | ||
|
||
const resultText = output.join('\n'); | ||
|
||
if (options.output) { | ||
await writeFile(join(options.output, 'llms.txt'), resultText); | ||
} | ||
|
||
return resultText; | ||
}, | ||
}; |
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.