diff --git a/packages/react/package.json b/packages/react/package.json index 4c73e0a2..e9199f72 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -51,8 +51,10 @@ "recma-stringify": "^1.0.0", "rehype-raw": "^7.0.0", "rehype-recma": "^1.0.0", + "remark-gfm": "^4.0.1", "remark-parse": "^11.0.0", "remark-rehype": "^11.1.2", + "remark-stringify": "^11.0.0", "semver": "^7.8.5", "unified": "^11.0.5", "unist-builder": "^4.0.0", diff --git a/packages/react/src/llms-txt-full/__tests__/generate.test.mjs b/packages/react/src/llms-txt-full/__tests__/generate.test.mjs index 2dc343f2..4cf2e5c5 100644 --- a/packages/react/src/llms-txt-full/__tests__/generate.test.mjs +++ b/packages/react/src/llms-txt-full/__tests__/generate.test.mjs @@ -1,13 +1,20 @@ import assert from 'node:assert/strict'; import { describe, it } from 'node:test'; +import { loadGenerator } from '@doc-kit/core/generators/loader.mjs'; import { setConfig } from '@doc-kit/core/utils/configuration/index.mjs'; +import { loadMarkdownPlugins } from '@doc-kit/core/utils/markdown/plugins.mjs'; import { u } from 'unist-builder'; import { generate } from '../generate.mjs'; const config = await setConfig({ target: ['llms-txt-full'] }); +// Pages are serialised with the pipeline of `llms-txt-full` +await loadMarkdownPlugins( + await loadGenerator(import.meta.resolve('../index.mjs')) +); + const entry = (path, text) => ({ path, content: u('root', [u('paragraph', [u('text', text)])]), diff --git a/packages/react/src/llms-txt-full/index.mjs b/packages/react/src/llms-txt-full/index.mjs index 3a6145a9..2ad6bead 100644 --- a/packages/react/src/llms-txt-full/index.mjs +++ b/packages/react/src/llms-txt-full/index.mjs @@ -21,5 +21,15 @@ export default { pageURL: llmsTxt.defaultConfiguration.pageURL, }, + markdown: { + // Pages are serialised back to Markdown, type annotations included + remarkPlugins: [ + '@doc-kit/core/plugins/type-annotations/remark.mjs', + 'remark-gfm', + 'remark-stringify', + '...', + ], + }, + generate, }; diff --git a/packages/react/src/llms-txt-full/utils/__tests__/buildPages.test.mjs b/packages/react/src/llms-txt-full/utils/__tests__/buildPages.test.mjs index 2ce666db..0d4d9bc1 100644 --- a/packages/react/src/llms-txt-full/utils/__tests__/buildPages.test.mjs +++ b/packages/react/src/llms-txt-full/utils/__tests__/buildPages.test.mjs @@ -1,10 +1,17 @@ import assert from 'node:assert/strict'; import { describe, it } from 'node:test'; +import { loadGenerator } from '@doc-kit/core/generators/loader.mjs'; +import { loadMarkdownPlugins } from '@doc-kit/core/utils/markdown/plugins.mjs'; import { u } from 'unist-builder'; import { buildPages } from '../buildPages.mjs'; +// Pages are serialised with the pipeline of `llms-txt-full` +await loadMarkdownPlugins( + await loadGenerator(import.meta.resolve('../../index.mjs')) +); + const entry = (path, text, extra = {}) => ({ path, content: u('root', [u('paragraph', [u('text', text)])]), @@ -25,6 +32,30 @@ describe('buildPages', () => { ]); }); + it('serialises type annotations back to Markdown', () => { + const content = u('root', [ + u('paragraph', [ + u('text', 'Returns a '), + u('typeAnnotation', { value: 'Promise' }), + u('text', '.'), + ]), + ]); + + assert.deepEqual(buildPages([{ path: '/a', content }]), [ + { path: '/a', markdown: 'Returns a {Promise}.\n' }, + ]); + }); + + it('serialises the JSX of MDX pages', () => { + const content = u('root', [ + u('mdxJsxFlowElement', { name: 'Foo', attributes: [] }, []), + ]); + + assert.deepEqual(buildPages([{ path: '/a', content, mdx: true }]), [ + { path: '/a', markdown: '\n' }, + ]); + }); + it('leaves out pages generated by other generators', () => { assert.deepEqual( buildPages([entry('/all', 'All.', { synthetic: true })]), diff --git a/packages/react/src/llms-txt-full/utils/buildPages.mjs b/packages/react/src/llms-txt-full/utils/buildPages.mjs index 3f2b5dbe..5dc2e228 100644 --- a/packages/react/src/llms-txt-full/utils/buildPages.mjs +++ b/packages/react/src/llms-txt-full/utils/buildPages.mjs @@ -1,4 +1,4 @@ -import { getRemark, getRemarkMdx } from '@doc-kit/core/utils/remark.mjs'; +import { getProcessor } from '@doc-kit/core/utils/markdown/processor.mjs'; /** * The Markdown of every page, from the content of its entries, in the order @@ -18,10 +18,12 @@ export const buildPages = entries => { } return [...pages].map(([path, sections]) => { - const remark = sections[0].mdx ? getRemarkMdx() : getRemark(); + const processor = getProcessor('llms-txt-full', { + mdx: sections[0].mdx, + }); const markdown = sections - .map(({ content }) => remark.stringify(content).trim()) + .map(({ content }) => processor.stringify(content).trim()) .filter(Boolean) .join('\n\n'); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index feebcb8f..615f4e01 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -286,12 +286,18 @@ importers: rehype-recma: specifier: ^1.0.0 version: 1.0.0(supports-color@7.2.0) + remark-gfm: + specifier: ^4.0.1 + version: 4.0.1(supports-color@7.2.0) remark-parse: specifier: ^11.0.0 version: 11.0.0(supports-color@7.2.0) remark-rehype: specifier: ^11.1.2 version: 11.1.2 + remark-stringify: + specifier: ^11.0.0 + version: 11.0.0 semver: specifier: ^7.8.5 version: 7.8.5