|
| 1 | +/** |
| 2 | + * This mini-plugin copies media files from `site/media/` to the output |
| 3 | + * directory's `media/` folder at end of rendering. |
| 4 | + * |
| 5 | + * @packageDocumentation |
| 6 | + */ |
| 7 | +import fs from 'node:fs'; |
| 8 | +import path from 'node:path'; |
| 9 | +import { fileURLToPath } from 'node:url'; |
| 10 | +import { PageEvent, RendererEvent } from 'typedoc'; |
| 11 | + |
| 12 | +/** |
| 13 | + * @import {Application} from "typedoc" |
| 14 | + */ |
| 15 | + |
| 16 | +const SOURCE_DIR = fileURLToPath(new URL('../site/media/', import.meta.url)); |
| 17 | + |
| 18 | +/** |
| 19 | + * @function |
| 20 | + * @param {Application} app |
| 21 | + */ |
| 22 | +export const load = (app) => { |
| 23 | + const outputDir = app.options.getValue('out'); |
| 24 | + const mediaDir = path.join(outputDir, 'media'); |
| 25 | + |
| 26 | + // Listen for declaration creation to inspect JSDoc block tags |
| 27 | + |
| 28 | + app.logger.info( |
| 29 | + `Will copy all files in ${path.relative(process.cwd(), SOURCE_DIR)} to ${path.relative(process.cwd(), mediaDir)}`, |
| 30 | + ); |
| 31 | + |
| 32 | + app.renderer.on(RendererEvent.END, () => { |
| 33 | + for (const file of fs.readdirSync(SOURCE_DIR)) { |
| 34 | + const sourceFile = path.join(SOURCE_DIR, file); |
| 35 | + const destFile = path.join(mediaDir, file); |
| 36 | + fs.cpSync(sourceFile, destFile); |
| 37 | + app.logger.info( |
| 38 | + `Copied ${path.relative(process.cwd(), sourceFile)} to ${path.relative(process.cwd(), destFile)}`, |
| 39 | + ); |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + app.renderer.on(PageEvent.END, (page) => { |
| 44 | + if (!page.contents) { |
| 45 | + return; |
| 46 | + } |
| 47 | + const navigationLinks = app.options.getValue('navigationLinks'); |
| 48 | + if (!navigationLinks) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + if ('GitHub' in navigationLinks) { |
| 53 | + // Replace the GitHub link in the footer with one that includes the icon |
| 54 | + page.contents = page.contents.replace( |
| 55 | + `<a href="${navigationLinks.GitHub}">GitHub</a>`, |
| 56 | + `<a href="${navigationLinks.GitHub}" title="bargs on GitHub"><span class="icon-github" aria-label="GitHub"></span></a>`, |
| 57 | + ); |
| 58 | + } |
| 59 | + if ('npm' in navigationLinks) { |
| 60 | + // Replace the npm link in the footer with one that includes the icon |
| 61 | + page.contents = page.contents.replace( |
| 62 | + `<a href="${navigationLinks.npm}">npm</a>`, |
| 63 | + `<a href="${navigationLinks.npm}" title="@boneskull/bargs on npm"><span class="icon-npm" aria-label="npm"></span></a>`, |
| 64 | + ); |
| 65 | + } |
| 66 | + }); |
| 67 | +}; |
0 commit comments