|
| 1 | +import * as Path from "path"; |
| 2 | +import { RendererComponent } from "../components.js"; |
| 3 | +import { PageEvent, RendererEvent } from "../events.js"; |
| 4 | +import { JSX, writeFile } from "../../utils/index.js"; |
| 5 | +import { DefaultTheme } from "../themes/default/DefaultTheme.js"; |
| 6 | +import { gzip } from "zlib"; |
| 7 | +import { promisify } from "util"; |
| 8 | +import { |
| 9 | + DeclarationReflection, |
| 10 | + ReferenceType, |
| 11 | + ReflectionKind, |
| 12 | +} from "../../models/index.js"; |
| 13 | +import type { Renderer } from "../index.js"; |
| 14 | + |
| 15 | +const gzipP = promisify(gzip); |
| 16 | + |
| 17 | +export interface HierarchyElement { |
| 18 | + html: string; |
| 19 | + text: string; |
| 20 | + path: string; |
| 21 | + parents?: ({ path: string } | { html: string; text: string })[]; |
| 22 | + children?: ({ path: string } | { html: string; text: string })[]; |
| 23 | +} |
| 24 | + |
| 25 | +export class HierarchyPlugin extends RendererComponent { |
| 26 | + constructor(renderer: Renderer) { |
| 27 | + super(renderer); |
| 28 | + this.owner.on(RendererEvent.BEGIN, this.onRendererBegin.bind(this)); |
| 29 | + } |
| 30 | + |
| 31 | + private onRendererBegin(_event: RendererEvent) { |
| 32 | + if (!(this.owner.theme instanceof DefaultTheme)) { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + this.owner.preRenderAsyncJobs.push((event) => |
| 37 | + this.buildHierarchy(event), |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + private async buildHierarchy(event: RendererEvent) { |
| 42 | + const theme = this.owner.theme as DefaultTheme; |
| 43 | + |
| 44 | + const context = theme.getRenderContext(new PageEvent(event.project)); |
| 45 | + |
| 46 | + const hierarchy = ( |
| 47 | + event.project.getReflectionsByKind( |
| 48 | + ReflectionKind.ClassOrInterface, |
| 49 | + ) as DeclarationReflection[] |
| 50 | + ) |
| 51 | + .filter( |
| 52 | + (reflection) => |
| 53 | + reflection.extendedTypes?.length || |
| 54 | + reflection.extendedBy?.length, |
| 55 | + ) |
| 56 | + .map((reflection) => ({ |
| 57 | + html: JSX.renderElement( |
| 58 | + context.type( |
| 59 | + ReferenceType.createResolvedReference( |
| 60 | + reflection.name, |
| 61 | + reflection, |
| 62 | + reflection.project, |
| 63 | + ), |
| 64 | + ), |
| 65 | + ), |
| 66 | + // Full name should be safe here, since this list only includes classes/interfaces. |
| 67 | + text: reflection.getFullName(), |
| 68 | + path: reflection.url!, |
| 69 | + parents: reflection.extendedTypes?.map((type) => |
| 70 | + !(type instanceof ReferenceType) || |
| 71 | + !(type.reflection instanceof DeclarationReflection) |
| 72 | + ? { |
| 73 | + html: JSX.renderElement(context.type(type)), |
| 74 | + text: type.toString(), |
| 75 | + } |
| 76 | + : { path: type.reflection.url! }, |
| 77 | + ), |
| 78 | + children: reflection.extendedBy?.map((type) => |
| 79 | + !(type instanceof ReferenceType) || |
| 80 | + !(type.reflection instanceof DeclarationReflection) |
| 81 | + ? { |
| 82 | + html: JSX.renderElement(context.type(type)), |
| 83 | + text: type.toString(), |
| 84 | + } |
| 85 | + : { path: type.reflection.url! }, |
| 86 | + ), |
| 87 | + })); |
| 88 | + |
| 89 | + if (!hierarchy.length) return; |
| 90 | + |
| 91 | + hierarchy.forEach((element) => { |
| 92 | + if (!element.parents?.length) delete element.parents; |
| 93 | + |
| 94 | + if (!element.children?.length) delete element.children; |
| 95 | + }); |
| 96 | + |
| 97 | + const hierarchyJs = Path.join( |
| 98 | + event.outputDirectory, |
| 99 | + "assets", |
| 100 | + "hierarchy.js", |
| 101 | + ); |
| 102 | + |
| 103 | + const gz = await gzipP(Buffer.from(JSON.stringify(hierarchy))); |
| 104 | + |
| 105 | + await writeFile( |
| 106 | + hierarchyJs, |
| 107 | + `window.hierarchyData = "data:application/octet-stream;base64,${gz.toString( |
| 108 | + "base64", |
| 109 | + )}"`, |
| 110 | + ); |
| 111 | + } |
| 112 | +} |
0 commit comments