Skip to content

Commit de88b03

Browse files
authored
fix(react): give llms-txt-full a Markdown pipeline (#1164)
1 parent 802ba72 commit de88b03

6 files changed

Lines changed: 61 additions & 3 deletions

File tree

‎packages/react/package.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@
5151
"recma-stringify": "^1.0.0",
5252
"rehype-raw": "^7.0.0",
5353
"rehype-recma": "^1.0.0",
54+
"remark-gfm": "^4.0.1",
5455
"remark-parse": "^11.0.0",
5556
"remark-rehype": "^11.1.2",
57+
"remark-stringify": "^11.0.0",
5658
"semver": "^7.8.5",
5759
"unified": "^11.0.5",
5860
"unist-builder": "^4.0.0",

‎packages/react/src/llms-txt-full/__tests__/generate.test.mjs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import assert from 'node:assert/strict';
22
import { describe, it } from 'node:test';
33

4+
import { loadGenerator } from '@doc-kit/core/generators/loader.mjs';
45
import { setConfig } from '@doc-kit/core/utils/configuration/index.mjs';
6+
import { loadMarkdownPlugins } from '@doc-kit/core/utils/markdown/plugins.mjs';
57
import { u } from 'unist-builder';
68

79
import { generate } from '../generate.mjs';
810

911
const config = await setConfig({ target: ['llms-txt-full'] });
1012

13+
// Pages are serialised with the pipeline of `llms-txt-full`
14+
await loadMarkdownPlugins(
15+
await loadGenerator(import.meta.resolve('../index.mjs'))
16+
);
17+
1118
const entry = (path, text) => ({
1219
path,
1320
content: u('root', [u('paragraph', [u('text', text)])]),

‎packages/react/src/llms-txt-full/index.mjs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,15 @@ export default {
2121
pageURL: llmsTxt.defaultConfiguration.pageURL,
2222
},
2323

24+
markdown: {
25+
// Pages are serialised back to Markdown, type annotations included
26+
remarkPlugins: [
27+
'@doc-kit/core/plugins/type-annotations/remark.mjs',
28+
'remark-gfm',
29+
'remark-stringify',
30+
'...',
31+
],
32+
},
33+
2434
generate,
2535
};

‎packages/react/src/llms-txt-full/utils/__tests__/buildPages.test.mjs‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import assert from 'node:assert/strict';
22
import { describe, it } from 'node:test';
33

4+
import { loadGenerator } from '@doc-kit/core/generators/loader.mjs';
5+
import { loadMarkdownPlugins } from '@doc-kit/core/utils/markdown/plugins.mjs';
46
import { u } from 'unist-builder';
57

68
import { buildPages } from '../buildPages.mjs';
79

10+
// Pages are serialised with the pipeline of `llms-txt-full`
11+
await loadMarkdownPlugins(
12+
await loadGenerator(import.meta.resolve('../../index.mjs'))
13+
);
14+
815
const entry = (path, text, extra = {}) => ({
916
path,
1017
content: u('root', [u('paragraph', [u('text', text)])]),
@@ -25,6 +32,30 @@ describe('buildPages', () => {
2532
]);
2633
});
2734

35+
it('serialises type annotations back to Markdown', () => {
36+
const content = u('root', [
37+
u('paragraph', [
38+
u('text', 'Returns a '),
39+
u('typeAnnotation', { value: 'Promise<string>' }),
40+
u('text', '.'),
41+
]),
42+
]);
43+
44+
assert.deepEqual(buildPages([{ path: '/a', content }]), [
45+
{ path: '/a', markdown: 'Returns a {Promise<string>}.\n' },
46+
]);
47+
});
48+
49+
it('serialises the JSX of MDX pages', () => {
50+
const content = u('root', [
51+
u('mdxJsxFlowElement', { name: 'Foo', attributes: [] }, []),
52+
]);
53+
54+
assert.deepEqual(buildPages([{ path: '/a', content, mdx: true }]), [
55+
{ path: '/a', markdown: '<Foo />\n' },
56+
]);
57+
});
58+
2859
it('leaves out pages generated by other generators', () => {
2960
assert.deepEqual(
3061
buildPages([entry('/all', 'All.', { synthetic: true })]),

‎packages/react/src/llms-txt-full/utils/buildPages.mjs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getRemark, getRemarkMdx } from '@doc-kit/core/utils/remark.mjs';
1+
import { getProcessor } from '@doc-kit/core/utils/markdown/processor.mjs';
22

33
/**
44
* The Markdown of every page, from the content of its entries, in the order
@@ -18,10 +18,12 @@ export const buildPages = entries => {
1818
}
1919

2020
return [...pages].map(([path, sections]) => {
21-
const remark = sections[0].mdx ? getRemarkMdx() : getRemark();
21+
const processor = getProcessor('llms-txt-full', {
22+
mdx: sections[0].mdx,
23+
});
2224

2325
const markdown = sections
24-
.map(({ content }) => remark.stringify(content).trim())
26+
.map(({ content }) => processor.stringify(content).trim())
2527
.filter(Boolean)
2628
.join('\n\n');
2729

‎pnpm-lock.yaml‎

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)