|
1 | 1 | // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2 | 2 |
|
3 | 3 | import { instantiate } from "./deno_doc_wasm.generated.js";
|
4 |
| -import type { DocNode } from "./types.d.ts"; |
| 4 | +import type { DocNode, Location } from "./types.d.ts"; |
5 | 5 | import { createCache } from "jsr:@deno/cache-dir@0.11";
|
6 | 6 | import type { CacheSetting, LoadResponse } from "jsr:@deno/graph@0.82";
|
7 | 7 |
|
@@ -124,3 +124,208 @@ export async function doc(
|
124 | 124 | printImportMapDiagnostics,
|
125 | 125 | );
|
126 | 126 | }
|
| 127 | + |
| 128 | +export interface ShortPath { |
| 129 | + /** Name identifier for the path. */ |
| 130 | + path: string; |
| 131 | + /** URL for the path. */ |
| 132 | + specifier: string; |
| 133 | + /** Whether the path is the main entrypoint. */ |
| 134 | + isMain: boolean; |
| 135 | +} |
| 136 | + |
| 137 | +export interface UrlResolveKindRoot { |
| 138 | + kind: "root"; |
| 139 | +} |
| 140 | + |
| 141 | +export interface UrlResolveKindAllSymbols { |
| 142 | + kind: "allSymbols"; |
| 143 | +} |
| 144 | + |
| 145 | +export interface UrlResolveKindCategory { |
| 146 | + kind: "category"; |
| 147 | + category: string; |
| 148 | +} |
| 149 | + |
| 150 | +export interface UrlResolveKindFile { |
| 151 | + kind: "file"; |
| 152 | + file: ShortPath; |
| 153 | +} |
| 154 | + |
| 155 | +export interface UrlResolveKindSymbol { |
| 156 | + kind: "symbol"; |
| 157 | + file: ShortPath; |
| 158 | + symbol: string; |
| 159 | +} |
| 160 | + |
| 161 | +export type UrlResolveKind = |
| 162 | + | UrlResolveKindRoot |
| 163 | + | UrlResolveKindAllSymbols |
| 164 | + | UrlResolveKindCategory |
| 165 | + | UrlResolveKindFile |
| 166 | + | UrlResolveKindSymbol; |
| 167 | + |
| 168 | +interface HrefResolver { |
| 169 | + /** Resolver for how files should link to eachother. */ |
| 170 | + resolvePath?(current: UrlResolveKind, target: UrlResolveKind): string; |
| 171 | + /** Resolver for global symbols, like the Deno namespace or other built-ins */ |
| 172 | + resolveGlobalSymbol?(symbol: string[]): string | undefined; |
| 173 | + /** Resolver for symbols from non-relative imports */ |
| 174 | + resolveImportHref?(symbol: string[], src: string): string | undefined; |
| 175 | + /** Resolve the URL used in source code link buttons. */ |
| 176 | + resolveSource?(location: Location): string | undefined; |
| 177 | + /** |
| 178 | + * Resolve external JSDoc module links. |
| 179 | + * Returns a tuple with link and title. |
| 180 | + */ |
| 181 | + resolveExternalJsdocModule?( |
| 182 | + module: string, |
| 183 | + symbol?: string, |
| 184 | + ): { link: string; title: string } | undefined; |
| 185 | +} |
| 186 | + |
| 187 | +export interface UsageComposerEntry { |
| 188 | + /** Name for the entry. Can be left blank in singleMode. */ |
| 189 | + name: string; |
| 190 | + /** Icon for the entry. */ |
| 191 | + icon?: string; |
| 192 | +} |
| 193 | + |
| 194 | +export type UsageToMd = ( |
| 195 | + url: string, |
| 196 | + customFileIdentifier: string | undefined, |
| 197 | +) => string; |
| 198 | + |
| 199 | +export interface UsageComposer { |
| 200 | + /** Whether the usage should only display a single item and not have a dropdown. */ |
| 201 | + singleMode: boolean; |
| 202 | + |
| 203 | + /** |
| 204 | + * Composer to generate usage. |
| 205 | + * |
| 206 | + * @param currentResolve The current resolve. |
| 207 | + * @param usageToMd Callback to generate a usage import block. |
| 208 | + */ |
| 209 | + compose( |
| 210 | + currentResolve: UrlResolveKind, |
| 211 | + usageToMd: UsageToMd, |
| 212 | + ): Map<UsageComposerEntry, string>; |
| 213 | +} |
| 214 | + |
| 215 | +interface GenerateOptions { |
| 216 | + /** The name of the package to use in the breadcrumbs. */ |
| 217 | + packageName?: string; |
| 218 | + /** The main entrypoint if one is present. */ |
| 219 | + mainEntrypoint?: string; |
| 220 | + /** Composer for generating the usage of a symbol of module. */ |
| 221 | + usageComposer?: UsageComposer; |
| 222 | + /** Resolver for how links should be resolved. */ |
| 223 | + hrefResolver?: HrefResolver; |
| 224 | + /** Map for remapping module names to a custom value. */ |
| 225 | + rewriteMap?: Record<string, string>; |
| 226 | + /** |
| 227 | + * Map of categories to their markdown description. |
| 228 | + * Only usable in category mode (single d.ts file with categories declared). |
| 229 | + */ |
| 230 | + categoryDocs?: Record<string, string | undefined>; |
| 231 | + /** Whether to disable search. */ |
| 232 | + disableSearch?: boolean; |
| 233 | + /** |
| 234 | + * Map of modules, where the value is a map of symbols with value of a link to |
| 235 | + * where this symbol should redirect to. |
| 236 | + */ |
| 237 | + symbolRedirectMap?: Record<string, Record<string, string>>; |
| 238 | + /** |
| 239 | + * Map of modules, where the value is a link to where the default symbol |
| 240 | + * should redirect to. |
| 241 | + */ |
| 242 | + defaultRedirectMap?: Record<string, string>; |
| 243 | + /** |
| 244 | + * Hook to inject content in the `head` tag. |
| 245 | + * |
| 246 | + * @param root the path to the root of the output. |
| 247 | + */ |
| 248 | + headInject?(root: string): string; |
| 249 | + /** |
| 250 | + * Function to render markdown. |
| 251 | + * |
| 252 | + * @param md The raw markdown that needs to be rendered. |
| 253 | + * @param titleOnly Whether only the title should be rendered. Recommended syntax to keep is: |
| 254 | + * - paragraph |
| 255 | + * - heading |
| 256 | + * - text |
| 257 | + * - code |
| 258 | + * - html inline |
| 259 | + * - emph |
| 260 | + * - strong |
| 261 | + * - strikethrough |
| 262 | + * - superscript |
| 263 | + * - link |
| 264 | + * - math |
| 265 | + * - escaped |
| 266 | + * - wiki link |
| 267 | + * - underline |
| 268 | + * - soft break |
| 269 | + * @param filePath The filepath where the rendering is happening. |
| 270 | + * @param anchorizer Anchorizer used to generate slugs and the sidebar. |
| 271 | + * @return The rendered markdown. |
| 272 | + */ |
| 273 | + markdownRenderer( |
| 274 | + md: string, |
| 275 | + titleOnly: boolean, |
| 276 | + filePath: ShortPath | undefined, |
| 277 | + anchorizer: (content: string, depthLevel: number) => string, |
| 278 | + ): string | undefined; |
| 279 | + /** Function to strip markdown. */ |
| 280 | + markdownStripper(md: string): string; |
| 281 | +} |
| 282 | + |
| 283 | +const defaultUsageComposer: UsageComposer = { |
| 284 | + singleMode: true, |
| 285 | + compose(currentResolve, usageToMd) { |
| 286 | + if ("file" in currentResolve) { |
| 287 | + return new Map([[ |
| 288 | + { name: "" }, |
| 289 | + usageToMd(currentResolve.file.specifier, undefined), |
| 290 | + ]]); |
| 291 | + } else { |
| 292 | + return new Map(); |
| 293 | + } |
| 294 | + }, |
| 295 | +}; |
| 296 | + |
| 297 | +/** |
| 298 | + * Generate HTML files for provided {@linkcode DocNode}s. |
| 299 | + * @param options Options for the generation. |
| 300 | + * @param docNodesByUrl DocNodes keyed by their absolute URL. |
| 301 | + */ |
| 302 | +export async function generateHtml( |
| 303 | + options: GenerateOptions, |
| 304 | + docNodesByUrl: Record<string, Array<DocNode>>, |
| 305 | +): Promise<Record<string, string>> { |
| 306 | + const { |
| 307 | + usageComposer = defaultUsageComposer, |
| 308 | + } = options; |
| 309 | + |
| 310 | + const wasm = await instantiate(); |
| 311 | + return wasm.generate_html( |
| 312 | + options.packageName, |
| 313 | + options.mainEntrypoint, |
| 314 | + usageComposer.singleMode, |
| 315 | + usageComposer.compose, |
| 316 | + options.rewriteMap, |
| 317 | + options.categoryDocs, |
| 318 | + options.disableSearch ?? false, |
| 319 | + options.symbolRedirectMap, |
| 320 | + options.defaultRedirectMap, |
| 321 | + options.hrefResolver?.resolvePath, |
| 322 | + options.hrefResolver?.resolveGlobalSymbol || (() => undefined), |
| 323 | + options.hrefResolver?.resolveImportHref || (() => undefined), |
| 324 | + options.hrefResolver?.resolveSource || (() => undefined), |
| 325 | + options.hrefResolver?.resolveExternalJsdocModule || (() => undefined), |
| 326 | + options.markdownRenderer, |
| 327 | + options.markdownStripper, |
| 328 | + options.headInject, |
| 329 | + docNodesByUrl, |
| 330 | + ); |
| 331 | +} |
0 commit comments