@prose-reader/enhancer-search 1.117.0 → 1.119.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.
Files changed (2) hide show
  1. package/package.json +3 -3
  2. package/src/index.ts +22 -11
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prose-reader/enhancer-search",
3
- "version": "1.117.0",
3
+ "version": "1.119.0",
4
4
  "type": "module",
5
5
  "main": "./dist/prose-reader-enhancer-search.umd.cjs",
6
6
  "module": "./dist/prose-reader-enhancer-search.js",
@@ -19,10 +19,10 @@
19
19
  "test": "vitest run --coverage"
20
20
  },
21
21
  "dependencies": {
22
- "@prose-reader/core": "^1.117.0"
22
+ "@prose-reader/core": "^1.119.0"
23
23
  },
24
24
  "peerDependencies": {
25
25
  "rxjs": "*"
26
26
  },
27
- "gitHead": "7979b7ba99701920e600a1c3a798d141fa3679f3"
27
+ "gitHead": "8fba086fc519af1bef577817629bd55a914cd923"
28
28
  }
package/src/index.ts CHANGED
@@ -3,14 +3,18 @@ import { Reader } from "@prose-reader/core"
3
3
  import { forkJoin, from, merge, Observable, of, Subject } from "rxjs"
4
4
  import { map, share, switchMap, takeUntil } from "rxjs/operators"
5
5
 
6
- const supportedContentType = [
7
- `application/xhtml+xml` as const,
8
- `application/xml` as const,
9
- `image/svg+xml` as const,
10
- `text/html` as const,
11
- `text/xml` as const,
6
+ const supportedContentType: DOMParserSupportedType[] = [
7
+ `application/xhtml+xml`,
8
+ `application/xml`,
9
+ `image/svg+xml`,
10
+ `text/html`,
11
+ `text/xml`,
12
12
  ]
13
13
 
14
+ const isSupportedContentType = (contentType: string): contentType is DOMParserSupportedType => {
15
+ return supportedContentType.includes(contentType as DOMParserSupportedType)
16
+ }
17
+
14
18
  type ResultItem = {
15
19
  spineItemIndex: number
16
20
  startCfi: string
@@ -97,19 +101,26 @@ export const searchEnhancer =
97
101
  return of([])
98
102
  }
99
103
 
100
- return from(item.getResource()).pipe(
104
+ return from(item.resourcesHandler.fetchResource()).pipe(
101
105
  switchMap((response) => {
106
+ if (!(response instanceof Response)) {
107
+ return of([])
108
+ }
109
+
110
+ const contentType = response?.headers.get(`Content-Type`) ?? ``
111
+
102
112
  // small optimization since we already know DOMParser only accept some documents only
103
113
  // the reader returns us a valid HTML document anyway so it is not ultimately necessary.
104
114
  // however we can still avoid doing unnecessary HTML generation for images resources, etc.
105
- if (!supportedContentType.includes(response?.headers.get(`Content-Type`) || (`` as any))) return of([])
115
+ if (!isSupportedContentType(contentType)) return of([])
106
116
 
107
- return from(item.getHtmlFromResource(response)).pipe(
108
- map((html) => {
117
+ return from(response.text()).pipe(
118
+ map((responseText) => {
109
119
  const parser = new DOMParser()
110
- const doc = parser.parseFromString(html, `application/xhtml+xml`)
120
+ const doc = parser.parseFromString(responseText, contentType)
111
121
 
112
122
  const ranges = searchNodeContainingText(doc, text)
123
+
113
124
  const newResults = ranges.map((range) => {
114
125
  const { end, start } = reader.cfi.generateCfiFromRange(range, item.item)
115
126
  const { node, offset, spineItemIndex } = reader.cfi.resolveCfi({ cfi: start }) || {}