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

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
  };
@@ -243,6 +249,53 @@ async function resolveCustomAssets(opts, hrefForServed) {
243
249
  files: [...cssAssets.files, ...jsAssets.files]
244
250
  };
245
251
  }
252
+ async function resolveFooter(raw) {
253
+ if (typeof raw === "string") {
254
+ const trimmed = raw.trim();
255
+ return trimmed.length > 0 ? trimmed : void 0;
256
+ }
257
+ if (raw && typeof raw === "object" && typeof raw.file === "string") {
258
+ const file = raw.file.trim();
259
+ if (!file) return void 0;
260
+ try {
261
+ const html = await readFile(resolvePath(file), "utf8");
262
+ return html.trim().length > 0 ? html : void 0;
263
+ } catch {
264
+ console.warn(
265
+ `clean-jsdoc-theme: could not read footer file ('${file}'); omitting the custom footer.`
266
+ );
267
+ return void 0;
268
+ }
269
+ }
270
+ return void 0;
271
+ }
272
+ var META_IDENTITY_ATTRS = ["name", "property", "http-equiv", "charset", "itemprop"];
273
+ function normalizeMeta(raw) {
274
+ if (!Array.isArray(raw)) return void 0;
275
+ const out = [];
276
+ for (const entry of raw) {
277
+ if (!entry || typeof entry !== "object" || Array.isArray(entry)) continue;
278
+ const o = entry;
279
+ const tag = {};
280
+ for (const [k, v] of Object.entries(o)) {
281
+ if (typeof v === "string") {
282
+ const t = v.trim();
283
+ if (t.length > 0) tag[k] = t;
284
+ } else if (typeof v === "number" && Number.isFinite(v)) {
285
+ tag[k] = String(v);
286
+ }
287
+ }
288
+ const hasIdentity = META_IDENTITY_ATTRS.some((a) => typeof tag[a] === "string");
289
+ if (!hasIdentity || Object.keys(tag).length === 0) {
290
+ console.warn(
291
+ `clean-jsdoc-theme: skipping a meta entry with no usable name/property/http-equiv/charset attribute (${JSON.stringify(entry)}).`
292
+ );
293
+ continue;
294
+ }
295
+ out.push(tag);
296
+ }
297
+ return out.length > 0 ? out : void 0;
298
+ }
246
299
  function isServableUrl(value) {
247
300
  return /^(https?:)?\/\//i.test(value) || /^data:/i.test(value);
248
301
  }
@@ -346,9 +399,7 @@ function sourceLinkToCommentEnabled(opts) {
346
399
  }
347
400
  function hasMarkdownPlugin(plugins) {
348
401
  if (!Array.isArray(plugins)) return false;
349
- return plugins.some(
350
- (p) => typeof p === "string" && /(^|[\\/])markdown(\.js)?$/i.test(p.trim())
351
- );
402
+ return plugins.some((p) => typeof p === "string" && /(^|[\\/])markdown(\.js)?$/i.test(p.trim()));
352
403
  }
353
404
  function assertMarkdownPlugin() {
354
405
  let conf;
@@ -597,6 +648,17 @@ async function resolveDocImages(docs, docsDir) {
597
648
  }
598
649
  return { docs: out, files, inlineSvgs };
599
650
  }
651
+ function overlayDocs(base, locale) {
652
+ const byPath = new Map(base.docs.map((d) => [d.path, d]));
653
+ for (const d of locale.docs) byPath.set(d.path, d);
654
+ const filesByPath = /* @__PURE__ */ new Map();
655
+ for (const f of [...base.files, ...locale.files]) filesByPath.set(f.path, f);
656
+ return {
657
+ docs: [...byPath.values()].sort((a, b) => a.path.localeCompare(b.path)),
658
+ files: [...filesByPath.values()],
659
+ inlineSvgs: { ...base.inlineSvgs, ...locale.inlineSvgs }
660
+ };
661
+ }
600
662
  function normalizeDocGroups(raw) {
601
663
  return normalizeSectionOrder(raw);
602
664
  }
@@ -636,8 +698,33 @@ function createBuildProgress(ora) {
636
698
  }
637
699
  return { stage };
638
700
  }
701
+ function readBuildSpec() {
702
+ const path = process.env.CLEAN_JSDOC_THEME_BUILD?.trim();
703
+ if (!path) return null;
704
+ const spec = JSON.parse(readFileSync(path, "utf8"));
705
+ if (typeof spec.version === "number" && spec.version !== 1) {
706
+ throw new Error(
707
+ `clean-jsdoc-theme: build spec version ${spec.version} unsupported (expected 1) \u2014 update clean-jsdoc-theme and aadesh together.`
708
+ );
709
+ }
710
+ if (typeof spec.destination !== "string" || typeof spec.basePath !== "string") {
711
+ throw new Error(`clean-jsdoc-theme: malformed build spec at "${path}".`);
712
+ }
713
+ return {
714
+ locale: typeof spec.locale === "string" ? spec.locale : "",
715
+ defaultLocale: typeof spec.defaultLocale === "string" ? spec.defaultLocale : "en",
716
+ apiMessages: spec.apiMessages ?? {},
717
+ chromeMessages: spec.chromeMessages ?? {},
718
+ destination: spec.destination,
719
+ basePath: spec.basePath,
720
+ siteBasePath: typeof spec.siteBasePath === "string" ? spec.siteBasePath : "/",
721
+ locales: Array.isArray(spec.locales) ? spec.locales : [],
722
+ ...typeof spec.docsDir === "string" && spec.docsDir ? { docsDir: spec.docsDir } : {}
723
+ };
724
+ }
639
725
  async function publish(data, opts, tutorials) {
640
- const destination = opts.destination;
726
+ const buildSpec = readBuildSpec();
727
+ const destination = buildSpec?.destination ?? opts.destination;
641
728
  if (!destination || typeof destination !== "string") {
642
729
  throw new Error(
643
730
  'clean-jsdoc-theme publish: opts.destination is required (set "opts.destination" in your jsdoc.json).'
@@ -652,7 +739,7 @@ async function publish(data, opts, tutorials) {
652
739
  const ora = opts.progress === false ? null : await loadOra();
653
740
  const progress = createBuildProgress(ora);
654
741
  const [
655
- { generateSite },
742
+ { generateSite, stampSite },
656
743
  { render, runPagefindAgainstDir },
657
744
  {
658
745
  validateThemeOpts,
@@ -660,7 +747,8 @@ async function publish(data, opts, tutorials) {
660
747
  formatDiagnostics,
661
748
  formatBuildReport,
662
749
  normalizeBasePath,
663
- withBase
750
+ withBase,
751
+ toExtractManifest
664
752
  }
665
753
  // Loaded sequentially (not Promise.all) so the spinner can step its label
666
754
  // through each module — the evaluation of these large ESM bundles is the
@@ -675,7 +763,7 @@ async function publish(data, opts, tutorials) {
675
763
  const utils = await loadUtils();
676
764
  return [setu, dwar, utils];
677
765
  });
678
- const basePath = normalizeBasePath(opts.basePath);
766
+ const basePath = normalizeBasePath(buildSpec?.basePath ?? opts.basePath);
679
767
  const hrefForServed = (servedPath) => withBase(basePath, "/" + servedPath);
680
768
  const fontResolver = createGoogleFontResolver();
681
769
  const { value, diagnostics } = await progress.stage(
@@ -706,12 +794,23 @@ async function publish(data, opts, tutorials) {
706
794
  "Reading sources & docs",
707
795
  async () => {
708
796
  const [srcs, rawDocs] = await Promise.all([sourcesPromise, docsPromise]);
709
- const resolved = docsDir ? await resolveDocImages(rawDocs, docsDir) : { docs: rawDocs, files: [], inlineSvgs: {} };
797
+ const base = docsDir ? await resolveDocImages(rawDocs, docsDir) : { docs: rawDocs, files: [], inlineSvgs: {} };
798
+ if (!buildSpec?.docsDir) {
799
+ return {
800
+ sources: srcs,
801
+ docs: base.docs,
802
+ docImageFiles: base.files,
803
+ inlineSvgs: base.inlineSvgs
804
+ };
805
+ }
806
+ const localeRaw = await collectDocs(buildSpec.docsDir);
807
+ const locale = await resolveDocImages(localeRaw, buildSpec.docsDir);
808
+ const merged = overlayDocs(base, locale);
710
809
  return {
711
810
  sources: srcs,
712
- docs: resolved.docs,
713
- docImageFiles: resolved.files,
714
- inlineSvgs: resolved.inlineSvgs
811
+ docs: merged.docs,
812
+ docImageFiles: merged.files,
813
+ inlineSvgs: merged.inlineSvgs
715
814
  };
716
815
  }
717
816
  );
@@ -720,7 +819,7 @@ async function publish(data, opts, tutorials) {
720
819
  const sectionOrder = normalizeSectionOrder(opts.sectionOrder);
721
820
  const menu = normalizeMenu(opts.menu);
722
821
  const clubSidebarItems = opts.clubSidebarItems === true;
723
- const manifest = await progress.stage("Generating pages", () => generateSite(data, {
822
+ const siteOptions = {
724
823
  ...pkg ? { pkg } : {},
725
824
  ...readme ? { readme } : {},
726
825
  ...tutorialTree.length > 0 ? { tutorials: tutorialTree } : {},
@@ -732,7 +831,23 @@ async function publish(data, opts, tutorials) {
732
831
  ...sectionOrder ? { sectionOrder } : {},
733
832
  ...menu ? { menu } : {},
734
833
  ...clubSidebarItems ? { clubSidebarItems } : {}
735
- }));
834
+ };
835
+ const manifest = await progress.stage(
836
+ "Generating pages",
837
+ () => buildSpec ? stampSite(data, buildSpec.apiMessages, siteOptions) : generateSite(data, siteOptions)
838
+ );
839
+ const extractPath = process.env.CLEAN_JSDOC_THEME_EXTRACT?.trim();
840
+ if (extractPath) {
841
+ const target = resolvePath(extractPath);
842
+ await progress.stage("Writing extract manifest", async () => {
843
+ await mkdir2(dirname2(target), { recursive: true });
844
+ await writeFile2(target, JSON.stringify(toExtractManifest(manifest), null, 2) + "\n", "utf8");
845
+ });
846
+ console.log(
847
+ `clean-jsdoc-theme: extract mode \u2014 wrote ${manifest.slots?.length ?? 0} slot(s) to ${target}`
848
+ );
849
+ return;
850
+ }
736
851
  const { siteName, files: logoFiles } = await prepareSiteName(value.siteName, hrefForServed);
737
852
  const notGoogle = new Set(
738
853
  diagnostics.list.filter((d) => d.code === "fonts/not-google" && d.path).map((d) => d.path)
@@ -740,23 +855,48 @@ async function publish(data, opts, tutorials) {
740
855
  const fonts = { ...value.fonts };
741
856
  if (notGoogle.has("fonts.heading")) delete fonts.heading;
742
857
  if (notGoogle.has("fonts.body")) delete fonts.body;
858
+ if (fonts.locales) {
859
+ const cleaned = {};
860
+ for (const [loc, set] of Object.entries(fonts.locales)) {
861
+ const s = { ...set };
862
+ if (notGoogle.has(`fonts.${loc}:heading`)) delete s.heading;
863
+ if (notGoogle.has(`fonts.${loc}:body`)) delete s.body;
864
+ cleaned[loc] = s;
865
+ }
866
+ fonts.locales = cleaned;
867
+ }
743
868
  const customAssets = await resolveCustomAssets(opts, hrefForServed);
869
+ const footer = await resolveFooter(opts.footer);
870
+ const meta = normalizeMeta(opts.meta);
744
871
  const absoluteDestination = resolvePath(destination);
745
872
  const islandCacheDir = resolvePath(process.cwd(), "node_modules", ".cache", "clean-jsdoc-theme");
746
873
  const result = await progress.stage(
747
874
  "Rendering site",
748
875
  () => render(manifest, {
749
- theme: { ...resolveTheme(opts, siteName, fonts, basePath), ...customAssets.theme },
876
+ theme: {
877
+ ...resolveTheme(opts, siteName, fonts, basePath, buildSpec?.locale),
878
+ ...customAssets.theme,
879
+ ...footer ? { footer } : {},
880
+ ...meta ? { meta } : {}
881
+ },
750
882
  destination: absoluteDestination,
751
883
  islandCacheDir,
752
- inlineSvgs
884
+ inlineSvgs,
885
+ // Build mode: render chrome in the locale (SSR provider + island seeding)
886
+ // and, with >1 locale, mount the language switcher.
887
+ ...buildSpec ? {
888
+ locale: {
889
+ code: buildSpec.locale,
890
+ defaultLocale: buildSpec.defaultLocale,
891
+ messages: buildSpec.chromeMessages,
892
+ siteBasePath: buildSpec.siteBasePath,
893
+ locales: buildSpec.locales.map((l) => ({ code: l.code, label: l.name ?? l.code }))
894
+ }
895
+ } : {}
753
896
  })
754
897
  );
755
898
  const outputFiles = [...result.files, ...logoFiles, ...customAssets.files, ...docImageFiles];
756
- await progress.stage(
757
- "Writing files",
758
- () => writeOutputFiles(absoluteDestination, outputFiles)
759
- );
899
+ await progress.stage("Writing files", () => writeOutputFiles(absoluteDestination, outputFiles));
760
900
  const zlib = await import("zlib");
761
901
  const gzipSizer = (b) => zlib.gzipSync(b).length;
762
902
  console.log(
@@ -782,9 +922,7 @@ async function publish(data, opts, tutorials) {
782
922
  () => runPagefindAgainstDir(absoluteDestination)
783
923
  );
784
924
  } catch (err) {
785
- console.warn(
786
- `clean-jsdoc-theme: pagefind step skipped (optional) \u2014 ${err.message}`
787
- );
925
+ console.warn(`clean-jsdoc-theme: pagefind step skipped (optional) \u2014 ${err.message}`);
788
926
  }
789
927
  }
790
928
  export {
@@ -795,11 +933,14 @@ export {
795
933
  normalizeCopyPage,
796
934
  normalizeDocGroups,
797
935
  normalizeMenu,
936
+ normalizeMeta,
798
937
  normalizePageNav,
799
938
  normalizeSectionOrder,
800
939
  outputSourceFilesEnabled,
940
+ overlayDocs,
801
941
  publish,
802
942
  resolveDocImages,
943
+ resolveFooter,
803
944
  sourceLinkToCommentEnabled
804
945
  };
805
946
  //# sourceMappingURL=publish.mjs.map