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

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,86 @@ 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
+ if (!ora) return await fn();
609
+ const spinner = ora(label).start();
610
+ const begin = Date.now();
611
+ try {
612
+ const result = await fn();
613
+ spinner.succeed(`${label} (${formatElapsed(Date.now() - begin)})`);
614
+ return result;
615
+ } catch (err) {
616
+ spinner.fail(label);
617
+ throw err;
618
+ }
619
+ }
620
+ return { stage };
621
+ }
489
622
  async function publish(data, opts, tutorials) {
490
623
  const destination = opts.destination;
491
624
  if (!destination || typeof destination !== "string") {
@@ -493,6 +626,14 @@ async function publish(data, opts, tutorials) {
493
626
  'clean-jsdoc-theme publish: opts.destination is required (set "opts.destination" in your jsdoc.json).'
494
627
  );
495
628
  }
629
+ assertMarkdownPlugin();
630
+ const color = Boolean(process.stdout.isTTY) && opts.nocolor !== true;
631
+ const docsDir = typeof opts.docs === "string" && opts.docs.trim().length > 0 ? opts.docs.trim() : void 0;
632
+ const pkgPromise = resolvePkg(data, opts);
633
+ const sourcesPromise = outputSourceFilesEnabled(opts) ? collectSourceFiles(data) : Promise.resolve([]);
634
+ const docsPromise = docsDir ? collectDocs(docsDir) : Promise.resolve([]);
635
+ const ora = opts.progress === false ? null : await loadOra();
636
+ const progress = createBuildProgress(ora);
496
637
  const [
497
638
  { generateSite },
498
639
  { render, runPagefindAgainstDir },
@@ -504,17 +645,22 @@ async function publish(data, opts, tutorials) {
504
645
  normalizeBasePath,
505
646
  withBase
506
647
  }
507
- ] = await Promise.all([loadSetu(), loadDwar(), loadUtils()]);
648
+ ] = await progress.stage(
649
+ "Loading renderer",
650
+ () => Promise.all([loadSetu(), loadDwar(), loadUtils()])
651
+ );
508
652
  const basePath = normalizeBasePath(opts.basePath);
509
653
  const hrefForServed = (servedPath) => withBase(basePath, "/" + servedPath);
510
654
  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;
655
+ const { value, diagnostics } = await progress.stage(
656
+ "Validating options",
657
+ () => validateThemeOpts({
658
+ opts,
659
+ fontResolver,
660
+ unknownKeyPolicy: "suggest-typos",
661
+ knownNonThemeKeys: JSDOC_OWN_OPTS
662
+ })
663
+ );
518
664
  const strict = opts.strict === true || opts.templates?.default?.strict === true;
519
665
  if (diagnostics.list.length > 0) {
520
666
  const formatted = formatDiagnostics(diagnostics, { color });
@@ -526,19 +672,29 @@ async function publish(data, opts, tutorials) {
526
672
  }
527
673
  console.log(formatted);
528
674
  }
529
- const pkg = await resolvePkg(data, opts);
675
+ const pkg = await pkgPromise;
530
676
  const readme = typeof opts.readme === "string" && opts.readme.length > 0 ? opts.readme : void 0;
531
677
  const tutorialTree = normalizeTutorials(tutorials);
532
- const sources = outputSourceFilesEnabled(opts) ? await collectSourceFiles(data) : [];
533
678
  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) : [];
679
+ const { sources, docs, docImageFiles, inlineSvgs } = await progress.stage(
680
+ "Reading sources & docs",
681
+ async () => {
682
+ const [srcs, rawDocs] = await Promise.all([sourcesPromise, docsPromise]);
683
+ const resolved = docsDir ? await resolveDocImages(rawDocs, docsDir) : { docs: rawDocs, files: [], inlineSvgs: {} };
684
+ return {
685
+ sources: srcs,
686
+ docs: resolved.docs,
687
+ docImageFiles: resolved.files,
688
+ inlineSvgs: resolved.inlineSvgs
689
+ };
690
+ }
691
+ );
536
692
  const docGroups = normalizeDocGroups(opts.docGroups);
537
693
  const defaultDocGroup = typeof opts.defaultDocGroup === "string" && opts.defaultDocGroup.trim().length > 0 ? opts.defaultDocGroup.trim() : void 0;
538
694
  const sectionOrder = normalizeSectionOrder(opts.sectionOrder);
539
695
  const menu = normalizeMenu(opts.menu);
540
696
  const clubSidebarItems = opts.clubSidebarItems === true;
541
- const manifest = generateSite(data, {
697
+ const manifest = await progress.stage("Generating pages", () => generateSite(data, {
542
698
  ...pkg ? { pkg } : {},
543
699
  ...readme ? { readme } : {},
544
700
  ...tutorialTree.length > 0 ? { tutorials: tutorialTree } : {},
@@ -550,7 +706,7 @@ async function publish(data, opts, tutorials) {
550
706
  ...sectionOrder ? { sectionOrder } : {},
551
707
  ...menu ? { menu } : {},
552
708
  ...clubSidebarItems ? { clubSidebarItems } : {}
553
- });
709
+ }));
554
710
  const { siteName, files: logoFiles } = await prepareSiteName(value.siteName, hrefForServed);
555
711
  const notGoogle = new Set(
556
712
  diagnostics.list.filter((d) => d.code === "fonts/not-google" && d.path).map((d) => d.path)
@@ -560,12 +716,21 @@ async function publish(data, opts, tutorials) {
560
716
  if (notGoogle.has("fonts.body")) delete fonts.body;
561
717
  const customAssets = await resolveCustomAssets(opts, hrefForServed);
562
718
  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);
719
+ const islandCacheDir = resolvePath(process.cwd(), "node_modules", ".cache", "clean-jsdoc-theme");
720
+ const result = await progress.stage(
721
+ "Rendering site",
722
+ () => render(manifest, {
723
+ theme: { ...resolveTheme(opts, siteName, fonts, basePath), ...customAssets.theme },
724
+ destination: absoluteDestination,
725
+ islandCacheDir,
726
+ inlineSvgs
727
+ })
728
+ );
729
+ const outputFiles = [...result.files, ...logoFiles, ...customAssets.files, ...docImageFiles];
730
+ await progress.stage(
731
+ "Writing files",
732
+ () => writeOutputFiles(absoluteDestination, outputFiles)
733
+ );
569
734
  const zlib = await import("zlib");
570
735
  const gzipSizer = (b) => zlib.gzipSync(b).length;
571
736
  console.log(
@@ -586,20 +751,29 @@ async function publish(data, opts, tutorials) {
586
751
  }
587
752
  }
588
753
  try {
589
- await runPagefindAgainstDir(absoluteDestination);
754
+ await progress.stage(
755
+ "Indexing search (pagefind)",
756
+ () => runPagefindAgainstDir(absoluteDestination)
757
+ );
590
758
  } catch (err) {
591
- console.warn(`clean-jsdoc-theme: pagefind step skipped \u2014 ${err.message}`);
759
+ console.warn(
760
+ `clean-jsdoc-theme: pagefind step skipped (optional) \u2014 ${err.message}`
761
+ );
592
762
  }
593
763
  }
594
764
  export {
595
765
  collectDocs,
596
766
  computeRelPaths,
767
+ hasMarkdownPlugin,
768
+ normalizeColors,
597
769
  normalizeCopyPage,
598
770
  normalizeDocGroups,
599
771
  normalizeMenu,
772
+ normalizePageNav,
600
773
  normalizeSectionOrder,
601
774
  outputSourceFilesEnabled,
602
775
  publish,
776
+ resolveDocImages,
603
777
  sourceLinkToCommentEnabled
604
778
  };
605
779
  //# sourceMappingURL=publish.mjs.map