@prose-reader/enhancer-search 1.117.0 → 1.118.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.
- package/package.json +3 -3
- 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.
|
|
3
|
+
"version": "1.118.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.
|
|
22
|
+
"@prose-reader/core": "^1.118.0"
|
|
23
23
|
},
|
|
24
24
|
"peerDependencies": {
|
|
25
25
|
"rxjs": "*"
|
|
26
26
|
},
|
|
27
|
-
"gitHead": "
|
|
27
|
+
"gitHead": "59295c56689dd28573e727df2e4cb688f5331e66"
|
|
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
|
|
8
|
-
`application/xml
|
|
9
|
-
`image/svg+xml
|
|
10
|
-
`text/html
|
|
11
|
-
`text/xml
|
|
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.
|
|
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 (!
|
|
115
|
+
if (!isSupportedContentType(contentType)) return of([])
|
|
106
116
|
|
|
107
|
-
return from(
|
|
108
|
-
map((
|
|
117
|
+
return from(response.text()).pipe(
|
|
118
|
+
map((responseText) => {
|
|
109
119
|
const parser = new DOMParser()
|
|
110
|
-
const doc = parser.parseFromString(
|
|
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 }) || {}
|