@brillout/docpress 0.10.19 → 0.10.21

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.
@@ -1,4 +1,5 @@
1
1
  export { resolveHeadingsData }
2
+ export type { ActiveCategory }
2
3
 
3
4
  import { assert, isBrowser, jsxToTextContent } from '../utils/server'
4
5
  import type {
@@ -25,6 +26,12 @@ type PageSectionResolved = {
25
26
  pageSectionLevel: number
26
27
  }
27
28
 
29
+ type ActiveCategory = {
30
+ name: string
31
+ order: number
32
+ hide?: boolean
33
+ }
34
+
28
35
  function resolveHeadingsData(pageContext: PageContextOriginal) {
29
36
  const config = getConfig()
30
37
 
@@ -37,7 +44,7 @@ function resolveHeadingsData(pageContext: PageContextOriginal) {
37
44
  const { headingsDetachedResolved } = resolved
38
45
  let { headingsResolved } = resolved
39
46
 
40
- const { activeHeading, isDetachedPage, activeCategory } = getActiveHeading(
47
+ const { activeHeading, isDetachedPage, activeCategoryName } = getActiveHeading(
41
48
  headingsResolved,
42
49
  headingsDetachedResolved,
43
50
  pageContext,
@@ -72,6 +79,17 @@ function resolveHeadingsData(pageContext: PageContextOriginal) {
72
79
  }
73
80
  }
74
81
 
82
+ const activeCategory: ActiveCategory = config.categories
83
+ // normalize
84
+ ?.map((c, i) => ({
85
+ order: i,
86
+ ...(typeof c === 'string' ? { name: c } : c),
87
+ }))
88
+ .find((c) => c.name === activeCategoryName) ?? {
89
+ name: activeCategoryName,
90
+ order: 99999999999,
91
+ }
92
+
75
93
  const pageContextAddendum = {
76
94
  navItemsAll,
77
95
  navItemsDetached,
@@ -150,7 +168,7 @@ function getActiveHeading(
150
168
  pageContext: { urlPathname: string; exports: Exports },
151
169
  ) {
152
170
  let activeHeading: HeadingResolved | HeadingDetachedResolved | null = null
153
- let activeCategory = 'Miscellaneous'
171
+ let activeCategoryName = 'Miscellaneous'
154
172
  let headingCategory: string | undefined
155
173
  const { urlPathname } = pageContext
156
174
  assert(urlPathname)
@@ -161,7 +179,7 @@ function getActiveHeading(
161
179
  if (heading.url === urlPathname) {
162
180
  activeHeading = heading
163
181
  assert(headingCategory)
164
- activeCategory = headingCategory
182
+ activeCategoryName = headingCategory
165
183
  assert(heading.level === 2, { pageUrl: urlPathname, heading })
166
184
  break
167
185
  }
@@ -183,8 +201,8 @@ function getActiveHeading(
183
201
  ].join('\n'),
184
202
  )
185
203
  }
186
- if (activeHeading.category) activeCategory = activeHeading.category
187
- return { activeHeading, isDetachedPage, activeCategory }
204
+ if (activeHeading.category) activeCategoryName = activeHeading.category
205
+ return { activeHeading, isDetachedPage, activeCategoryName }
188
206
  }
189
207
 
190
208
  function getPageSectionsResolved(
@@ -1,7 +1,13 @@
1
1
  export { resolveHeadingsData };
2
+ export type { ActiveCategory };
2
3
  import type { NavItem } from '../NavItemComponent';
3
4
  import type { LinkData } from '../components';
4
5
  import type { PageContextOriginal } from './resolvePageContext';
6
+ type ActiveCategory = {
7
+ name: string;
8
+ order: number;
9
+ hide?: boolean;
10
+ };
5
11
  declare function resolveHeadingsData(pageContext: PageContextOriginal): {
6
12
  navItemsAll: NavItem[];
7
13
  navItemsDetached: NavItem[] | undefined;
@@ -9,5 +15,5 @@ declare function resolveHeadingsData(pageContext: PageContextOriginal): {
9
15
  isLandingPage: boolean;
10
16
  pageTitle: string | null;
11
17
  documentTitle: string;
12
- activeCategory: string;
18
+ activeCategory: ActiveCategory;
13
19
  };
@@ -26,6 +26,7 @@ import { parseMarkdownMini } from '../parseMarkdownMini';
26
26
  import { determineNavItemsColumnLayout } from '../renderer/determineNavItemsColumnLayout';
27
27
  assert(!isBrowser());
28
28
  function resolveHeadingsData(pageContext) {
29
+ var _a, _b;
29
30
  var config = getConfig();
30
31
  {
31
32
  var headings = config.headings, headingsDetached = config.headingsDetached;
@@ -34,8 +35,8 @@ function resolveHeadingsData(pageContext) {
34
35
  var resolved = getHeadingsResolved(config);
35
36
  var headingsDetachedResolved = resolved.headingsDetachedResolved;
36
37
  var headingsResolved = resolved.headingsResolved;
37
- var _a = getActiveHeading(headingsResolved, headingsDetachedResolved, pageContext), activeHeading = _a.activeHeading, isDetachedPage = _a.isDetachedPage, activeCategory = _a.activeCategory;
38
- var _b = getTitles(activeHeading, pageContext, config), documentTitle = _b.documentTitle, isLandingPage = _b.isLandingPage, pageTitle = _b.pageTitle;
38
+ var _c = getActiveHeading(headingsResolved, headingsDetachedResolved, pageContext), activeHeading = _c.activeHeading, isDetachedPage = _c.isDetachedPage, activeCategoryName = _c.activeCategoryName;
39
+ var _d = getTitles(activeHeading, pageContext, config), documentTitle = _d.documentTitle, isLandingPage = _d.isLandingPage, pageTitle = _d.pageTitle;
39
40
  var pageSectionsResolved = getPageSectionsResolved(pageContext, activeHeading);
40
41
  var linksAll = __spreadArray(__spreadArray(__spreadArray([], pageSectionsResolved.map(pageSectionToLinkData), true), headingsResolved.map(headingToLinkData), true), headingsDetachedResolved.map(headingToLinkData), true);
41
42
  var navItemsAll;
@@ -57,6 +58,10 @@ function resolveHeadingsData(pageContext) {
57
58
  });
58
59
  }
59
60
  }
61
+ var activeCategory = (_b = (_a = config.categories) === null || _a === void 0 ? void 0 : _a.map(function (c, i) { return (__assign({ order: i }, (typeof c === 'string' ? { name: c } : c))); }).find(function (c) { return c.name === activeCategoryName; })) !== null && _b !== void 0 ? _b : {
62
+ name: activeCategoryName,
63
+ order: 99999999999,
64
+ };
60
65
  var pageContextAddendum = {
61
66
  navItemsAll: navItemsAll,
62
67
  navItemsDetached: navItemsDetached,
@@ -121,7 +126,7 @@ function getTitles(activeHeading, pageContext, config) {
121
126
  function getActiveHeading(headingsResolved, headingsDetachedResolved, pageContext) {
122
127
  var _a;
123
128
  var activeHeading = null;
124
- var activeCategory = 'Miscellaneous';
129
+ var activeCategoryName = 'Miscellaneous';
125
130
  var headingCategory;
126
131
  var urlPathname = pageContext.urlPathname;
127
132
  assert(urlPathname);
@@ -133,7 +138,7 @@ function getActiveHeading(headingsResolved, headingsDetachedResolved, pageContex
133
138
  if (heading.url === urlPathname) {
134
139
  activeHeading = heading;
135
140
  assert(headingCategory);
136
- activeCategory = headingCategory;
141
+ activeCategoryName = headingCategory;
137
142
  assert(heading.level === 2, { pageUrl: urlPathname, heading: heading });
138
143
  break;
139
144
  }
@@ -156,8 +161,8 @@ function getActiveHeading(headingsResolved, headingsDetachedResolved, pageContex
156
161
  ].join('\n'));
157
162
  }
158
163
  if (activeHeading.category)
159
- activeCategory = activeHeading.category;
160
- return { activeHeading: activeHeading, isDetachedPage: isDetachedPage, activeCategory: activeCategory };
164
+ activeCategoryName = activeHeading.category;
165
+ return { activeHeading: activeHeading, isDetachedPage: isDetachedPage, activeCategoryName: activeCategoryName };
161
166
  }
162
167
  function getPageSectionsResolved(pageContext, activeHeading) {
163
168
  var _a;
@@ -16,7 +16,7 @@ declare function resolvePageContext(pageContext: PageContextOriginal): {
16
16
  isLandingPage: boolean;
17
17
  pageTitle: string | null;
18
18
  documentTitle: string;
19
- activeCategory: string;
19
+ activeCategory: import("./resolveHeadingsData").ActiveCategory;
20
20
  } & {
21
21
  urlPathname: string;
22
22
  meta: {
@@ -20,6 +20,7 @@ type Config = {
20
20
  };
21
21
  headings: HeadingDefinition[];
22
22
  headingsDetached: HeadingDetachedDefinition[];
23
+ categories?: Category[];
23
24
  /** Sets `<meta name="description" content="${tagline}" />` */
24
25
  tagline: string;
25
26
  websiteUrl: string;
@@ -34,3 +35,9 @@ type Config = {
34
35
  navLogoStyle?: React.CSSProperties;
35
36
  navLogoTextStyle?: React.CSSProperties;
36
37
  };
38
+ /** Order in Algolia search results */
39
+ type Category = string | {
40
+ name: string;
41
+ /** Hide from Algolia search */
42
+ hide?: boolean;
43
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brillout/docpress",
3
- "version": "0.10.19",
3
+ "version": "0.10.21",
4
4
  "type": "module",
5
5
  "dependencies": {
6
6
  "@brillout/picocolors": "^1.0.10",
@@ -6,6 +6,7 @@ import { assert } from '../utils/server'
6
6
  import type { PageContextResolved } from '../config/resolvePageContext'
7
7
  import { getPageElement } from './getPageElement'
8
8
  import type { OnRenderHtmlAsync } from 'vike/types'
9
+ import { ActiveCategory } from '../config/resolveHeadingsData'
9
10
 
10
11
  const onRenderHtml: OnRenderHtmlAsync = async (
11
12
  pageContext,
@@ -32,7 +33,7 @@ Promise<Awaited<ReturnType<OnRenderHtmlAsync>>> => {
32
33
  ${descriptionTag}
33
34
  <meta name="viewport" content="width=device-width,initial-scale=1">
34
35
  ${getOpenGraphTags(pageContext.urlPathname, pageContextResolved.documentTitle, pageContextResolved.meta)}
35
- <meta name="docsearch:category" content="${pageContextResolved.activeCategory}" />
36
+ ${getAlgoliaTags(pageContextResolved.activeCategory)}
36
37
  </head>
37
38
  <body>
38
39
  <div id="page-view">${dangerouslySkipEscape(pageHtml)}</div>
@@ -40,6 +41,15 @@ Promise<Awaited<ReturnType<OnRenderHtmlAsync>>> => {
40
41
  </html>`
41
42
  }
42
43
 
44
+ function getAlgoliaTags(activeCategory: ActiveCategory) {
45
+ const categoryNameTag = escapeInject`<meta name="algolia:category" content="${activeCategory.name}">`
46
+ if (activeCategory.hide) {
47
+ return escapeInject`${categoryNameTag}<meta name="algolia:category:hide"> `
48
+ } else {
49
+ return escapeInject`${categoryNameTag}<meta name="algolia:category:order" content="${activeCategory.order.toString()}"> `
50
+ }
51
+ }
52
+
43
53
  function getOpenGraphTags(
44
54
  url: string,
45
55
  documentTitle: string,
package/types/Config.ts CHANGED
@@ -22,6 +22,7 @@ type Config = {
22
22
  }
23
23
  headings: HeadingDefinition[]
24
24
  headingsDetached: HeadingDetachedDefinition[]
25
+ categories?: Category[]
25
26
  /** Sets `<meta name="description" content="${tagline}" />` */
26
27
  tagline: string
27
28
  websiteUrl: string
@@ -36,3 +37,12 @@ type Config = {
36
37
  navLogoStyle?: React.CSSProperties
37
38
  navLogoTextStyle?: React.CSSProperties
38
39
  }
40
+
41
+ /** Order in Algolia search results */
42
+ type Category =
43
+ | string
44
+ | {
45
+ name: string
46
+ /** Hide from Algolia search */
47
+ hide?: boolean
48
+ }