@brillout/docpress 0.12.1 → 0.12.3

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,16 +1,18 @@
1
1
  export { Hit }
2
2
 
3
3
  import React from 'react'
4
- import { DocSearch } from '@docsearch/react'
5
- import type { InternalDocSearchHit } from './types'
4
+ import type { DocSearchHit, InternalDocSearchHit, StoredDocSearchHit } from '@docsearch/react'
6
5
  import { Snippet } from './Snippet'
7
6
  import { SourceIcon } from './SourceIcon'
8
7
  import { SelectIcon } from './SelectIcon'
9
8
 
10
- type HitProps = Parameters<typeof DocSearch>[0]['hitComponent']
11
- type ContentType = 'lvl0' | 'lvl1' | 'lvl2' | 'lvl3' | 'lvl4' | 'lvl5' | 'lvl6'
9
+ type HitProps = {
10
+ hit: InternalDocSearchHit | StoredDocSearchHit
11
+ children: React.ReactNode
12
+ }
13
+ type HierarchyType = Exclude<DocSearchHit['type'], 'content'>
12
14
 
13
- const Hit: HitProps = ({ hit }) => {
15
+ function Hit({ hit }: HitProps) {
14
16
  return (
15
17
  <a href={hit.type === 'lvl1' ? hit.url.split('#')[0] : hit.url}>
16
18
  <div className="DocSearch-Hit-Container">
@@ -29,14 +31,14 @@ const Hit: HitProps = ({ hit }) => {
29
31
  <div className="DocSearch-Hit-icon">
30
32
  <SourceIcon type={hit.type} />
31
33
  </div>
32
- {hit.hierarchy[hit.type as ContentType] && hit.type === 'lvl1' && (
34
+ {hit.hierarchy[hit.type as HierarchyType] && hit.type === 'lvl1' && (
33
35
  <div className="DocSearch-Hit-content-wrapper">
34
36
  <Snippet className="DocSearch-Hit-title" hit={hit} attribute="hierarchy.lvl1" />
35
37
  {hit.content && <Snippet className="DocSearch-Hit-path" hit={hit} attribute="content" />}
36
38
  </div>
37
39
  )}
38
40
 
39
- {hit.hierarchy[hit.type as ContentType] &&
41
+ {hit.hierarchy[hit.type as HierarchyType] &&
40
42
  (hit.type === 'lvl2' ||
41
43
  hit.type === 'lvl3' ||
42
44
  hit.type === 'lvl4' ||
@@ -1,5 +1,5 @@
1
1
  import { createElement } from 'react'
2
- import { StoredDocSearchHit } from './types'
2
+ import { InternalDocSearchHit, StoredDocSearchHit } from '@docsearch/react'
3
3
 
4
4
  function getPropertyByPath(object: Record<string, any>, path: string): any {
5
5
  const parts = path.split('.')
@@ -17,7 +17,7 @@ interface SnippetProps<TItem> {
17
17
  [prop: string]: unknown
18
18
  }
19
19
 
20
- export function Snippet<TItem extends StoredDocSearchHit>({
20
+ export function Snippet<TItem extends InternalDocSearchHit | StoredDocSearchHit>({
21
21
  hit,
22
22
  attribute,
23
23
  tagName = 'span',
@@ -26,7 +26,7 @@ export function Snippet<TItem extends StoredDocSearchHit>({
26
26
  let title = ''
27
27
  let lvl2 = ''
28
28
 
29
- if (!hit.__docsearch_parent && hit.type !== 'lvl1' && attribute !== 'content') {
29
+ if (!(hit as InternalDocSearchHit).__docsearch_parent && hit.type !== 'lvl1' && attribute !== 'content') {
30
30
  if (hit.type === 'content') {
31
31
  lvl2 = getPropertyByPath(hit, `_snippetResult.hierarchy.lvl2.value`) || getPropertyByPath(hit, 'hierarchy.lvl2')
32
32
  lvl2 = lvl2 ? ` > ${lvl2}` : ''
@@ -82,7 +82,7 @@ function getLinkTextData(href: string, pageContext: PageContextResolved, doNotIn
82
82
  const { hrefPathname, hrefHash } = parseHref(href)
83
83
 
84
84
  const linkData = findLinkData(hrefPathname || pageContext.urlPathname, pageContext)
85
- assert(linkData)
85
+ if (!linkData) return null
86
86
  const isLinkOnSamePage = linkData.url === pageContext.urlPathname
87
87
  if (!hrefPathname) assert(isLinkOnSamePage)
88
88
 
@@ -54,7 +54,8 @@ function getLinkText(_a) {
54
54
  function getLinkTextData(href, pageContext, doNotInferSectionTitle) {
55
55
  var _a = parseHref(href), hrefPathname = _a.hrefPathname, hrefHash = _a.hrefHash;
56
56
  var linkData = findLinkData(hrefPathname || pageContext.urlPathname, pageContext);
57
- assert(linkData);
57
+ if (!linkData)
58
+ return null;
58
59
  var isLinkOnSamePage = linkData.url === pageContext.urlPathname;
59
60
  if (!hrefPathname)
60
61
  assert(isLinkOnSamePage);
@@ -3,4 +3,7 @@ export { assertUsage };
3
3
  export { assertWarning };
4
4
  declare function assert(condition: unknown, debugInfo?: unknown): asserts condition;
5
5
  declare function assertUsage(condition: unknown, msg: string): asserts condition;
6
- declare function assertWarning(condition: unknown, msg: string): void;
6
+ declare function assertWarning(condition: unknown, msg: string, { onlyOnce, showStackTrace }?: {
7
+ onlyOnce?: boolean | string;
8
+ showStackTrace?: true;
9
+ }): void;
@@ -1,7 +1,11 @@
1
1
  export { assert };
2
2
  export { assertUsage };
3
3
  export { assertWarning };
4
+ import { getGlobalObject } from './getGlobalObject';
4
5
  var devModeKey = '__docpress_dev_mode';
6
+ var globalObject = getGlobalObject('utils/assert.ts', {
7
+ alreadyLogged: new Set(),
8
+ });
5
9
  if (isBrowser()) {
6
10
  ;
7
11
  window.toggleDevMode = toggleDevMode;
@@ -66,18 +70,31 @@ function toggleDevMode() {
66
70
  }
67
71
  console.log("DEV MODE ".concat(isEnabled() ? 'enabled' : 'disabled'));
68
72
  }
69
- function assertWarning(condition, msg) {
73
+ function assertWarning(condition, msg, _a) {
74
+ var _b = _a === void 0 ? {} : _a, _c = _b.onlyOnce, onlyOnce = _c === void 0 ? true : _c, showStackTrace = _b.showStackTrace;
70
75
  if (condition) {
71
76
  return;
72
77
  }
73
- msg = '[DocPress][Warning] ' + msg;
74
78
  var err = new Error(msg);
75
- if (import.meta.env.DEV) {
76
- console.warn(err);
77
- if (isDevMode())
78
- window.alert(err);
79
+ if (!import.meta.env.DEV) {
80
+ throw err;
79
81
  }
80
82
  else {
81
- throw err;
83
+ if (onlyOnce) {
84
+ var alreadyLogged = globalObject.alreadyLogged;
85
+ var key = onlyOnce === true ? msg : onlyOnce;
86
+ if (alreadyLogged.has(key))
87
+ return;
88
+ alreadyLogged.add(key);
89
+ }
90
+ msg = '[DocPress][Warning] ' + msg;
91
+ if (!showStackTrace) {
92
+ console.warn(msg);
93
+ }
94
+ else {
95
+ console.warn(err);
96
+ }
97
+ if (isDevMode())
98
+ window.alert(err);
82
99
  }
83
100
  }
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@brillout/docpress",
3
- "version": "0.12.1",
3
+ "version": "0.12.3",
4
4
  "type": "module",
5
5
  "dependencies": {
6
6
  "@brillout/picocolors": "^1.0.10",
7
- "@docsearch/css": "3.6.1",
8
- "@docsearch/react": "3.6.1",
7
+ "@docsearch/css": "3.9.0",
8
+ "@docsearch/react": "3.9.0",
9
9
  "@mdx-js/mdx": "3.0.1",
10
10
  "@mdx-js/react": "3.0.1",
11
11
  "@mdx-js/rollup": "3.0.1",
package/utils/assert.ts CHANGED
@@ -2,7 +2,11 @@ export { assert }
2
2
  export { assertUsage }
3
3
  export { assertWarning }
4
4
 
5
+ import { getGlobalObject } from './getGlobalObject'
5
6
  const devModeKey = '__docpress_dev_mode'
7
+ const globalObject = getGlobalObject('utils/assert.ts', {
8
+ alreadyLogged: new Set<string>(),
9
+ })
6
10
 
7
11
  if (isBrowser()) {
8
12
  ;(window as any).toggleDevMode = toggleDevMode
@@ -70,16 +74,30 @@ function toggleDevMode() {
70
74
  console.log(`DEV MODE ${isEnabled() ? 'enabled' : 'disabled'}`)
71
75
  }
72
76
 
73
- function assertWarning(condition: unknown, msg: string) {
77
+ function assertWarning(
78
+ condition: unknown,
79
+ msg: string,
80
+ { onlyOnce = true, showStackTrace }: { onlyOnce?: boolean | string; showStackTrace?: true } = {},
81
+ ) {
74
82
  if (condition) {
75
83
  return
76
84
  }
77
- msg = '[DocPress][Warning] ' + msg
78
85
  const err = new Error(msg)
79
- if (import.meta.env.DEV) {
80
- console.warn(err)
81
- if (isDevMode()) window.alert(err)
82
- } else {
86
+ if (!import.meta.env.DEV) {
83
87
  throw err
88
+ } else {
89
+ if (onlyOnce) {
90
+ const { alreadyLogged } = globalObject
91
+ const key = onlyOnce === true ? msg : onlyOnce
92
+ if (alreadyLogged.has(key)) return
93
+ alreadyLogged.add(key)
94
+ }
95
+ msg = '[DocPress][Warning] ' + msg
96
+ if (!showStackTrace) {
97
+ console.warn(msg)
98
+ } else {
99
+ console.warn(err)
100
+ }
101
+ if (isDevMode()) window.alert(err)
84
102
  }
85
103
  }
@@ -1,86 +0,0 @@
1
- export type InternalDocSearchHit = DocSearchHit & {
2
- __docsearch_parent?: InternalDocSearchHit | null
3
- }
4
-
5
- export type StoredDocSearchHit = Omit<InternalDocSearchHit, '_highlightResult' | '_snippetResult'>
6
-
7
- type ContentType = 'content' | 'lvl0' | 'lvl1' | 'lvl2' | 'lvl3' | 'lvl4' | 'lvl5' | 'lvl6'
8
-
9
- interface DocSearchHitAttributeHighlightResult {
10
- value: string
11
- matchLevel: 'full' | 'none' | 'partial'
12
- matchedWords: string[]
13
- fullyHighlighted?: boolean
14
- }
15
-
16
- interface DocSearchHitHighlightResultHierarchy {
17
- lvl0: DocSearchHitAttributeHighlightResult
18
- lvl1: DocSearchHitAttributeHighlightResult
19
- lvl2: DocSearchHitAttributeHighlightResult
20
- lvl3: DocSearchHitAttributeHighlightResult
21
- lvl4: DocSearchHitAttributeHighlightResult
22
- lvl5: DocSearchHitAttributeHighlightResult
23
- lvl6: DocSearchHitAttributeHighlightResult
24
- }
25
-
26
- interface DocSearchHitHighlightResult {
27
- content: DocSearchHitAttributeHighlightResult
28
- hierarchy: DocSearchHitHighlightResultHierarchy
29
- hierarchy_camel: DocSearchHitHighlightResultHierarchy[]
30
- }
31
-
32
- interface DocSearchHitAttributeSnippetResult {
33
- value: string
34
- matchLevel: 'full' | 'none' | 'partial'
35
- }
36
-
37
- interface DocSearchHitSnippetResult {
38
- content: DocSearchHitAttributeSnippetResult
39
- hierarchy: DocSearchHitHighlightResultHierarchy
40
- hierarchy_camel: DocSearchHitHighlightResultHierarchy[]
41
- }
42
-
43
- export declare type DocSearchHit = {
44
- objectID: string
45
- content: string | null
46
- url: string
47
- url_without_anchor: string
48
- type: ContentType
49
- anchor: string | null
50
- hierarchy: {
51
- lvl0: string
52
- lvl1: string
53
- lvl2: string | null
54
- lvl3: string | null
55
- lvl4: string | null
56
- lvl5: string | null
57
- lvl6: string | null
58
- }
59
- _highlightResult: DocSearchHitHighlightResult
60
- _snippetResult: DocSearchHitSnippetResult
61
- _rankingInfo?: {
62
- promoted: boolean
63
- nbTypos: number
64
- firstMatchedWord: number
65
- proximityDistance?: number
66
- geoDistance: number
67
- geoPrecision?: number
68
- nbExactWords: number
69
- words: number
70
- filters: number
71
- userScore: number
72
- matchedGeoLocation?: {
73
- lat: number
74
- lng: number
75
- distance: number
76
- }
77
- }
78
- _distinctSeqID?: number
79
- __autocomplete_indexName?: string
80
- __autocomplete_queryID?: string
81
- __autocomplete_algoliaCredentials?: {
82
- appId: string
83
- apiKey: string
84
- }
85
- __autocomplete_id?: number
86
- }