import fs from 'node:fs'; import path from 'node:path'; import { afterEach, describe, expect, it, vi } from 'vitest'; import { renderDocsMarkdown } from './docs-markdown'; const quickStart = fs.readFileSync( path.resolve(process.cwd(), 'src/content/docs/headless/quick-start.mdx'), 'utf8', ); const viewerQuickStart = fs.readFileSync( path.resolve(process.cwd(), 'src/content/docs/viewer/quick-start.mdx'), 'utf8', ); describe('renderDocsMarkdown', () => { afterEach(() => vi.unstubAllEnvs()); it('exports only the active Headless integration and expands its complete example', () => { vi.stubEnv('DOCS_INSTALL_CHANNEL', 'next'); const markdown = renderDocsMarkdown({ sourceCode: quickStart, canonicalPath: '/docs/headless/react/quick-start', integration: 'react', metadata: { title: 'Quick start', description: 'Build your own PDF viewer UI.', }, }); expect(markdown).toContain('title: "Quick start — React"'); expect(markdown).toContain('\n---\n\n# Quick start'); expect(markdown).toContain('pnpm add @embedpdf/react@next @embedpdf/engine@next'); expect(markdown).toContain("import { localEngine } from '@embedpdf/engine'"); expect(markdown).toContain('**`basic.tsx`**'); expect(markdown).not.toContain('@embedpdf/vue'); expect(markdown).not.toContain('@embedpdf/svelte'); expect(markdown).not.toContain('@embedpdf/angular'); expect(markdown).not.toContain(' { const markdown = renderDocsMarkdown({ sourceCode: '[Next](/docs/headless/selection)', canonicalPath: '/docs/headless/vue/current', integration: 'vue', }); expect(markdown).toContain('(https://www.embedpdf.com/docs/headless/vue/selection.md)'); }); it('gives a link written from the page its full URL, in the page’s integration', () => { const from = (canonicalPath: string, integration: 'react' | 'vanilla', link: string) => renderDocsMarkdown({ sourceCode: `[Next](${link})`, canonicalPath, integration }); expect(from('/docs/headless/react/viewing/stage', 'react', '../text/selection')).toContain( '(https://www.embedpdf.com/docs/headless/react/text/selection.md)', ); // An index page's URL has no folder of its own: `/docs/headless/react` reads `setup` beside it. expect(from('/docs/headless/react', 'react', 'setup')).toContain( '(https://www.embedpdf.com/docs/headless/react/setup.md)', ); expect(from('/docs/viewer/vanilla', 'vanilla', 'code/plugins')).toContain( '(https://www.embedpdf.com/docs/viewer/vanilla/code/plugins.md)', ); }); it('names the page’s other integrations by the URL of their Markdown', () => { const markdown = renderDocsMarkdown({ sourceCode: '# Setup', canonicalPath: '/docs/viewer/vanilla/setup', integration: 'vanilla', siblings: ['react', 'vue'], }); expect(markdown).toContain( [ 'integration: "Vanilla JS"', 'other_integrations:', ' "React": "https://www.embedpdf.com/docs/viewer/react/setup.md"', ' "Vue": "https://www.embedpdf.com/docs/viewer/vue/setup.md"', 'source: "https://www.embedpdf.com/docs/viewer/vanilla/setup"', ].join('\n'), ); }); it('exports only the selected Viewer integration', () => { const markdown = renderDocsMarkdown({ sourceCode: viewerQuickStart, canonicalPath: '/docs/viewer/vue/quick-start', integration: 'vue', metadata: { title: 'Quick start', description: 'Install the viewer, show a PDF, and make it yours.', }, }); expect(markdown).toContain('title: "Quick start — Vue"'); expect(markdown).toContain('integration: "Vue"'); expect(markdown).toContain('@embedpdf/viewer-vue'); expect(markdown).toContain("import { PDFViewer } from '@embedpdf/viewer-vue'"); expect(markdown).not.toContain('@embedpdf/viewer-react'); expect(markdown).not.toContain('@embedpdf/viewer-angular'); expect(markdown).not.toContain(' branch on its own integration, plain HTML included', () => { const sourceCode = [ '# Events', '', 'Listen for `epdf:ready` on the element.', '', "Pass `onReady`.", ].join('\n'); const exported = (integration: 'vanilla' | 'react' | 'angular') => renderDocsMarkdown({ sourceCode, canonicalPath: `/docs/viewer/${integration}/code/viewer`, integration, }); expect(exported('vanilla')).toContain('Listen for `epdf:ready` on the element.'); expect(exported('vanilla')).not.toContain('Pass `onReady`.'); expect(exported('react')).toContain('Pass `onReady`.'); expect(exported('react')).not.toContain('epdf:ready'); expect(exported('angular')).not.toContain('epdf:ready'); expect(exported('angular')).not.toContain('onReady'); }); it('fails when a custom component has no explicit Markdown projection', () => { expect(() => renderDocsMarkdown({ sourceCode: '', canonicalPath: '/docs/example', }), ).toThrow('No Markdown projection is defined for .'); }); });