|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | | -import { readFile } from 'node:fs/promises'; |
| 3 | +import { readFile, cp, mkdir } from 'node:fs/promises'; |
4 | 4 | import { join } from 'node:path'; |
5 | 5 |
|
6 | 6 | import { processJSXEntries } from './utils/processing.mjs'; |
7 | 7 | import getConfig from '../../utils/configuration/index.mjs'; |
8 | 8 | import { writeFile } from '../../utils/file.mjs'; |
9 | 9 |
|
| 10 | +/** |
| 11 | + * Aggregates and copies local assets referenced in the markdown ASTs to the output directory. |
| 12 | + * |
| 13 | + * @param {Array<import('./types').JSXContent>} input - The processed entries containing asset maps |
| 14 | + * @param {string} outputDir - The absolute path to the generation output directory |
| 15 | + * @returns {Promise<void>} |
| 16 | + */ |
| 17 | +async function copyProjectAssets(input, outputDir) { |
| 18 | + const allAssets = new Map(); |
| 19 | + |
| 20 | + for (const entry of input) { |
| 21 | + if (entry.assetsMap) { |
| 22 | + for (const [source, name] of Object.entries(entry.assetsMap)) { |
| 23 | + allAssets.set(source, join(outputDir, 'assets', name)); |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + if (allAssets.size === 0) { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + const assetsOutputDir = join(outputDir, 'assets'); |
| 33 | + await mkdir(assetsOutputDir, { recursive: true }); |
| 34 | + |
| 35 | + for (const [source, dest] of allAssets.entries()) { |
| 36 | + try { |
| 37 | + await cp(source, dest, { force: true }); |
| 38 | + } catch (err) { |
| 39 | + console.error(`[doc-kit] Error copying asset: ${source}`, err); |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
10 | 44 | /** |
11 | 45 | * Main generation function that processes JSX AST entries into web bundles. |
12 | 46 | * |
@@ -34,6 +68,8 @@ export async function generate(input) { |
34 | 68 |
|
35 | 69 | // Write CSS bundle |
36 | 70 | await writeFile(join(config.output, 'styles.css'), css, 'utf-8'); |
| 71 | + |
| 72 | + await copyProjectAssets(input, config.output); |
37 | 73 | } |
38 | 74 |
|
39 | 75 | return results.map(({ html }) => ({ html: html.toString(), css })); |
|
0 commit comments