@writedocs/generator 0.8.1 → 0.9.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.
@@ -72,15 +72,52 @@ export function initSearch(root: ParentNode) {
72
72
  let currentResults: HTMLElement[] = [];
73
73
  let activeIndex = -1;
74
74
 
75
+ // Down on the last result (or Up on the first) stays there - Enter always
76
+ // has a result to open.
75
77
  function setActive(index: number) {
76
78
  currentResults.forEach((el) => el.classList.remove('wd-search-result-active'));
77
- activeIndex = index >= 0 && index < currentResults.length ? index : -1;
79
+ activeIndex = currentResults.length ? Math.min(Math.max(index, 0), currentResults.length - 1) : -1;
78
80
  if (activeIndex >= 0) {
79
81
  currentResults[activeIndex].classList.add('wd-search-result-active');
80
82
  currentResults[activeIndex].scrollIntoView({ block: 'nearest' });
81
83
  }
82
84
  }
83
85
 
86
+ // Search looks in the version, language and product of the page the reader
87
+ // is on (plus pages outside any of them), with a switch to search
88
+ // everything. [...slug].astro marks each page's scope for Pagefind and
89
+ // for this - see lib/search-scope.js.
90
+ let everything = false;
91
+ const pageScope = () => {
92
+ const el = document.querySelector<HTMLElement>('[data-wd-search-scope]');
93
+ const key = el?.dataset.wdSearchScope ?? 'all';
94
+ return key === 'all' ? null : { key, label: el?.dataset.wdSearchScopeLabel || key };
95
+ };
96
+ function scopeNote(scope: { label: string } | null, found: boolean) {
97
+ if (!scope) return null;
98
+ const note = document.createElement('div');
99
+ note.className = 'wd-search-scope';
100
+ const text = document.createElement('span');
101
+ const button = document.createElement('button');
102
+ button.type = 'button';
103
+ button.className = 'wd-search-scope-toggle';
104
+ if (everything) {
105
+ text.textContent = 'Showing results from the whole site.';
106
+ button.textContent = `Only ${scope.label}`;
107
+ } else {
108
+ text.textContent = found ? `Showing results in ${scope.label}.` : `No results in ${scope.label}.`;
109
+ button.textContent = 'Search everything';
110
+ }
111
+ button.addEventListener('click', (e) => {
112
+ e.stopPropagation();
113
+ everything = !everything;
114
+ input.focus();
115
+ runSearch(input.value);
116
+ });
117
+ note.append(text, ' ', button);
118
+ return note;
119
+ }
120
+
84
121
  async function runSearch(term: string) {
85
122
  if (!term.trim()) {
86
123
  resultsEl.innerHTML = '<div class="wd-search-hint">Start typing to search...</div>';
@@ -94,10 +131,14 @@ export function initSearch(root: ParentNode) {
94
131
  // superseded this call - pagefind's own debounce, on top of (not
95
132
  // instead of) the plain `input` listener below, so a fast typist never
96
133
  // races two searches rendering out of order.
97
- const search = await pf.debouncedSearch(term, {}, 150);
134
+ const scope = pageScope();
135
+ const options = scope && !everything ? { filters: { any: [{ scope: scope.key }, { scope: 'all' }] } } : {};
136
+ const search = await pf.debouncedSearch(term, options, 150);
98
137
  if (search === null) return;
99
138
  if (search.results.length === 0) {
100
139
  resultsEl.innerHTML = '<div class="wd-search-empty">No results.</div>';
140
+ const note = scopeNote(scope, false);
141
+ if (note) resultsEl.replaceChildren(note);
101
142
  currentResults = [];
102
143
  activeIndex = -1;
103
144
  return;
@@ -111,15 +152,27 @@ export function initSearch(root: ParentNode) {
111
152
  const title = document.createElement('div');
112
153
  title.className = 'wd-search-result-title';
113
154
  title.textContent = data.meta?.title ?? data.url;
155
+ // Where the page sits: "API Reference › Webhooks".
156
+ const section = data.meta?.section ? document.createElement('div') : null;
157
+ if (section) {
158
+ section.className = 'wd-search-result-section';
159
+ section.textContent = data.meta.section;
160
+ }
114
161
  const excerpt = document.createElement('div');
115
162
  excerpt.className = 'wd-search-result-excerpt';
116
163
  // innerHTML (not textContent) is deliberate: pagefind returns this
117
164
  // pre-sanitized with <mark> wrapping the matched terms, which is the
118
165
  // whole point - a plain-text excerpt couldn't highlight anything.
119
- excerpt.innerHTML = data.excerpt;
120
- a.append(title, excerpt);
166
+ // The section is indexed as the page's last words, so a short page's
167
+ // excerpt ends with it - shown once, on its own line, unless it's what
168
+ // matched (then the excerpt keeps it, highlighted).
169
+ const trailing = data.meta?.section ? ` ${data.meta.section}.` : null;
170
+ excerpt.innerHTML = trailing && data.excerpt.endsWith(trailing) ? data.excerpt.slice(0, -trailing.length) : data.excerpt;
171
+ a.append(title, ...(section ? [section] : []), excerpt);
121
172
  resultsEl.appendChild(a);
122
173
  }
174
+ const note = scopeNote(scope, true);
175
+ if (note) resultsEl.appendChild(note);
123
176
  currentResults = Array.from(resultsEl.querySelectorAll('.wd-search-result'));
124
177
  setActive(0);
125
178
  }
@@ -128,6 +181,7 @@ export function initSearch(root: ParentNode) {
128
181
  overlay.hidden = false;
129
182
  document.body.style.overflow = 'hidden';
130
183
  input.value = '';
184
+ everything = false;
131
185
  resultsEl.innerHTML = '<div class="wd-search-hint">Start typing to search...</div>';
132
186
  currentResults = [];
133
187
  activeIndex = -1;
@@ -0,0 +1,169 @@
1
+ // A tab row with more tabs than fit slides sideways instead of wrapping:
2
+ // an arrow shows on each side that has more, the active tab starts in
3
+ // view, and keyboard focus, a horizontal wheel/trackpad swipe and a touch
4
+ // swipe move the row too. TopBar.astro renders the markup:
5
+ // [data-tabs-strip] > arrow, .wd-tabs-viewport > .wd-topbar-tabs, arrow.
6
+ //
7
+ // The row is moved with a transform inside an `overflow-x: clip` viewport,
8
+ // not scrolled: a scroll container clips both directions, and would cut
9
+ // off the menus of the tabs and global dropdowns that open below the row.
10
+ // `clip` on one axis leaves the other visible. Browsers without it keep
11
+ // the row wrapping (topbar.css), and this does nothing.
12
+ import { maxOffset, clampOffset, stepOffset, revealOffset } from '../lib/tabs-strip-offset.js';
13
+
14
+ export function initTabsStrips(root: ParentNode) {
15
+ if (!CSS.supports('overflow-x', 'clip')) return;
16
+ root.querySelectorAll<HTMLElement>('[data-tabs-strip]').forEach(initStrip);
17
+ }
18
+
19
+ function initStrip(strip: HTMLElement) {
20
+ if (strip.dataset.wdInit) return;
21
+ strip.dataset.wdInit = 'true';
22
+ const viewport = strip.querySelector<HTMLElement>('.wd-tabs-viewport');
23
+ const track = strip.querySelector<HTMLElement>('.wd-topbar-tabs');
24
+ const prev = strip.querySelector<HTMLElement>('[data-tabs-prev]');
25
+ const next = strip.querySelector<HTMLElement>('[data-tabs-next]');
26
+ if (!viewport || !track || !prev || !next) return;
27
+
28
+ let offset = 0;
29
+ const geometry = () => {
30
+ const view = viewport.clientWidth;
31
+ return { view, max: maxOffset(track.scrollWidth, view), edge: prev.offsetWidth || 36 };
32
+ };
33
+ const apply = () => {
34
+ const { max } = geometry();
35
+ offset = clampOffset(offset, max);
36
+ track.style.transform = offset ? `translateX(${-offset}px)` : '';
37
+ strip.classList.toggle('wd-can-prev', offset > 0);
38
+ strip.classList.toggle('wd-can-next', offset < max);
39
+ };
40
+ // The tab (or dropdown) a node belongs to - a direct child of the row.
41
+ const itemOf = (node: Element | null) => {
42
+ while (node && node.parentElement !== track) node = node.parentElement;
43
+ return node as HTMLElement | null;
44
+ };
45
+ const reveal = (item: HTMLElement | null) => {
46
+ if (!item) return;
47
+ offset = revealOffset(offset, item.offsetLeft, item.offsetLeft + item.offsetWidth, geometry());
48
+ apply();
49
+ };
50
+
51
+ // Where the page starts: the active tab in view, without sliding to it.
52
+ // Tabs widen when the web font arrives, after this first placement - until
53
+ // the reader moves the row, each resize puts the active tab back in view.
54
+ let readerMoved = false;
55
+ const activeItem = itemOf(track.querySelector('.wd-tab-link.active'));
56
+ const settle = () => {
57
+ strip.classList.remove('wd-tabs-animate');
58
+ reveal(activeItem);
59
+ requestAnimationFrame(() => strip.classList.add('wd-tabs-animate'));
60
+ };
61
+ settle();
62
+
63
+ prev.addEventListener('click', (e) => {
64
+ e.stopPropagation();
65
+ readerMoved = true;
66
+ offset = stepOffset(offset, -1, geometry());
67
+ apply();
68
+ });
69
+ next.addEventListener('click', (e) => {
70
+ e.stopPropagation();
71
+ readerMoved = true;
72
+ offset = stepOffset(offset, 1, geometry());
73
+ apply();
74
+ });
75
+ track.addEventListener('focusin', (e) => reveal(itemOf(e.target as Element)));
76
+
77
+ // A sideways swipe on a trackpad, or Shift + wheel. A plain vertical
78
+ // wheel still scrolls the page.
79
+ viewport.addEventListener(
80
+ 'wheel',
81
+ (e) => {
82
+ const delta = Math.abs(e.deltaX) > Math.abs(e.deltaY) ? e.deltaX : e.shiftKey ? e.deltaY : 0;
83
+ if (!delta || geometry().max === 0) return;
84
+ e.preventDefault();
85
+ readerMoved = true;
86
+ strip.classList.remove('wd-tabs-animate');
87
+ offset += delta;
88
+ apply();
89
+ requestAnimationFrame(() => strip.classList.add('wd-tabs-animate'));
90
+ },
91
+ { passive: false }
92
+ );
93
+
94
+ // Touch: a sideways swipe drags the row, and a quick one carries on a
95
+ // little after the finger lifts. `touch-action: pan-y` (topbar.css) leaves
96
+ // sideways moves to this and up/down ones to the page, which scrolls as
97
+ // usual. A swipe that ends on a tab doesn't open it.
98
+ let drag: { id: number; x: number; from: number; moving: boolean; last: [number, number][] } | null = null;
99
+ let swallowClick = false;
100
+ viewport.addEventListener('pointerdown', (e) => {
101
+ if (e.pointerType !== 'touch' || geometry().max === 0) return;
102
+ drag = { id: e.pointerId, x: e.clientX, from: offset, moving: false, last: [[e.clientX, e.timeStamp]] };
103
+ });
104
+ viewport.addEventListener('pointermove', (e) => {
105
+ if (!drag || e.pointerId !== drag.id) return;
106
+ const dx = e.clientX - drag.x;
107
+ if (!drag.moving) {
108
+ if (Math.abs(dx) < 8) return;
109
+ drag.moving = true;
110
+ readerMoved = true;
111
+ strip.classList.remove('wd-tabs-animate');
112
+ viewport.setPointerCapture(e.pointerId);
113
+ }
114
+ drag.last = [...drag.last.slice(-1), [e.clientX, e.timeStamp]];
115
+ offset = drag.from - dx;
116
+ apply();
117
+ });
118
+ const endDrag = (e: PointerEvent) => {
119
+ if (!drag || e.pointerId !== drag.id) return;
120
+ if (drag.moving && e.type === 'pointerup') {
121
+ const [[x0, t0], [x1, t1]] = drag.last.length > 1 ? drag.last : [drag.last[0], drag.last[0]];
122
+ const velocity = t1 > t0 ? (x1 - x0) / (t1 - t0) : 0; // px per ms
123
+ strip.classList.add('wd-tabs-animate');
124
+ offset -= velocity * 180;
125
+ apply();
126
+ swallowClick = true;
127
+ setTimeout(() => (swallowClick = false), 400);
128
+ } else {
129
+ requestAnimationFrame(() => strip.classList.add('wd-tabs-animate'));
130
+ }
131
+ drag = null;
132
+ };
133
+ viewport.addEventListener('pointerup', endDrag);
134
+ viewport.addEventListener('pointercancel', endDrag);
135
+ viewport.addEventListener(
136
+ 'click',
137
+ (e) => {
138
+ if (!swallowClick) return;
139
+ e.preventDefault();
140
+ e.stopPropagation();
141
+ swallowClick = false;
142
+ },
143
+ true
144
+ );
145
+
146
+ // A menu that opens under a tab near the right end would be cut off
147
+ // there - it opens to the left of its tab instead.
148
+ track.querySelectorAll<HTMLElement>('.wd-dropdown').forEach((dropdown) => {
149
+ const menu = dropdown.querySelector<HTMLElement>('.wd-dropdown-menu');
150
+ if (!menu) return;
151
+ const place = () => {
152
+ const shown = menu.style.display;
153
+ menu.style.visibility = 'hidden';
154
+ menu.style.display = 'block';
155
+ const width = menu.offsetWidth;
156
+ menu.style.display = shown;
157
+ menu.style.visibility = '';
158
+ const right = dropdown.getBoundingClientRect().left + width;
159
+ dropdown.classList.toggle('wd-dropdown-align-end', right > viewport.getBoundingClientRect().right);
160
+ };
161
+ dropdown.addEventListener('pointerenter', place);
162
+ dropdown.addEventListener('focusin', place);
163
+ });
164
+
165
+ // Tabs change width as fonts load, and the space changes with the window.
166
+ const resized = () => (readerMoved ? apply() : settle());
167
+ new ResizeObserver(resized).observe(viewport);
168
+ new ResizeObserver(resized).observe(track);
169
+ }
@@ -760,7 +760,10 @@
760
760
  "view",
761
761
  "chatgpt",
762
762
  "claude",
763
- "perplexity"
763
+ "perplexity",
764
+ "mcp",
765
+ "cursor",
766
+ "vscode"
764
767
  ]
765
768
  }
766
769
  },
@@ -784,8 +787,8 @@
784
787
  "additionalProperties": false
785
788
  }
786
789
  ],
787
- "description": "The \"Copy page\" menu on every page - copy as Markdown, view as Markdown, open in ChatGPT, Claude or Perplexity - and a Markdown copy of each page at its address + \".md\". On by default with every option. A list picks the options (\"copy\", \"view\", \"chatgpt\", \"claude\", \"perplexity\"); false turns it all off.",
788
- "markdownDescription": "The \"Copy page\" menu on every page - copy as Markdown, view as Markdown, open in ChatGPT, Claude or Perplexity - and a Markdown copy of each page at its address + \".md\". On by default with every option. A list picks the options (\"copy\", \"view\", \"chatgpt\", \"claude\", \"perplexity\"); false turns it all off."
790
+ "description": "The \"Copy page\" menu on every page - copy as Markdown, view as Markdown, open in ChatGPT, Claude or Perplexity, copy the MCP server URL, connect to Cursor or VS Code - and a Markdown copy of each page at its address + \".md\". On by default with every option (the MCP ones only while `mcp` is on). A list picks the options (\"copy\", \"view\", \"chatgpt\", \"claude\", \"perplexity\", \"mcp\", \"cursor\", \"vscode\"); false turns it all off.",
791
+ "markdownDescription": "The \"Copy page\" menu on every page - copy as Markdown, view as Markdown, open in ChatGPT, Claude or Perplexity, copy the MCP server URL, connect to Cursor or VS Code - and a Markdown copy of each page at its address + \".md\". On by default with every option (the MCP ones only while `mcp` is on). A list picks the options (\"copy\", \"view\", \"chatgpt\", \"claude\", \"perplexity\", \"mcp\", \"cursor\", \"vscode\"); false turns it all off."
789
792
  },
790
793
  "mcp": {
791
794
  "default": true,