Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions src/generators/web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ The `web` generator transforms JSX AST entries into complete web bundles, produc

The `web` generator accepts the following configuration options:

| Name | Type | Default | Description |
| ----------------- | --------- | --------------------------------------------- | --------------------------------------------------------------------- |
| `output` | `string` | - | The directory where HTML, JavaScript, and CSS files will be written |
| `templatePath` | `string` | `'template.html'` | Path to the HTML template file |
| `project` | `string` | `'Node.js'` | Project name used in page titles and the version selector |
| `title` | `string` | `'{project} v{version} Documentation'` | Title template for HTML pages (supports `{project}`, `{version}`) |
| `useAbsoluteURLs` | `boolean` | `false` | When `true`, all internal links use absolute URLs based on `baseURL` |
| `editURL` | `string` | `'${GITHUB_EDIT_URL}/doc/api{path}.md'` | URL template for "edit this page" links |
| `pageURL` | `string` | `'{baseURL}/latest-{version}/api{path}.html'` | URL template for documentation page links |
| `imports` | `object` | See below | Object mapping `#theme/` aliases to component paths for customization |
| `virtualImports` | `object` | `{}` | Additional virtual module mappings merged into the build |
| Name | Type | Default | Description |
| ----------------------- | ---------- | --------------------------------------------- | --------------------------------------------------------------------- |
| `output` | `string` | - | The directory where HTML, JavaScript, and CSS files will be written |
| `templatePath` | `string` | `'template.html'` | Path to the HTML template file |
| `project` | `string` | `'Node.js'` | Project name used in page titles and the version selector |
| `title` | `string` | `'{project} v{version} Documentation'` | Title template for HTML pages (supports `{project}`, `{version}`) |
| `useAbsoluteURLs` | `boolean` | `false` | When `true`, all internal links use absolute URLs based on `baseURL` |
| `editURL` | `string` | `'${GITHUB_EDIT_URL}/doc/api{path}.md'` | URL template for "edit this page" links |
| `pageURL` | `string` | `'{baseURL}/latest-{version}/api{path}.html'` | URL template for documentation page links |
| `imports` | `object` | See below | Object mapping `#theme/` aliases to component paths for customization |
| `virtualImports` | `object` | `{}` | Additional virtual module mappings merged into the build |
| `additionalPathsToCopy` | `string[]` | `[]` | Array of paths to copy to the output directory |

#### Default `imports`

Expand Down
25 changes: 24 additions & 1 deletion src/generators/web/__tests__/generate.test.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import assert from 'node:assert/strict';
import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { describe, it } from 'node:test';

import { setConfig } from '../../../utils/configuration/index.mjs';
import buildContent from '../../jsx-ast/utils/buildContent.mjs';
import { buildNotFoundPage } from '../../jsx-ast/utils/synthetic/404.mjs';
import { generate } from '../generate.mjs';
import { copyAdditionalPaths, generate } from '../generate.mjs';

const createEntry = (api, name) => {
const heading = {
Expand Down Expand Up @@ -54,4 +57,24 @@ describe('web generate', () => {
assert.doesNotMatch(notFoundResult.html, /href=404\.json/);
assert.doesNotMatch(notFoundResult.html, /href=404\.md/);
});

it('copies additional paths to the output directory', async () => {
const temp = await mkdtemp(join(tmpdir(), 'doc-kit-web-'));
const source = join(temp, 'assets');
const output = join(temp, 'output');

await mkdir(source);
await writeFile(join(source, 'logo.txt'), 'static asset');

try {
await copyAdditionalPaths([source], output);

assert.equal(
await readFile(join(output, 'assets', 'logo.txt'), 'utf-8'),
'static asset'
);
} finally {
await rm(temp, { recursive: true, force: true });
}
});
});
19 changes: 17 additions & 2 deletions src/generators/web/generate.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
'use strict';

import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
import { cp, readFile } from 'node:fs/promises';
import { basename, join } from 'node:path';

import { processJSXEntries } from './utils/processing.mjs';
import getConfig from '../../utils/configuration/index.mjs';
import { writeFile } from '../../utils/file.mjs';

/**
* Copies configured static paths into the generator output directory.
*
* @param {Array<string>} paths
* @param {string} output
*/
export const copyAdditionalPaths = (paths, output) =>
Promise.all(
paths.map(path =>
cp(path, join(output, basename(path)), { recursive: true })
)
);

/**
* Main generation function that processes JSX AST entries into web bundles.
*
Expand Down Expand Up @@ -39,6 +52,8 @@ export async function generate(input) {
}

await writeFile(join(config.output, 'styles.css'), css, 'utf-8');

await copyAdditionalPaths(config.additionalPathsToCopy, config.output);
}

return results.map(({ html }) => ({ html: html.toString(), css }));
Expand Down
1 change: 1 addition & 0 deletions src/generators/web/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ export default createLazyGenerator({
'#theme/Layout': join(import.meta.dirname, './ui/components/Layout'),
},
virtualImports: {},
additionalPathsToCopy: [],
},
});
1 change: 1 addition & 0 deletions src/generators/web/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export type Configuration = {
useAbsoluteURLs: boolean;
imports: Record<string, string>;
virtualImports: Record<string, string>;
additionalPathsToCopy: Array<string>;
};

export type Generator = GeneratorMetadata<
Expand Down
Loading