-
Notifications
You must be signed in to change notification settings - Fork 22
Adhere to new /llms.txt standard
#252
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1bf06a5
fix local build issues
MattPereira a386935
add custom plugin for generating llms.txt files during build process
MattPereira a8df8be
troubleshoot vercel build errors
MattPereira a60bbfc
fix start of llms.txt file missing title
MattPereira d42ec5d
serve .md files for each docs content page
MattPereira 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,107 @@ | ||
| import type { Plugin } from '@vuepress/core' | ||
| import { fs, path } from '@vuepress/utils' | ||
| import { globby } from 'globby' | ||
| import { unified } from 'unified' | ||
| import remarkParse from 'remark-parse' | ||
| import remarkStringify from 'remark-stringify' | ||
| import remarkFrontmatter from 'remark-frontmatter' | ||
| import { visit } from 'unist-util-visit' | ||
| import { toMarkdown } from 'mdast-util-to-markdown' | ||
| import type { Heading } from 'mdast' | ||
| import { directiveToMarkdown } from 'mdast-util-directive' | ||
| import remarkDirective from 'remark-directive' | ||
| import { gfmToMarkdown } from 'mdast-util-gfm' | ||
|
|
||
| /** | ||
| * Generates the following files and serves them at the appropriate paths: | ||
| * - llms.txt | ||
| * - llms-full.txt | ||
| * - .md version of each docs page like https://docs.balancer.fi/concepts/core-concepts/introduction.md | ||
| */ | ||
| export const llmsTxtPlugin = (): Plugin => ({ | ||
| name: 'llms-txt-generator', | ||
|
|
||
| onGenerated: async (app) => { | ||
|
|
||
| const outDir = app.dir.dest() | ||
| const docsDir = app.dir.source() | ||
|
|
||
| const glob = `${docsDir}/**/*.md` | ||
| const files = await globby(glob); | ||
|
|
||
| const content = [`# Balancer V3`, '', '> Learn, integrate, and build on a programmable AMM', ''] | ||
| const llmsTxtContent = [...content, '## Docs', '']; | ||
| const llmsFullTxtContent = content; | ||
|
|
||
| for (const file of files) { | ||
| let pagePath = `${file.replace(docsDir, '').replace(/\.[^.]*$/, '')}.md` | ||
| if (!pagePath) continue | ||
|
|
||
|
|
||
| const contents = await fs.readFile(file, 'utf-8'); | ||
| const parser = unified().use(remarkParse).use(remarkFrontmatter).use(remarkDirective).use(remarkStringify) | ||
|
|
||
| // Abstract syntax tree from the contents of all the markdown files | ||
| const ast = parser.parse(contents) | ||
|
|
||
| // For links to pages served at /llms.txt | ||
| visit(ast, { type: 'heading', depth: 1 }, (n, i) => { | ||
| const node = n.children[0] | ||
| if (node.type !== 'text') return | ||
|
|
||
| const value = node.value | ||
| const [, title, subTitle] = value.match(/^([^\[\]]+)(?: \[([^\[\]]+)\])?$/) ?? [] | ||
|
|
||
| // Look for a description: either use subtitle or first paragraph after heading | ||
| let found = false | ||
| let description = subTitle | ||
| if (!description) | ||
| visit(ast, { type: 'paragraph' }, (n, j) => { | ||
| if (found) return | ||
| if (j && i && j <= i) return | ||
|
|
||
| found = true | ||
| description = toMarkdown(n, {extensions: [directiveToMarkdown(), gfmToMarkdown()]}) | ||
|
|
||
| // Remove container directive syntax ( only for llms.txt ) | ||
| description = description.replace(/^.*\\:::.*(?:\n|$)/gm, '').replace(/\n+$/, ''); | ||
| }) | ||
|
|
||
| llmsTxtContent.push(`- [${title}](https://docs.balancer.fi${pagePath})${description ? `: ${description}` : ''}`) | ||
| }) | ||
|
|
||
| // Adjust depth of headings to make sense given site title is H1 | ||
| visit( | ||
| ast, | ||
| (n) => n.type === 'heading', | ||
| (n) => { | ||
| const node = n as Heading | ||
| if (node.depth === 1 || node.depth === 2 || node.depth === 3 || node.depth === 4) | ||
| node.depth = (node.depth + 1) as 2 | 3 | 4 | 5 | ||
| }, | ||
| ) | ||
|
|
||
| // remove front matter | ||
| visit(ast, { type: 'yaml' }, (_, i, p) => { | ||
| if (!p) return | ||
| if (typeof i !== 'number') return | ||
| p.children.splice(i, 1) | ||
| }) | ||
|
|
||
| const llmFriendlyMarkdown = toMarkdown(ast, {extensions: [directiveToMarkdown(), gfmToMarkdown()]}) | ||
|
|
||
| // Serve the .md version of each page alongside the .html | ||
| const mdOutputPath = path.join(outDir, pagePath) | ||
| fs.writeFileSync(mdOutputPath, llmFriendlyMarkdown) | ||
|
|
||
| llmsFullTxtContent.push( llmFriendlyMarkdown, '') | ||
| } | ||
|
|
||
| const llmsTxt = llmsTxtContent.join('\n') | ||
| const llmsFullTxt = llmsFullTxtContent.join('\n') | ||
|
|
||
| fs.ensureDirSync(outDir) | ||
| fs.writeFileSync(path.resolve(outDir, 'llms.txt'), llmsTxt) | ||
| fs.writeFileSync(path.resolve(outDir, 'llms-full.txt'), llmsFullTxt) | ||
| }, | ||
| }) |
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 |
|---|---|---|
|
|
@@ -39,4 +39,4 @@ features: | |
| details: All things data | ||
| link: /data-and-analytics/README.md | ||
|
|
||
|
|
||
| --- | ||
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 |
|---|---|---|
|
|
@@ -4,6 +4,8 @@ order: 4 | |
|
|
||
| # Error signatures | ||
|
|
||
| Catalogue for decoding custom error signatures into their associated error names | ||
|
|
||
|
Comment on lines
+7
to
+8
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Short descriptions at top of each page are helpful for llms.txt file |
||
| | Error Name | Parameters | Error Signature | | ||
| | ------------------------------------ | ------------------------------------------------------------ | --------------- | | ||
| | AfterAddLiquidityHookFailed | | `0x1f11c6e8` | | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need valid frontmatter to parse the abstract syntax tree properly