@astrojs/starlight 0.10.0 → 0.10.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # @astrojs/starlight
2
2
 
3
+ ## 0.10.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#726](https://github.com/withastro/starlight/pull/726) [`f3157c6`](https://github.com/withastro/starlight/commit/f3157c6065943af39995b6dbae5f63cf424bd9a3) Thanks [@delucis](https://github.com/delucis)! - Fix a rare bug in table of contents when handling headings that increase by more than one level on a page.
8
+
9
+ - [#729](https://github.com/withastro/starlight/pull/729) [`80c6ab1`](https://github.com/withastro/starlight/commit/80c6ab1c1ec48805e74c53b615a78d65127eeacb) Thanks [@delucis](https://github.com/delucis)! - Upgrade Pagefind to v1.0.3
10
+
11
+ - [#715](https://github.com/withastro/starlight/pull/715) [`e726155`](https://github.com/withastro/starlight/commit/e7261559f2539a0ceefd36a28e4fbbc17f5970b8) Thanks [@itsmatteomanf](https://github.com/itsmatteomanf)! - feat: prevent scroll on body when search is open
12
+
3
13
  ## 0.10.0
4
14
 
5
15
  ### Minor Changes
@@ -72,20 +72,23 @@ const pagefindTranslations = {
72
72
 
73
73
  const openModal = (event?: MouseEvent) => {
74
74
  dialog.showModal();
75
+ document.body.toggleAttribute('data-search-modal-open', true);
75
76
  this.querySelector('input')?.focus();
76
77
  event?.stopPropagation();
77
78
  window.addEventListener('click', onClick);
78
79
  };
79
80
 
80
- const closeModal = () => {
81
- dialog.close();
82
- window.removeEventListener('click', onClick);
83
- };
81
+ const closeModal = () => dialog.close();
84
82
 
85
83
  openBtn.addEventListener('click', openModal);
86
84
  openBtn.disabled = false;
87
85
  closeBtn.addEventListener('click', closeModal);
88
86
 
87
+ dialog.addEventListener('close', () => {
88
+ document.body.toggleAttribute('data-search-modal-open', false);
89
+ window.removeEventListener('click', onClick);
90
+ });
91
+
89
92
  // Listen for `/` and `cmd + k` keyboard shortcuts.
90
93
  window.addEventListener('keydown', (e) => {
91
94
  const isInput =
@@ -230,6 +233,10 @@ const pagefindTranslations = {
230
233
  </style>
231
234
 
232
235
  <style is:global>
236
+ [data-search-modal-open] {
237
+ overflow: hidden;
238
+ }
239
+
233
240
  #starlight__search {
234
241
  --sl-search-result-spacing: calc(1.25rem * var(--pagefind-ui-scale));
235
242
  --sl-search-result-pad-inline-start: calc(3.75rem * var(--pagefind-ui-scale));
@@ -4,52 +4,29 @@ export interface TocItem extends MarkdownHeading {
4
4
  children: TocItem[];
5
5
  }
6
6
 
7
- function diveChildren(item: TocItem, depth: number): TocItem[] {
8
- if (depth === 1) {
9
- return item.children;
10
- } else if (item.children.length > 0) {
11
- return diveChildren(item.children.at(-1)!, depth - 1);
12
- } else {
13
- return [];
14
- }
15
- }
16
-
17
7
  interface TocOpts {
18
8
  minHeadingLevel: number;
19
9
  maxHeadingLevel: number;
20
- title?: string;
10
+ title: string;
21
11
  }
22
12
 
13
+ /** Convert the flat headings array generated by Astro into a nested tree structure. */
23
14
  export function generateToC(
24
15
  headings: MarkdownHeading[],
25
- { minHeadingLevel, maxHeadingLevel, title = 'Overview' }: TocOpts
16
+ { minHeadingLevel, maxHeadingLevel, title }: TocOpts
26
17
  ) {
27
- const overview = { depth: 2, slug: '_top', text: title };
28
- headings = [
29
- overview,
30
- ...headings.filter(({ depth }) => depth >= minHeadingLevel && depth <= maxHeadingLevel),
31
- ];
32
- const toc: Array<TocItem> = [];
18
+ headings = headings.filter(({ depth }) => depth >= minHeadingLevel && depth <= maxHeadingLevel);
19
+ const toc: Array<TocItem> = [{ depth: 2, slug: '_top', text: title, children: [] }];
20
+ for (const heading of headings) injectChild(toc, { ...heading, children: [] });
21
+ return toc;
22
+ }
33
23
 
34
- for (const heading of headings) {
35
- if (toc.length === 0) {
36
- toc.push({ ...heading, children: [] });
37
- } else {
38
- const lastItemInToc = toc.at(-1)!;
39
- if (heading.depth < lastItemInToc.depth) {
40
- throw new Error(`Orphan heading found: ${heading.text}.`);
41
- }
42
- if (heading.depth === lastItemInToc.depth) {
43
- // same depth
44
- toc.push({ ...heading, children: [] });
45
- } else {
46
- // higher depth
47
- // push into children, or children's children alike
48
- const gap = heading.depth - lastItemInToc.depth;
49
- const target = diveChildren(lastItemInToc, gap);
50
- target.push({ ...heading, children: [] });
51
- }
52
- }
24
+ /** Inject a ToC entry as deep in the tree as its `depth` property requires. */
25
+ function injectChild(items: TocItem[], item: TocItem): void {
26
+ const lastItem = items.at(-1);
27
+ if (!lastItem || lastItem.depth >= item.depth) {
28
+ items.push(item);
29
+ } else {
30
+ return injectChild(lastItem.children, item);
53
31
  }
54
- return toc;
55
32
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/starlight",
3
- "version": "0.10.0",
3
+ "version": "0.10.1",
4
4
  "description": "Build beautiful, high-performance documentation websites with Astro",
5
5
  "keywords": [
6
6
  "docs",
@@ -39,13 +39,13 @@
39
39
  "dependencies": {
40
40
  "@astrojs/mdx": "^1.0.0",
41
41
  "@astrojs/sitemap": "^3.0.0",
42
- "@pagefind/default-ui": "^1.0.2",
42
+ "@pagefind/default-ui": "^1.0.3",
43
43
  "@types/mdast": "^3.0.11",
44
44
  "bcp-47": "^2.1.0",
45
45
  "execa": "^7.1.1",
46
46
  "hast-util-select": "^5.0.5",
47
47
  "hastscript": "^7.2.0",
48
- "pagefind": "^1.0.2",
48
+ "pagefind": "^1.0.3",
49
49
  "rehype": "^12.0.1",
50
50
  "remark-directive": "^2.0.1",
51
51
  "unified": "^10.1.2",