clean-jsdoc-theme 5.0.0-alpha.1 → 5.0.0-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/publish.mjs CHANGED
@@ -58,25 +58,26 @@ function resolvePackageDir(name) {
58
58
  }
59
59
  throw new Error(`clean-jsdoc-theme: could not locate '${name}' on disk starting from ${start}.`);
60
60
  }
61
+ function resolveExportTarget(node, pkgRoot) {
62
+ if (typeof node === "string") return resolvePath(pkgRoot, node);
63
+ if (!node || typeof node !== "object") return void 0;
64
+ const o = node;
65
+ const imp = resolveExportTarget(o.import, pkgRoot);
66
+ if (imp) return imp;
67
+ if (typeof o.default === "string") return resolvePath(pkgRoot, o.default);
68
+ return void 0;
69
+ }
61
70
  function resolveEsmEntry(name) {
62
71
  const pkgRoot = resolvePackageDir(name);
63
72
  const pkg = JSON.parse(
64
73
  readFileSync(resolvePath(pkgRoot, "package.json"), "utf8")
65
74
  );
66
75
  const exp = pkg.exports;
76
+ if (typeof exp === "string") return resolvePath(pkgRoot, exp);
67
77
  if (exp && typeof exp === "object") {
68
78
  const dot = exp["."];
69
- if (typeof dot === "string") return resolvePath(pkgRoot, dot);
70
- if (dot && typeof dot === "object") {
71
- const imp = dot.import;
72
- if (typeof imp === "string") return resolvePath(pkgRoot, imp);
73
- if (imp && typeof imp === "object") {
74
- const def2 = imp.default;
75
- if (typeof def2 === "string") return resolvePath(pkgRoot, def2);
76
- }
77
- const def = dot.default;
78
- if (typeof def === "string") return resolvePath(pkgRoot, def);
79
- }
79
+ const target = resolveExportTarget(dot !== void 0 ? dot : exp, pkgRoot);
80
+ if (target) return target;
80
81
  }
81
82
  if (typeof pkg.module === "string") return resolvePath(pkgRoot, pkg.module);
82
83
  if (typeof pkg.main === "string") return resolvePath(pkgRoot, pkg.main);
@@ -107,6 +108,14 @@ var loadUtils = () => loadDep("@clean-jsdoc-theme/utils", [
107
108
  "normalizeBasePath",
108
109
  "withBase"
109
110
  ]);
111
+ var loadOra = async () => {
112
+ try {
113
+ const mod = await loadDep("ora", ["default"]);
114
+ return mod.default;
115
+ } catch {
116
+ return null;
117
+ }
118
+ };
110
119
  var JSDOC_OWN_OPTS = /* @__PURE__ */ new Set([
111
120
  "destination",
112
121
  "template",
@@ -162,13 +171,19 @@ var defaultTheme = {
162
171
  function resolveTheme(opts, siteName, fonts, basePath) {
163
172
  const aiPrompt = typeof opts.aiPrompt === "string" && opts.aiPrompt.trim() ? opts.aiPrompt.trim() : void 0;
164
173
  const copyPage = normalizeCopyPage(opts.copyPage);
174
+ const pageNav = normalizePageNav(opts.pageNav);
175
+ const colors = normalizeColors(opts.colors);
176
+ const darkColors = normalizeColors(opts.darkColors);
165
177
  return {
166
178
  ...defaultTheme,
167
179
  basePath,
168
180
  ...aiPrompt ? { aiPrompt } : {},
169
181
  ...copyPage ? { copyPage } : {},
182
+ ...pageNav ? { pageNav } : {},
170
183
  tokens: {
171
184
  ...defaultTheme.tokens,
185
+ colors: { ...defaultTheme.tokens.colors, ...colors ?? {} },
186
+ darkColors: { ...defaultTheme.tokens.darkColors, ...darkColors ?? {} },
172
187
  fonts: {
173
188
  heading: fonts.heading ?? defaultTheme.tokens.fonts.heading,
174
189
  body: fonts.body ?? defaultTheme.tokens.fonts.body,
@@ -251,7 +266,7 @@ async function prepareSiteName(raw, hrefForServed) {
251
266
  try {
252
267
  const abs = resolvePath(v);
253
268
  const buf = await readFile(abs);
254
- const served = `_assets/logo-${key}${extname(abs)}`;
269
+ const served = `_assets/logo-${key}.${contentHash(buf)}${extname(abs)}`;
255
270
  files.push({ path: served, contents: buf });
256
271
  out[key] = hrefForServed(served);
257
272
  } catch {
@@ -329,6 +344,29 @@ function sourceLinkToCommentEnabled(opts) {
329
344
  if (opts?.templates?.default?.sourceLinkToComment === true) return true;
330
345
  return false;
331
346
  }
347
+ function hasMarkdownPlugin(plugins) {
348
+ if (!Array.isArray(plugins)) return false;
349
+ return plugins.some(
350
+ (p) => typeof p === "string" && /(^|[\\/])markdown(\.js)?$/i.test(p.trim())
351
+ );
352
+ }
353
+ function assertMarkdownPlugin() {
354
+ let conf;
355
+ try {
356
+ conf = __require("jsdoc/env").conf;
357
+ } catch {
358
+ return;
359
+ }
360
+ if (!conf) return;
361
+ if (!hasMarkdownPlugin(conf.plugins)) {
362
+ throw new Error(
363
+ `clean-jsdoc-theme requires JSDoc's "plugins/markdown" plugin, which renders Markdown in doclet descriptions to HTML. Add it to your jsdoc.json:
364
+
365
+ "plugins": ["plugins/markdown"]
366
+ `
367
+ );
368
+ }
369
+ }
332
370
  function normalizeSectionOrder(raw) {
333
371
  if (!Array.isArray(raw)) return void 0;
334
372
  const out = raw.filter((s) => typeof s === "string").map((s) => s.trim()).filter((s) => s.length > 0);
@@ -375,6 +413,24 @@ function normalizeCopyPage(raw) {
375
413
  }
376
414
  return Object.keys(config).length > 0 ? config : void 0;
377
415
  }
416
+ function normalizePageNav(raw) {
417
+ if (raw === false) return { enabled: false };
418
+ if (raw === true || raw == null) return void 0;
419
+ if (typeof raw !== "object") return void 0;
420
+ const enabled = raw.enabled;
421
+ return typeof enabled === "boolean" ? { enabled } : void 0;
422
+ }
423
+ var COLOR_KEYS = ["bg", "bgMuted", "fg", "fgMuted", "accent", "accentFg", "border"];
424
+ function normalizeColors(raw) {
425
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) return void 0;
426
+ const src = raw;
427
+ const out = {};
428
+ for (const key of COLOR_KEYS) {
429
+ const v = src[key];
430
+ if (typeof v === "string" && v.trim().length > 0) out[key] = v.trim();
431
+ }
432
+ return Object.keys(out).length > 0 ? out : void 0;
433
+ }
378
434
  var SOURCE_EXTENSIONS = /* @__PURE__ */ new Set([".js", ".mjs", ".cjs", ".jsx", ".ts", ".mts", ".cts", ".tsx"]);
379
435
  function computeRelPaths(absPaths) {
380
436
  const out = /* @__PURE__ */ new Map();
@@ -483,9 +539,103 @@ async function collectDocs(dir) {
483
539
  docs.sort((a, b) => a.path.localeCompare(b.path));
484
540
  return docs;
485
541
  }
542
+ var DOC_IMAGE_RE = /(!\[[^\]]*\]\()([^)\s]+)((?:\s+"[^"]*")?\))/g;
543
+ async function resolveDocImages(docs, docsDir) {
544
+ if (docs.length === 0) return { docs, files: [], inlineSvgs: {} };
545
+ const root = resolvePath(docsDir);
546
+ const files = [];
547
+ const seenServed = /* @__PURE__ */ new Set();
548
+ const inlineSvgs = {};
549
+ const cache = /* @__PURE__ */ new Map();
550
+ const resolveOne = async (rawSrc, docDir) => {
551
+ const src = rawSrc.trim();
552
+ if (!src || isServableUrl(src) || src.startsWith("#") || src.startsWith("data:")) return null;
553
+ const abs = src.startsWith("/") ? resolvePath(src.slice(1)) : resolvePath(docDir, src);
554
+ if (cache.has(abs)) return cache.get(abs) ?? null;
555
+ let bytes;
556
+ try {
557
+ bytes = await readFile(abs);
558
+ } catch {
559
+ console.warn(
560
+ `clean-jsdoc-theme: could not read doc image '${src}' (resolved '${abs}'); leaving it as-is.`
561
+ );
562
+ cache.set(abs, null);
563
+ return null;
564
+ }
565
+ const ext = extname(abs);
566
+ const served = `_assets/${basename(abs, ext) || "asset"}.${contentHash(bytes)}${ext}`;
567
+ if (!seenServed.has(served)) {
568
+ seenServed.add(served);
569
+ files.push({ path: served, contents: bytes });
570
+ }
571
+ const href = "/" + served;
572
+ if (ext.toLowerCase() === ".svg") {
573
+ inlineSvgs[href] = bytes.toString("utf8").replace(/<svg\b/, '<svg style="max-width:100%;height:auto;display:block"');
574
+ }
575
+ cache.set(abs, href);
576
+ return href;
577
+ };
578
+ const out = [];
579
+ for (const doc of docs) {
580
+ const docDir = dirname2(resolvePath(root, doc.path));
581
+ const map = /* @__PURE__ */ new Map();
582
+ for (const m of doc.content.matchAll(DOC_IMAGE_RE)) {
583
+ const src = m[2];
584
+ if (map.has(src)) continue;
585
+ const href = await resolveOne(src, docDir);
586
+ if (href) map.set(src, href);
587
+ }
588
+ if (map.size === 0) {
589
+ out.push(doc);
590
+ continue;
591
+ }
592
+ const content = doc.content.replace(
593
+ DOC_IMAGE_RE,
594
+ (full, pre, src, post) => map.has(src) ? `${pre}${map.get(src)}${post}` : full
595
+ );
596
+ out.push({ ...doc, content });
597
+ }
598
+ return { docs: out, files, inlineSvgs };
599
+ }
486
600
  function normalizeDocGroups(raw) {
487
601
  return normalizeSectionOrder(raw);
488
602
  }
603
+ function formatElapsed(ms) {
604
+ return ms < 1e3 ? `${ms}ms` : `${(ms / 1e3).toFixed(1)}s`;
605
+ }
606
+ function createBuildProgress(ora) {
607
+ async function stage(label, fn) {
608
+ const noop = { setText: () => {
609
+ } };
610
+ if (!ora) return await fn(noop);
611
+ const spinner = ora(label).start();
612
+ const begin = Date.now();
613
+ let current = label;
614
+ const refresh = () => {
615
+ const secs = Math.floor((Date.now() - begin) / 1e3);
616
+ spinner.text = secs > 0 ? `${current} (${secs}s)` : current;
617
+ };
618
+ const heartbeat = setInterval(refresh, 1e3);
619
+ if (typeof heartbeat.unref === "function") heartbeat.unref();
620
+ const handle = {
621
+ setText(text) {
622
+ current = text;
623
+ refresh();
624
+ }
625
+ };
626
+ try {
627
+ const result = await fn(handle);
628
+ clearInterval(heartbeat);
629
+ spinner.succeed(`${label} (${formatElapsed(Date.now() - begin)})`);
630
+ return result;
631
+ } catch (err) {
632
+ clearInterval(heartbeat);
633
+ spinner.fail(label);
634
+ throw err;
635
+ }
636
+ }
637
+ return { stage };
638
+ }
489
639
  async function publish(data, opts, tutorials) {
490
640
  const destination = opts.destination;
491
641
  if (!destination || typeof destination !== "string") {
@@ -493,6 +643,14 @@ async function publish(data, opts, tutorials) {
493
643
  'clean-jsdoc-theme publish: opts.destination is required (set "opts.destination" in your jsdoc.json).'
494
644
  );
495
645
  }
646
+ assertMarkdownPlugin();
647
+ const color = Boolean(process.stdout.isTTY) && opts.nocolor !== true;
648
+ const docsDir = typeof opts.docs === "string" && opts.docs.trim().length > 0 ? opts.docs.trim() : void 0;
649
+ const pkgPromise = resolvePkg(data, opts);
650
+ const sourcesPromise = outputSourceFilesEnabled(opts) ? collectSourceFiles(data) : Promise.resolve([]);
651
+ const docsPromise = docsDir ? collectDocs(docsDir) : Promise.resolve([]);
652
+ const ora = opts.progress === false ? null : await loadOra();
653
+ const progress = createBuildProgress(ora);
496
654
  const [
497
655
  { generateSite },
498
656
  { render, runPagefindAgainstDir },
@@ -504,17 +662,31 @@ async function publish(data, opts, tutorials) {
504
662
  normalizeBasePath,
505
663
  withBase
506
664
  }
507
- ] = await Promise.all([loadSetu(), loadDwar(), loadUtils()]);
665
+ // Loaded sequentially (not Promise.all) so the spinner can step its label
666
+ // through each module — the evaluation of these large ESM bundles is the
667
+ // single longest, most event-loop-blocking stage, so visibly naming the
668
+ // module in flight is what stops it from reading as a hang.
669
+ ] = await progress.stage("Loading renderer", async ({ setText }) => {
670
+ setText("Loading renderer (setu)");
671
+ const setu = await loadSetu();
672
+ setText("Loading renderer (dwar)");
673
+ const dwar = await loadDwar();
674
+ setText("Loading renderer (utils)");
675
+ const utils = await loadUtils();
676
+ return [setu, dwar, utils];
677
+ });
508
678
  const basePath = normalizeBasePath(opts.basePath);
509
679
  const hrefForServed = (servedPath) => withBase(basePath, "/" + servedPath);
510
680
  const fontResolver = createGoogleFontResolver();
511
- const { value, diagnostics } = await validateThemeOpts({
512
- opts,
513
- fontResolver,
514
- unknownKeyPolicy: "suggest-typos",
515
- knownNonThemeKeys: JSDOC_OWN_OPTS
516
- });
517
- const color = Boolean(process.stdout.isTTY) && opts.nocolor !== true;
681
+ const { value, diagnostics } = await progress.stage(
682
+ "Validating options",
683
+ () => validateThemeOpts({
684
+ opts,
685
+ fontResolver,
686
+ unknownKeyPolicy: "suggest-typos",
687
+ knownNonThemeKeys: JSDOC_OWN_OPTS
688
+ })
689
+ );
518
690
  const strict = opts.strict === true || opts.templates?.default?.strict === true;
519
691
  if (diagnostics.list.length > 0) {
520
692
  const formatted = formatDiagnostics(diagnostics, { color });
@@ -526,19 +698,29 @@ async function publish(data, opts, tutorials) {
526
698
  }
527
699
  console.log(formatted);
528
700
  }
529
- const pkg = await resolvePkg(data, opts);
701
+ const pkg = await pkgPromise;
530
702
  const readme = typeof opts.readme === "string" && opts.readme.length > 0 ? opts.readme : void 0;
531
703
  const tutorialTree = normalizeTutorials(tutorials);
532
- const sources = outputSourceFilesEnabled(opts) ? await collectSourceFiles(data) : [];
533
704
  const sourceLinkToComment = sourceLinkToCommentEnabled(opts);
534
- const docsDir = typeof opts.docs === "string" && opts.docs.trim().length > 0 ? opts.docs.trim() : void 0;
535
- const docs = docsDir ? await collectDocs(docsDir) : [];
705
+ const { sources, docs, docImageFiles, inlineSvgs } = await progress.stage(
706
+ "Reading sources & docs",
707
+ async () => {
708
+ const [srcs, rawDocs] = await Promise.all([sourcesPromise, docsPromise]);
709
+ const resolved = docsDir ? await resolveDocImages(rawDocs, docsDir) : { docs: rawDocs, files: [], inlineSvgs: {} };
710
+ return {
711
+ sources: srcs,
712
+ docs: resolved.docs,
713
+ docImageFiles: resolved.files,
714
+ inlineSvgs: resolved.inlineSvgs
715
+ };
716
+ }
717
+ );
536
718
  const docGroups = normalizeDocGroups(opts.docGroups);
537
719
  const defaultDocGroup = typeof opts.defaultDocGroup === "string" && opts.defaultDocGroup.trim().length > 0 ? opts.defaultDocGroup.trim() : void 0;
538
720
  const sectionOrder = normalizeSectionOrder(opts.sectionOrder);
539
721
  const menu = normalizeMenu(opts.menu);
540
722
  const clubSidebarItems = opts.clubSidebarItems === true;
541
- const manifest = generateSite(data, {
723
+ const manifest = await progress.stage("Generating pages", () => generateSite(data, {
542
724
  ...pkg ? { pkg } : {},
543
725
  ...readme ? { readme } : {},
544
726
  ...tutorialTree.length > 0 ? { tutorials: tutorialTree } : {},
@@ -550,7 +732,7 @@ async function publish(data, opts, tutorials) {
550
732
  ...sectionOrder ? { sectionOrder } : {},
551
733
  ...menu ? { menu } : {},
552
734
  ...clubSidebarItems ? { clubSidebarItems } : {}
553
- });
735
+ }));
554
736
  const { siteName, files: logoFiles } = await prepareSiteName(value.siteName, hrefForServed);
555
737
  const notGoogle = new Set(
556
738
  diagnostics.list.filter((d) => d.code === "fonts/not-google" && d.path).map((d) => d.path)
@@ -560,12 +742,21 @@ async function publish(data, opts, tutorials) {
560
742
  if (notGoogle.has("fonts.body")) delete fonts.body;
561
743
  const customAssets = await resolveCustomAssets(opts, hrefForServed);
562
744
  const absoluteDestination = resolvePath(destination);
563
- const result = await render(manifest, {
564
- theme: { ...resolveTheme(opts, siteName, fonts, basePath), ...customAssets.theme },
565
- destination: absoluteDestination
566
- });
567
- const outputFiles = [...result.files, ...logoFiles, ...customAssets.files];
568
- await writeOutputFiles(absoluteDestination, outputFiles);
745
+ const islandCacheDir = resolvePath(process.cwd(), "node_modules", ".cache", "clean-jsdoc-theme");
746
+ const result = await progress.stage(
747
+ "Rendering site",
748
+ () => render(manifest, {
749
+ theme: { ...resolveTheme(opts, siteName, fonts, basePath), ...customAssets.theme },
750
+ destination: absoluteDestination,
751
+ islandCacheDir,
752
+ inlineSvgs
753
+ })
754
+ );
755
+ const outputFiles = [...result.files, ...logoFiles, ...customAssets.files, ...docImageFiles];
756
+ await progress.stage(
757
+ "Writing files",
758
+ () => writeOutputFiles(absoluteDestination, outputFiles)
759
+ );
569
760
  const zlib = await import("zlib");
570
761
  const gzipSizer = (b) => zlib.gzipSync(b).length;
571
762
  console.log(
@@ -586,20 +777,29 @@ async function publish(data, opts, tutorials) {
586
777
  }
587
778
  }
588
779
  try {
589
- await runPagefindAgainstDir(absoluteDestination);
780
+ await progress.stage(
781
+ "Indexing search (pagefind)",
782
+ () => runPagefindAgainstDir(absoluteDestination)
783
+ );
590
784
  } catch (err) {
591
- console.warn(`clean-jsdoc-theme: pagefind step skipped \u2014 ${err.message}`);
785
+ console.warn(
786
+ `clean-jsdoc-theme: pagefind step skipped (optional) \u2014 ${err.message}`
787
+ );
592
788
  }
593
789
  }
594
790
  export {
595
791
  collectDocs,
596
792
  computeRelPaths,
793
+ hasMarkdownPlugin,
794
+ normalizeColors,
597
795
  normalizeCopyPage,
598
796
  normalizeDocGroups,
599
797
  normalizeMenu,
798
+ normalizePageNav,
600
799
  normalizeSectionOrder,
601
800
  outputSourceFilesEnabled,
602
801
  publish,
802
+ resolveDocImages,
603
803
  sourceLinkToCommentEnabled
604
804
  };
605
805
  //# sourceMappingURL=publish.mjs.map