import assert from 'node:assert/strict'; import { mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; import { collectResult } from '@lit-labs/ssr/lib/render-result.js'; import { html } from 'lit'; import path from 'node:path'; import { fileURLToPath, pathToFileURL } from 'node:url'; import { init, parse as parseExports } from 'es-module-lexer'; import { describe, it } from 'node:test'; import { finalizeRocketIcons, iconsFromPackage } from './icons.js'; import { compileMarkdownLoad, compileMarkdownSetup } from './markdownCompiler.js'; import { RocketCodeBlock } from './RocketCodeBlock.js'; const projectRoot = fileURLToPath(new URL('..', import.meta.url)); const docsPagesDir = path.join(projectRoot, 'docs/pages'); describe('Test markdownCompiler', () => { it('01: compiles docs Markdown Pages with explicit Registered Components exports', async () => { await init; const missingComponentsExports = []; for (const pageFile of findMarkdownPages(docsPagesDir)) { const source = readFileSync(pageFile, 'utf8'); if (!usesRegisteredComponentLayout(source)) { continue; } const compiledModule = await compileMarkdownLoad(source); const [, exports] = parseExports(compiledModule); const exportsComponents = exports.some( exported => exported.type !== 'reexport-all' && exported.name === 'components', ); if (!exportsComponents) { missingComponentsExports.push(path.relative(projectRoot, pageFile)); } } assert.deepEqual(missingComponentsExports, []); }); it('02: renders labeled Markdown Code Blocks with frame data and Prism output', async () => { defineRocketCodeBlock(); const code = 'const command = "rocket build";\nconsole.log(`copy ${command}!`);'; const body = await renderMarkdownPage(`# Guide \`\`\`js label="Install command" {1} showLineNumbers ${code} \`\`\` `); const encodedCode = readAttribute(body, 'encoded-code'); assert.match(body, /]*>[\s\S]*Install command/); assert.match(body, /part="language-badge"[^>]*>[\s\S]*JS/); assert.match(body, /
[\s\S]*part="copy-button"[\s\S]*<\/figcaption>/); assert.match(body, //); assert.match(body, /]*\bname="copy"/); assert.match(body, /]*\bname="check-lg"/); assert.match(body, /]*\bname="x-circle"/); assert.equal(Buffer.from(encodedCode, 'base64').toString('utf8'), code); assert.match(body, /
/);
    assert.match(body, //);
    assert.match(body, /class="code-line line-number highlight-line" line="1"/);
    assert.match(body, /class="token keyword"[^>]*>const/);

    const finalizedBody = await finalizeRocketIcons(body, {
      layoutIconLibraries: {
        bootstrap: iconsFromPackage('bootstrap-icons', 'icons/*.svg'),
      },
      layoutDefaultIconLibrary: 'bootstrap',
      pageData: { _hasBrowserLoadedComponents: true },
    });
    assert.deepEqual(Object.keys(readIconManifest(finalizedBody).icons).sort(), [
      'bootstrap:check-lg',
      'bootstrap:copy',
      'bootstrap:x-circle',
    ]);
  });

  it('03: renders unlabeled Markdown Code Blocks without a header', async () => {
    defineRocketCodeBlock();
    const code = 'const label = "not a header";';
    const body = await renderMarkdownPage(`# Guide

\`\`\`js
${code}
\`\`\`
`);
    const encodedCode = readAttribute(body, 'encoded-code');

    assert.match(body, / {
    defineRocketCodeBlock();
    const code = 'npm run build';
    const body = await renderMarkdownPage(`# Guide

\`\`\`bash
${code}
\`\`\`
`);

    assert.match(body, /]*\slanguage="bash"/);
    assert.doesNotMatch(body, /]*\sframe=/);
    assert.match(body, /part="frame terminal-frame"/);
    assert.match(body, /part="code terminal-code"/);
  });

  it('05: renders js demo source panels inside rocket-js-demo with Code Block treatment', async () => {
    defineRocketCodeBlock();
    const code = [
      "import { html } from 'lit';",
      '',
      'export const simpleDemo = () => html``;',
    ].join('\n');
    const body = await renderMarkdownPage(`# Demo

\`\`\`js demo label="docs/pages/examples/simple-demo.js"
${code}
\`\`\`
`);
    const encodedCode = readAttribute(body, 'encoded-code');

    assert.match(
      body,
      /[\s\S]*]*\blabel="docs\/pages\/examples\/simple-demo\.js"[^>]*\slanguage="js"[\s\S]*<\/rocket-code-block>[\s\S]*<\/rocket-js-demo>/,
    );
    assert.doesNotMatch(body, /rocket-preview/);
    assert.equal(Buffer.from(encodedCode, 'base64').toString('utf8'), code);
    assert.match(body, /part="copy-button"/);
    assert.match(body, /
/);
  });

  it('06: renders Markdown Code Blocks with custom starting line numbers', async () => {
    defineRocketCodeBlock();
    const body = await renderMarkdownPage(`# Guide

\`\`\`js showLineNumbers=10
const first = 1;
const second = 2;
\`\`\`
`);

    assert.match(body, /class="code-line line-number" line="10"/);
    assert.match(body, /class="code-line line-number" line="11"/);
  });

  it('07: renders Markdown heading permalinks for shareable sections', async () => {
    const body = await renderMarkdownPage(`# Guide

## Install Rocket
`);
    const anchor = readAnchorForHash(body, 'install-rocket');

    assert.match(body, /

Install Rocket#<\/a>$/); }); it('08: imports the JavaScript Demo define entrypoint in generated browser code', async () => { const code = [ "import { html } from 'lit';", '', 'export const simpleDemo = () => html``;', ].join('\n'); const source = `# Demo \`\`\`js demo ${code} \`\`\` `; const pageModule = await compileMarkdownLoad(source); const singleDemoModule = await compileMarkdownLoad(source, { singleDemo: 'simpleDemo' }); assert.match(pageModule, /import\("@rocket\/js\/define\/RocketJsDemo\.js"\)/); assert.match(singleDemoModule, /import\("@rocket\/js\/define\/RocketJsDemo\.js"\)/); assert.doesNotMatch(pageModule, /import\("@rocket\/js\/RocketJsDemo\.js"\)/); assert.doesNotMatch(singleDemoModule, /import\("@rocket\/js\/RocketJsDemo\.js"\)/); }); it('09: renders js request-demo blocks as Request Demo elements with Code Block source', async () => { defineRocketCodeBlock(); const code = [ 'export default function content(request) {', ' return { pathname: new URL(request.url).pathname };', '}', ].join('\n'); const body = await renderMarkdownPage(`# Request Demo \`\`\`js request-demo url="/api/time?format=json" label="docs/pages/api/time.rocket.js" height=360 ${code} \`\`\` `); const encodedCode = readAttribute(body, 'encoded-code'); assert.match(body, /]*\burl="\/api\/time\?format=json"/); assert.match(body, /]*\blabel="docs\/pages\/api\/time\.rocket\.js"/); assert.match(body, /]*\bheight="360"/); assert.doesNotMatch(body, /
/); assert.doesNotMatch(body, /data-request-demo-source/); assert.doesNotMatch(body, /data-request-demo-actions/); assert.match( body, /]*\blabel="docs\/pages\/api\/time\.rocket\.js"[^>]*\blanguage="js"[\s\S]*<\/rocket-code-block>[\s\S]*<\/rocket-request-demo>/, ); assert.equal(Buffer.from(encodedCode, 'base64').toString('utf8'), code); }); it('10: does not extract or execute js request-demo source as Page or JavaScript Demo code', async () => { const requestDemoCode = [ 'customElements.define("request-demo-owned-card", class extends HTMLElement {});', 'export const shouldNotBecomeDemo = () => "request demo source";', ].join('\n'); const source = `\`\`\`js server export const config = { path: "/request-demo" }; \`\`\` # Request Demo \`\`\`js request-demo url="/missing-route" ${requestDemoCode} \`\`\` `; const setupCode = await compileMarkdownSetup(source); const pageModule = await compileMarkdownLoad(source); const clientCode = readClientCode(pageModule); assert.doesNotMatch(setupCode, /request-demo-owned-card/); assert.doesNotMatch(setupCode, /shouldNotBecomeDemo/); assert.equal(clientCode, ''); assert.doesNotMatch(pageModule, /demo setup code/); assert.doesNotMatch(pageModule, /rocketSingleDemo/); }); it('11: omits default Request Demo height from generated markup', async () => { defineRocketCodeBlock(); const body = await renderMarkdownPage(`# Request Demo \`\`\`js request-demo url="/api/default-height.json" export default () => ({ ok: true }); \`\`\` `); assert.match(body, /]*\burl="\/api\/default-height\.json"/); assert.doesNotMatch(body, /]*\bheight=/); assert.doesNotMatch(body, / { const tempDir = mkdtempSync(path.join(process.cwd(), 'temp/rocket-markdown-')); const moduleFile = path.join(tempDir, 'page.mjs'); const layoutFile = path.join(tempDir, 'layout.mjs'); try { writeFileSync( layoutFile, [ "import { html } from 'lit';", '', 'export const directLayout = data => html`
${data.content}
`;', ].join('\n'), ); writeFileSync( moduleFile, await compileMarkdownLoad(`\`\`\`js server export { directLayout as layout } from './layout.mjs'; \`\`\` # Direct Layout `), ); const module = await import(pathToFileURL(moduleFile).href); const body = await collectResult( module.contentFn( {}, /** @param {{ content: unknown }} data */ data => html`
${data.content}
`, ), ); assert.match(body, /data-layout="direct"/); assert.doesNotMatch(body, /data-layout="default"/); assert.match(body, /Direct Layout/); } finally { rmSync(tempDir, { recursive: true, force: true }); } }); it('13: imports generated Markdown Page runtime dependencies through Rocket', async () => { const source = `# Runtime dependencies \`\`\`js demo export const runtimeDemo = () => 'runtime'; \`\`\` `; const moduleCodes = [ await compileMarkdownLoad(source), await compileMarkdownLoad(source, { singleDemo: 'runtimeDemo' }), ]; for (const moduleCode of moduleCodes) { assert.match(moduleCode, /import \{ html, render \} from '@rocket\/js\/ssr\.js';/); assert.doesNotMatch(moduleCode, /from '@lit-labs\/ssr'/); assert.doesNotMatch(moduleCode, /from 'lit'/); } }); }); function defineRocketCodeBlock() { if (!customElements.get('rocket-code-block')) { customElements.define('rocket-code-block', RocketCodeBlock); } } /** * @param {string} source */ async function renderMarkdownPage(source) { const tempDir = mkdtempSync(path.join(process.cwd(), 'temp/rocket-markdown-')); const moduleFile = path.join(tempDir, 'page.mjs'); try { writeFileSync(moduleFile, await compileMarkdownLoad(source)); const module = await import(pathToFileURL(moduleFile).href); return collectResult( module.contentFn( {}, /** @param {{ content: unknown }} data */ data => data.content, ), ); } finally { rmSync(tempDir, { recursive: true, force: true }); } } /** * @param {string} html * @param {string} name */ function readAttribute(html, name) { const match = html.match(new RegExp(`\\s${name}="([^"]*)"`)); assert.ok(match, `Expected ${name} attribute`); return match[1]; } /** * @param {string} moduleCode */ function readClientCode(moduleCode) { const match = /data\._clientCode = `([\s\S]*?)`;\n\s+data\.content/.exec(moduleCode); assert.ok(match, 'Expected generated client code assignment'); return match[1]; } /** * @param {string} html * @param {string} id */ function readAnchorForHash(html, id) { const match = html.match(new RegExp(`]*href="#${id}"[^>]*>#[\\s\\S]*?<\\/a>`)); assert.ok(match, `Expected anchor link for #${id}`); return match[0]; } /** * @param {string} html */ function readIconManifest(html) { const match = /