-
Notifications
You must be signed in to change notification settings - Fork 74
feat(llms-txt): write each page's Markdown and llms-full.txt #1127
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
Open
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a88459e
feat(llms-txt): write each page's Markdown and llms-full.txt
ovflowd 6f31cee
test(llms-txt): cover building the pages' Markdown
ovflowd 91c094e
docs(llms-txt): document writeMarkdown and writeFull, add a changeset
ovflowd fc26ff3
refactor(react): move llms-full.txt into a llms-txt-full generator
ovflowd 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@doc-kit/core': minor | ||
| '@doc-kit/generator-react': minor | ||
| --- | ||
|
|
||
| Add the `llms-txt-full` generator, writing a `llms-full.txt` file holding the Markdown of every page, each preceded by its URL. | ||
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
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
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 @@ | ||
| # `llms-txt-full` Generator | ||
|
|
||
| The `llms-txt-full` generator creates a `llms-full.txt` file holding the Markdown of every page, each preceded by its URL, so Large Language Models (LLMs) can read the whole documentation in one request. Pages are rebuilt from their metadata entries, leaving out the ones other generators create. | ||
|
|
||
| ## Configuring | ||
|
|
||
| - `output` {string} The directory where `llms-full.txt` will be written. | ||
| - `pageURL` {string} URL template for the URL preceding each page. | ||
| **Default:** `'{baseURL}{path}.md'`. |
29 changes: 29 additions & 0 deletions
29
packages/react/src/llms-txt-full/__tests__/generate.test.mjs
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,29 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { describe, it } from 'node:test'; | ||
|
|
||
| import { setConfig } from '@doc-kit/core/utils/configuration/index.mjs'; | ||
| import { u } from 'unist-builder'; | ||
|
|
||
| import { generate } from '../generate.mjs'; | ||
|
|
||
| const config = await setConfig({ target: ['llms-txt-full'] }); | ||
|
|
||
| const entry = (path, text) => ({ | ||
| path, | ||
| content: u('root', [u('paragraph', [u('text', text)])]), | ||
| }); | ||
|
|
||
| describe('llms-txt-full', () => { | ||
| it('precedes the Markdown of every page with its URL', async () => { | ||
| config['llms-txt-full'].baseURL = 'https://example.com'; | ||
| config['llms-txt-full'].output = undefined; | ||
|
|
||
| const full = await generate([entry('/a', 'First.'), entry('/b', 'Other.')]); | ||
|
|
||
| assert.equal( | ||
| full, | ||
| '---\nurl: https://example.com/a.md\n---\nFirst.\n\n' + | ||
| '---\nurl: https://example.com/b.md\n---\nOther.\n' | ||
| ); | ||
| }); | ||
| }); |
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,31 @@ | ||
| 'use strict'; | ||
|
|
||
| import { join } from 'node:path'; | ||
|
|
||
| import getConfig from '@doc-kit/core/utils/configuration/index.mjs'; | ||
| import { populate } from '@doc-kit/core/utils/configuration/templates.mjs'; | ||
| import { writeFile } from '@doc-kit/core/utils/file.mjs'; | ||
|
|
||
| import { buildPages } from './utils/buildPages.mjs'; | ||
|
|
||
| /** | ||
| * Generates a llms-full.txt file | ||
| * | ||
| * @type {import('./types').Generator['generate']} | ||
| */ | ||
| export async function generate(input) { | ||
| const config = getConfig('llms-txt-full'); | ||
|
|
||
| const full = buildPages(input) | ||
| .map( | ||
| ({ path, markdown }) => | ||
| `---\nurl: ${populate(config.pageURL, { ...config, path })}\n---\n${markdown}` | ||
| ) | ||
| .join('\n'); | ||
|
|
||
| if (config.output) { | ||
| await writeFile(join(config.output, 'llms-full.txt'), full); | ||
| } | ||
|
|
||
| return full; | ||
| } |
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,25 @@ | ||
| 'use strict'; | ||
|
|
||
| import llmsTxt from '../llms-txt/index.mjs'; | ||
| import { generate } from './generate.mjs'; | ||
|
|
||
| /** | ||
| * This generator generates a llms-full.txt file holding the Markdown of every | ||
| * page, for LLMs to read the whole documentation in one request | ||
| * | ||
| * @type {import('./types').Generator} | ||
| */ | ||
| export default { | ||
| name: 'llms-txt-full', | ||
|
|
||
| description: | ||
| 'Generates a llms-full.txt file holding the Markdown of every page, each preceded by its URL', | ||
|
|
||
| dependsOn: '@doc-kit/core/metadata', | ||
|
|
||
| defaultConfiguration: { | ||
| pageURL: llmsTxt.defaultConfiguration.pageURL, | ||
| }, | ||
|
|
||
| generate, | ||
| }; |
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,8 @@ | ||
| import { MetadataEntry } from '@doc-kit/core/generators/metadata/types'; | ||
|
|
||
| export type Generator = GeneratorMetadata< | ||
| { | ||
| pageURL: string; | ||
| }, | ||
| Generate<Array<MetadataEntry>, Promise<string>> | ||
| >; |
34 changes: 34 additions & 0 deletions
34
packages/react/src/llms-txt-full/utils/__tests__/buildPages.test.mjs
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,34 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { describe, it } from 'node:test'; | ||
|
|
||
| import { u } from 'unist-builder'; | ||
|
|
||
| import { buildPages } from '../buildPages.mjs'; | ||
|
|
||
| const entry = (path, text, extra = {}) => ({ | ||
| path, | ||
| content: u('root', [u('paragraph', [u('text', text)])]), | ||
| ...extra, | ||
| }); | ||
|
|
||
| describe('buildPages', () => { | ||
| it('joins the content of the entries of each page, in order', () => { | ||
| const pages = buildPages([ | ||
| entry('/a', 'First.'), | ||
| entry('/b', 'Other.'), | ||
| entry('/a', 'Second.'), | ||
| ]); | ||
|
|
||
| assert.deepEqual(pages, [ | ||
| { path: '/a', markdown: 'First.\n\nSecond.\n' }, | ||
| { path: '/b', markdown: 'Other.\n' }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('leaves out pages generated by other generators', () => { | ||
| assert.deepEqual( | ||
| buildPages([entry('/all', 'All.', { synthetic: true })]), | ||
| [] | ||
| ); | ||
| }); | ||
| }); |
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,30 @@ | ||
| import { getRemark, getRemarkMdx } from '@doc-kit/core/utils/remark.mjs'; | ||
|
|
||
| /** | ||
| * The Markdown of every page, from the content of its entries, in the order | ||
| * of the input. | ||
| * | ||
| * @param {Array<import('@doc-kit/core/generators/metadata/types').MetadataEntry>} entries | ||
| * @returns {Array<{ path: string, markdown: string }>} | ||
| */ | ||
| export const buildPages = entries => { | ||
| /** @type {Map<string, Array<import('@doc-kit/core/generators/metadata/types').MetadataEntry>>} */ | ||
| const pages = new Map(); | ||
|
|
||
| for (const entry of entries) { | ||
| if (!entry.synthetic) { | ||
| pages.set(entry.path, [...(pages.get(entry.path) ?? []), entry]); | ||
| } | ||
| } | ||
|
|
||
| return [...pages].map(([path, sections]) => { | ||
| const remark = sections[0].mdx ? getRemarkMdx() : getRemark(); | ||
|
|
||
| const markdown = sections | ||
| .map(({ content }) => remark.stringify(content).trim()) | ||
| .filter(Boolean) | ||
| .join('\n\n'); | ||
|
|
||
| return { path, markdown: `${markdown}\n` }; | ||
| }); | ||
| }; |
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
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.