@writedocs/generator 0.10.3 → 0.10.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@writedocs/generator",
3
- "version": "0.10.3",
3
+ "version": "0.10.4",
4
4
  "description": "Static site generator for docs — a writedocs.json + MDX folder in, a static site out.",
5
5
  "type": "module",
6
6
  "bin": {
package/src/lib/config.ts CHANGED
@@ -2,7 +2,7 @@ import fs from 'node:fs';
2
2
  import path from 'node:path';
3
3
  import { EXCLUDED_TOP_LEVEL_DIRS, navPageId } from './pages.js';
4
4
  import { isHidden } from './hidden-sections.js';
5
- import { itemPage, childLevel, resolveLandings as resolveLandingsJs } from './item-pages.js';
5
+ import { itemPage, childLevel, equivalentItem, resolveLandings as resolveLandingsJs } from './item-pages.js';
6
6
  import matter from 'gray-matter';
7
7
  import { writedocsTempDir } from './writedocs-temp-dir.js';
8
8
  import { readableTextOn } from './color.js';
@@ -785,7 +785,7 @@ export function resolveLandings(navigation: NavigationConfig): Landing[] {
785
785
  * chosen - the switcher shows the item's name until the reader picks one
786
786
  * (`placeholder`, currentOption() in lib/selector-placement.js). */
787
787
  export function landingSelectors(landing: Landing, hrefForSlug: (slug: string) => string): Selector[] {
788
- const selectors = buildSelectors(landing.path, hrefForSlug);
788
+ const selectors = buildSelectors(landing.path, hrefForSlug, -1, true);
789
789
  const level = childLevel(landing.item) as { kind: PathSegment['kind']; items: NamedContainer[] } | null;
790
790
  if (!level) return selectors;
791
791
  selectors.push({
@@ -1066,11 +1066,15 @@ function dropdownMenuOf(
1066
1066
  export function buildSelectors(
1067
1067
  path: PathSegment[],
1068
1068
  hrefForSlug: (slug: string) => string,
1069
- activePagePosition: number = -1
1069
+ activePagePosition: number = -1,
1070
+ // The page is an item's own page (landingSelectors()): a version or
1071
+ // language switch goes to the same item in the other option - where
1072
+ // choosing that item goes, its own page or its first page.
1073
+ landing = false
1070
1074
  ): Selector[] {
1071
1075
  return path.map((segment, segIndex): Selector => {
1072
1076
  const preservesPosition =
1073
- (segment.kind === 'version' || segment.kind === 'language') && activePagePosition >= 0;
1077
+ (segment.kind === 'version' || segment.kind === 'language') && (activePagePosition >= 0 || landing);
1074
1078
  const remainingPath = path.slice(segIndex + 1);
1075
1079
  return {
1076
1080
  kind: segment.kind,
@@ -1079,9 +1083,12 @@ export function buildSelectors(
1079
1083
  // A hidden item is never offered - not even to its own pages.
1080
1084
  if (isHidden(item)) return [];
1081
1085
  const isActive = i === segment.index;
1082
- const equivalentSlug = preservesPosition
1083
- ? equivalentPageIn(item as Container, remainingPath, activePagePosition)
1084
- : null;
1086
+ const sameItem = preservesPosition && landing ? equivalentItem(item, remainingPath) : null;
1087
+ const equivalentSlug = !preservesPosition
1088
+ ? null
1089
+ : landing
1090
+ ? sameItem && firstSlugOf(sameItem as Container)
1091
+ : equivalentPageIn(item as Container, remainingPath, activePagePosition);
1085
1092
  const targetSlug = equivalentSlug ?? firstSlugOf(item as Container) ?? 'index';
1086
1093
  return {
1087
1094
  label: labelOf(item),
@@ -69,3 +69,21 @@ export function resolveLandings(navigation) {
69
69
  if (navigation && !Array.isArray(navigation)) walk(navigation, []);
70
70
  return out;
71
71
  }
72
+
73
+ /** The item in `from` at the same place as the item an item's own page
74
+ * belongs to - for a version or language switcher on that page.
75
+ * `remainingPath` is the levels below the one being switched, down to the
76
+ * page's item (`{ kind, index }` segments); empty when the item is the
77
+ * version or language itself. Null when `from` isn't built the same way
78
+ * that far down - the switcher then falls back to the option's first page,
79
+ * as it does for any page. */
80
+ export function equivalentItem(from, remainingPath) {
81
+ let node = from;
82
+ for (const segment of remainingPath) {
83
+ const level = childLevel(node);
84
+ if (!level || level.kind !== segment.kind) return null;
85
+ node = level.items[segment.index];
86
+ if (!node || typeof node !== 'object' || 'href' in node) return null;
87
+ }
88
+ return node;
89
+ }