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

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
@@ -6,7 +6,7 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
6
6
  });
7
7
 
8
8
  // src/publish.ts
9
- import { readFile, readdir } from "fs/promises";
9
+ import { mkdir as mkdir2, readFile, readdir, writeFile as writeFile2 } from "fs/promises";
10
10
  import { readFileSync } from "fs";
11
11
  import { createHash } from "crypto";
12
12
  import { basename, dirname as dirname2, extname, join as joinPath, resolve as resolvePath } from "path";
@@ -98,7 +98,7 @@ async function loadDep(name, requiredExports) {
98
98
  }
99
99
  return mod;
100
100
  }
101
- var loadSetu = () => loadDep("@clean-jsdoc-theme/setu", ["generateSite"]);
101
+ var loadSetu = () => loadDep("@clean-jsdoc-theme/setu", ["generateSite", "stampSite"]);
102
102
  var loadDwar = () => loadDep("@clean-jsdoc-theme/dwar", ["render", "runPagefindAgainstDir"]);
103
103
  var loadUtils = () => loadDep("@clean-jsdoc-theme/utils", [
104
104
  "validateThemeOpts",
@@ -106,7 +106,8 @@ var loadUtils = () => loadDep("@clean-jsdoc-theme/utils", [
106
106
  "formatDiagnostics",
107
107
  "formatBuildReport",
108
108
  "normalizeBasePath",
109
- "withBase"
109
+ "withBase",
110
+ "toExtractManifest"
110
111
  ]);
111
112
  var loadOra = async () => {
112
113
  try {
@@ -168,7 +169,16 @@ var defaultTheme = {
168
169
  },
169
170
  basePath: "/"
170
171
  };
171
- function resolveTheme(opts, siteName, fonts, basePath) {
172
+ function resolveFontSet(fonts, locale) {
173
+ const base = defaultTheme.tokens.fonts;
174
+ const override = locale && fonts.locales?.[locale] || {};
175
+ return {
176
+ heading: override.heading ?? fonts.heading ?? base.heading,
177
+ body: override.body ?? fonts.body ?? base.body,
178
+ mono: override.mono ?? fonts.mono ?? base.mono
179
+ };
180
+ }
181
+ function resolveTheme(opts, siteName, fonts, basePath, locale) {
172
182
  const aiPrompt = typeof opts.aiPrompt === "string" && opts.aiPrompt.trim() ? opts.aiPrompt.trim() : void 0;
173
183
  const copyPage = normalizeCopyPage(opts.copyPage);
174
184
  const pageNav = normalizePageNav(opts.pageNav);
@@ -184,11 +194,7 @@ function resolveTheme(opts, siteName, fonts, basePath) {
184
194
  ...defaultTheme.tokens,
185
195
  colors: { ...defaultTheme.tokens.colors, ...colors ?? {} },
186
196
  darkColors: { ...defaultTheme.tokens.darkColors, ...darkColors ?? {} },
187
- fonts: {
188
- heading: fonts.heading ?? defaultTheme.tokens.fonts.heading,
189
- body: fonts.body ?? defaultTheme.tokens.fonts.body,
190
- mono: fonts.mono ?? defaultTheme.tokens.fonts.mono
191
- },
197
+ fonts: resolveFontSet(fonts, locale),
192
198
  ...siteName ? { siteName } : {}
193
199
  }
194
200
  };
@@ -346,9 +352,7 @@ function sourceLinkToCommentEnabled(opts) {
346
352
  }
347
353
  function hasMarkdownPlugin(plugins) {
348
354
  if (!Array.isArray(plugins)) return false;
349
- return plugins.some(
350
- (p) => typeof p === "string" && /(^|[\\/])markdown(\.js)?$/i.test(p.trim())
351
- );
355
+ return plugins.some((p) => typeof p === "string" && /(^|[\\/])markdown(\.js)?$/i.test(p.trim()));
352
356
  }
353
357
  function assertMarkdownPlugin() {
354
358
  let conf;
@@ -597,6 +601,17 @@ async function resolveDocImages(docs, docsDir) {
597
601
  }
598
602
  return { docs: out, files, inlineSvgs };
599
603
  }
604
+ function overlayDocs(base, locale) {
605
+ const byPath = new Map(base.docs.map((d) => [d.path, d]));
606
+ for (const d of locale.docs) byPath.set(d.path, d);
607
+ const filesByPath = /* @__PURE__ */ new Map();
608
+ for (const f of [...base.files, ...locale.files]) filesByPath.set(f.path, f);
609
+ return {
610
+ docs: [...byPath.values()].sort((a, b) => a.path.localeCompare(b.path)),
611
+ files: [...filesByPath.values()],
612
+ inlineSvgs: { ...base.inlineSvgs, ...locale.inlineSvgs }
613
+ };
614
+ }
600
615
  function normalizeDocGroups(raw) {
601
616
  return normalizeSectionOrder(raw);
602
617
  }
@@ -636,8 +651,33 @@ function createBuildProgress(ora) {
636
651
  }
637
652
  return { stage };
638
653
  }
654
+ function readBuildSpec() {
655
+ const path = process.env.CLEAN_JSDOC_THEME_BUILD?.trim();
656
+ if (!path) return null;
657
+ const spec = JSON.parse(readFileSync(path, "utf8"));
658
+ if (typeof spec.version === "number" && spec.version !== 1) {
659
+ throw new Error(
660
+ `clean-jsdoc-theme: build spec version ${spec.version} unsupported (expected 1) \u2014 update clean-jsdoc-theme and aadesh together.`
661
+ );
662
+ }
663
+ if (typeof spec.destination !== "string" || typeof spec.basePath !== "string") {
664
+ throw new Error(`clean-jsdoc-theme: malformed build spec at "${path}".`);
665
+ }
666
+ return {
667
+ locale: typeof spec.locale === "string" ? spec.locale : "",
668
+ defaultLocale: typeof spec.defaultLocale === "string" ? spec.defaultLocale : "en",
669
+ apiMessages: spec.apiMessages ?? {},
670
+ chromeMessages: spec.chromeMessages ?? {},
671
+ destination: spec.destination,
672
+ basePath: spec.basePath,
673
+ siteBasePath: typeof spec.siteBasePath === "string" ? spec.siteBasePath : "/",
674
+ locales: Array.isArray(spec.locales) ? spec.locales : [],
675
+ ...typeof spec.docsDir === "string" && spec.docsDir ? { docsDir: spec.docsDir } : {}
676
+ };
677
+ }
639
678
  async function publish(data, opts, tutorials) {
640
- const destination = opts.destination;
679
+ const buildSpec = readBuildSpec();
680
+ const destination = buildSpec?.destination ?? opts.destination;
641
681
  if (!destination || typeof destination !== "string") {
642
682
  throw new Error(
643
683
  'clean-jsdoc-theme publish: opts.destination is required (set "opts.destination" in your jsdoc.json).'
@@ -652,7 +692,7 @@ async function publish(data, opts, tutorials) {
652
692
  const ora = opts.progress === false ? null : await loadOra();
653
693
  const progress = createBuildProgress(ora);
654
694
  const [
655
- { generateSite },
695
+ { generateSite, stampSite },
656
696
  { render, runPagefindAgainstDir },
657
697
  {
658
698
  validateThemeOpts,
@@ -660,7 +700,8 @@ async function publish(data, opts, tutorials) {
660
700
  formatDiagnostics,
661
701
  formatBuildReport,
662
702
  normalizeBasePath,
663
- withBase
703
+ withBase,
704
+ toExtractManifest
664
705
  }
665
706
  // Loaded sequentially (not Promise.all) so the spinner can step its label
666
707
  // through each module — the evaluation of these large ESM bundles is the
@@ -675,7 +716,7 @@ async function publish(data, opts, tutorials) {
675
716
  const utils = await loadUtils();
676
717
  return [setu, dwar, utils];
677
718
  });
678
- const basePath = normalizeBasePath(opts.basePath);
719
+ const basePath = normalizeBasePath(buildSpec?.basePath ?? opts.basePath);
679
720
  const hrefForServed = (servedPath) => withBase(basePath, "/" + servedPath);
680
721
  const fontResolver = createGoogleFontResolver();
681
722
  const { value, diagnostics } = await progress.stage(
@@ -706,12 +747,23 @@ async function publish(data, opts, tutorials) {
706
747
  "Reading sources & docs",
707
748
  async () => {
708
749
  const [srcs, rawDocs] = await Promise.all([sourcesPromise, docsPromise]);
709
- const resolved = docsDir ? await resolveDocImages(rawDocs, docsDir) : { docs: rawDocs, files: [], inlineSvgs: {} };
750
+ const base = docsDir ? await resolveDocImages(rawDocs, docsDir) : { docs: rawDocs, files: [], inlineSvgs: {} };
751
+ if (!buildSpec?.docsDir) {
752
+ return {
753
+ sources: srcs,
754
+ docs: base.docs,
755
+ docImageFiles: base.files,
756
+ inlineSvgs: base.inlineSvgs
757
+ };
758
+ }
759
+ const localeRaw = await collectDocs(buildSpec.docsDir);
760
+ const locale = await resolveDocImages(localeRaw, buildSpec.docsDir);
761
+ const merged = overlayDocs(base, locale);
710
762
  return {
711
763
  sources: srcs,
712
- docs: resolved.docs,
713
- docImageFiles: resolved.files,
714
- inlineSvgs: resolved.inlineSvgs
764
+ docs: merged.docs,
765
+ docImageFiles: merged.files,
766
+ inlineSvgs: merged.inlineSvgs
715
767
  };
716
768
  }
717
769
  );
@@ -720,7 +772,7 @@ async function publish(data, opts, tutorials) {
720
772
  const sectionOrder = normalizeSectionOrder(opts.sectionOrder);
721
773
  const menu = normalizeMenu(opts.menu);
722
774
  const clubSidebarItems = opts.clubSidebarItems === true;
723
- const manifest = await progress.stage("Generating pages", () => generateSite(data, {
775
+ const siteOptions = {
724
776
  ...pkg ? { pkg } : {},
725
777
  ...readme ? { readme } : {},
726
778
  ...tutorialTree.length > 0 ? { tutorials: tutorialTree } : {},
@@ -732,7 +784,23 @@ async function publish(data, opts, tutorials) {
732
784
  ...sectionOrder ? { sectionOrder } : {},
733
785
  ...menu ? { menu } : {},
734
786
  ...clubSidebarItems ? { clubSidebarItems } : {}
735
- }));
787
+ };
788
+ const manifest = await progress.stage(
789
+ "Generating pages",
790
+ () => buildSpec ? stampSite(data, buildSpec.apiMessages, siteOptions) : generateSite(data, siteOptions)
791
+ );
792
+ const extractPath = process.env.CLEAN_JSDOC_THEME_EXTRACT?.trim();
793
+ if (extractPath) {
794
+ const target = resolvePath(extractPath);
795
+ await progress.stage("Writing extract manifest", async () => {
796
+ await mkdir2(dirname2(target), { recursive: true });
797
+ await writeFile2(target, JSON.stringify(toExtractManifest(manifest), null, 2) + "\n", "utf8");
798
+ });
799
+ console.log(
800
+ `clean-jsdoc-theme: extract mode \u2014 wrote ${manifest.slots?.length ?? 0} slot(s) to ${target}`
801
+ );
802
+ return;
803
+ }
736
804
  const { siteName, files: logoFiles } = await prepareSiteName(value.siteName, hrefForServed);
737
805
  const notGoogle = new Set(
738
806
  diagnostics.list.filter((d) => d.code === "fonts/not-google" && d.path).map((d) => d.path)
@@ -740,23 +808,44 @@ async function publish(data, opts, tutorials) {
740
808
  const fonts = { ...value.fonts };
741
809
  if (notGoogle.has("fonts.heading")) delete fonts.heading;
742
810
  if (notGoogle.has("fonts.body")) delete fonts.body;
811
+ if (fonts.locales) {
812
+ const cleaned = {};
813
+ for (const [loc, set] of Object.entries(fonts.locales)) {
814
+ const s = { ...set };
815
+ if (notGoogle.has(`fonts.${loc}:heading`)) delete s.heading;
816
+ if (notGoogle.has(`fonts.${loc}:body`)) delete s.body;
817
+ cleaned[loc] = s;
818
+ }
819
+ fonts.locales = cleaned;
820
+ }
743
821
  const customAssets = await resolveCustomAssets(opts, hrefForServed);
744
822
  const absoluteDestination = resolvePath(destination);
745
823
  const islandCacheDir = resolvePath(process.cwd(), "node_modules", ".cache", "clean-jsdoc-theme");
746
824
  const result = await progress.stage(
747
825
  "Rendering site",
748
826
  () => render(manifest, {
749
- theme: { ...resolveTheme(opts, siteName, fonts, basePath), ...customAssets.theme },
827
+ theme: {
828
+ ...resolveTheme(opts, siteName, fonts, basePath, buildSpec?.locale),
829
+ ...customAssets.theme
830
+ },
750
831
  destination: absoluteDestination,
751
832
  islandCacheDir,
752
- inlineSvgs
833
+ inlineSvgs,
834
+ // Build mode: render chrome in the locale (SSR provider + island seeding)
835
+ // and, with >1 locale, mount the language switcher.
836
+ ...buildSpec ? {
837
+ locale: {
838
+ code: buildSpec.locale,
839
+ defaultLocale: buildSpec.defaultLocale,
840
+ messages: buildSpec.chromeMessages,
841
+ siteBasePath: buildSpec.siteBasePath,
842
+ locales: buildSpec.locales.map((l) => ({ code: l.code, label: l.name ?? l.code }))
843
+ }
844
+ } : {}
753
845
  })
754
846
  );
755
847
  const outputFiles = [...result.files, ...logoFiles, ...customAssets.files, ...docImageFiles];
756
- await progress.stage(
757
- "Writing files",
758
- () => writeOutputFiles(absoluteDestination, outputFiles)
759
- );
848
+ await progress.stage("Writing files", () => writeOutputFiles(absoluteDestination, outputFiles));
760
849
  const zlib = await import("zlib");
761
850
  const gzipSizer = (b) => zlib.gzipSync(b).length;
762
851
  console.log(
@@ -782,9 +871,7 @@ async function publish(data, opts, tutorials) {
782
871
  () => runPagefindAgainstDir(absoluteDestination)
783
872
  );
784
873
  } catch (err) {
785
- console.warn(
786
- `clean-jsdoc-theme: pagefind step skipped (optional) \u2014 ${err.message}`
787
- );
874
+ console.warn(`clean-jsdoc-theme: pagefind step skipped (optional) \u2014 ${err.message}`);
788
875
  }
789
876
  }
790
877
  export {
@@ -798,6 +885,7 @@ export {
798
885
  normalizePageNav,
799
886
  normalizeSectionOrder,
800
887
  outputSourceFilesEnabled,
888
+ overlayDocs,
801
889
  publish,
802
890
  resolveDocImages,
803
891
  sourceLinkToCommentEnabled