@prose-reader/enhancer-search 1.8.0 → 1.9.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 CHANGED
@@ -1,24 +1,29 @@
1
1
  {
2
2
  "name": "@prose-reader/enhancer-search",
3
- "version": "1.8.0",
4
- "main": "dist/index.js",
5
- "license": "MIT",
6
- "files": [
7
- "/dist"
8
- ],
3
+ "version": "1.9.0",
4
+ "type": "module",
5
+ "main": "./dist/prose-reader-enhancer-search.umd.cjs",
6
+ "module": "./dist/prose-reader-enhancer-search.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/prose-reader-enhancer-search.js",
11
+ "require": "./dist/prose-reader-enhancer-search.umd.cjs"
12
+ }
13
+ },
9
14
  "scripts": {
10
- "start": "prose-reader-enhancer-scripts start",
11
- "build": "NODE_ENV=development prose-reader-enhancer-scripts build",
12
- "build:prod": "NODE_ENV=production prose-reader-enhancer-scripts build",
13
- "lint": "prose-reader-enhancer-scripts lint",
14
- "test": "echo"
15
+ "start": "vite build --watch --mode development",
16
+ "build": "tsc && vite build",
17
+ "lint:read": "prettier --check . && eslint . --ext .ts,.tsx,.js,.jsx",
18
+ "lint:write": "prettier --write . && eslint --fix . --ext .ts,.tsx,.js,.jsx",
19
+ "test": "vitest run --coverage"
15
20
  },
16
21
  "dependencies": {
17
- "@prose-reader/core": "^1.8.0",
22
+ "@prose-reader/core": "^1.9.0",
18
23
  "@prose-reader/enhancer-scripts": "^1.8.0"
19
24
  },
20
25
  "peerDependencies": {
21
26
  "rxjs": "*"
22
27
  },
23
- "gitHead": "2eb25b5fea63b563bbd426f18bc4464a609ad7af"
28
+ "gitHead": "e419ffe0deab682e8c52ea27fb7fa55208f26be1"
24
29
  }
@@ -0,0 +1,7 @@
1
+ import { expect, test } from "vitest"
2
+
3
+ test("it works", () => {
4
+ expect(true).toBe(true)
5
+ })
6
+
7
+ export {}
package/src/index.ts ADDED
@@ -0,0 +1,179 @@
1
+ import { Enhancer } from "@prose-reader/core"
2
+ import { forkJoin, from, merge, Observable, of, Subject } from "rxjs"
3
+ import { map, share, switchMap, takeUntil } from "rxjs/operators"
4
+
5
+ type ResultItem = {
6
+ spineItemIndex: number
7
+ startCfi: string
8
+ endCfi: string
9
+ pageIndex?: number
10
+ contextText: string
11
+ startOffset: number
12
+ endOffset: number
13
+ }
14
+
15
+ const supportedContentType = [
16
+ `application/xhtml+xml` as const,
17
+ `application/xml` as const,
18
+ `image/svg+xml` as const,
19
+ `text/html` as const,
20
+ `text/xml` as const,
21
+ ]
22
+
23
+ export type SearchResult = ResultItem[]
24
+
25
+ /**
26
+ *
27
+ */
28
+ export const searchEnhancer: Enhancer<
29
+ Record<string, never>,
30
+ {
31
+ search: {
32
+ search: (text: string) => void
33
+ $: {
34
+ search$: Observable<{ type: `start` } | { type: `end`; data: SearchResult }>
35
+ }
36
+ }
37
+ }
38
+ > = (next) => (options) => {
39
+ const reader = next(options)
40
+
41
+ const searchSubject$ = new Subject<string>()
42
+
43
+ const searchNodeContainingText = (node: Node, text: string) => {
44
+ const nodeList = node.childNodes
45
+
46
+ if (node.nodeName === `head`) return []
47
+
48
+ const rangeList: {
49
+ startNode: Node
50
+ start: number
51
+ endNode: Node
52
+ end: number
53
+ }[] = []
54
+ for (let i = 0; i < nodeList.length; i++) {
55
+ const subNode = nodeList[i]
56
+
57
+ if (!subNode) {
58
+ continue
59
+ }
60
+
61
+ if (subNode?.hasChildNodes()) {
62
+ rangeList.push(...searchNodeContainingText(subNode, text))
63
+ }
64
+
65
+ if (subNode.nodeType === 3) {
66
+ const content = (subNode as Text).data.toLowerCase()
67
+ if (content) {
68
+ let match
69
+ const regexp = RegExp(`(${text})`, `g`)
70
+
71
+ while ((match = regexp.exec(content)) !== null) {
72
+ if (match.index >= 0 && subNode.ownerDocument) {
73
+ const range = subNode.ownerDocument.createRange()
74
+ range.setStart(subNode, match.index)
75
+ range.setEnd(subNode, match.index + text.length)
76
+ rangeList.push({
77
+ startNode: subNode,
78
+ start: match.index,
79
+ endNode: subNode,
80
+ end: match.index + text.length,
81
+ })
82
+ }
83
+ }
84
+ }
85
+ }
86
+ }
87
+
88
+ return rangeList
89
+ }
90
+
91
+ const searchForItem = (index: number, text: string) => {
92
+ const item = reader.getSpineItem(index)
93
+
94
+ if (!item) {
95
+ return of([])
96
+ }
97
+
98
+ return from(item.getResource()).pipe(
99
+ switchMap((response) => {
100
+ // small optimization since we already know DOMParser only accept some documents only
101
+ // the reader returns us a valid HTML document anyway so it is not ultimately necessary.
102
+ // however we can still avoid doing unnecessary HTML generation for images resources, etc.
103
+ if (!supportedContentType.includes(response?.headers.get(`Content-Type`) || (`` as any))) return of([])
104
+
105
+ return from(item.getHtmlFromResource(response)).pipe(
106
+ map((html) => {
107
+ const parser = new DOMParser()
108
+ const doc = parser.parseFromString(html, `application/xhtml+xml`)
109
+
110
+ const ranges = searchNodeContainingText(doc, text)
111
+ const newResults = ranges.map((range) => {
112
+ const { end, start } = reader.generateCfi(range, item.item)
113
+ const { node, offset, spineItemIndex } = reader.resolveCfi(start) || {}
114
+ const pageIndex =
115
+ node && spineItemIndex !== undefined
116
+ ? reader.locator.getSpineItemPageIndexFromNode(node, offset, spineItemIndex)
117
+ : undefined
118
+
119
+ return {
120
+ spineItemIndex: index,
121
+ startCfi: start,
122
+ endCfi: end,
123
+ pageIndex,
124
+ contextText: range.startNode.parentElement?.textContent || ``,
125
+ startOffset: range.start,
126
+ endOffset: range.end,
127
+ }
128
+ })
129
+
130
+ return newResults
131
+ })
132
+ )
133
+ })
134
+ )
135
+ }
136
+
137
+ const search = (text: string) => {
138
+ searchSubject$.next(text)
139
+ }
140
+
141
+ /**
142
+ * Main search process stream
143
+ */
144
+ const search$ = merge(
145
+ searchSubject$.asObservable().pipe(map(() => ({ type: `start` as const }))),
146
+ searchSubject$.asObservable().pipe(
147
+ switchMap((text) => {
148
+ if (text === ``) {
149
+ return of([])
150
+ }
151
+
152
+ const searches$ = reader.context.getManifest()?.spineItems.map((_, index) => searchForItem(index, text)) || []
153
+
154
+ return forkJoin(searches$).pipe(
155
+ map((results) => {
156
+ return results.reduce((acc, value) => [...acc, ...value], [])
157
+ })
158
+ )
159
+ }),
160
+ map((data) => ({ type: `end` as const, data }))
161
+ )
162
+ ).pipe(share(), takeUntil(reader.$.destroy$))
163
+
164
+ const destroy = () => {
165
+ searchSubject$.complete()
166
+ reader.destroy()
167
+ }
168
+
169
+ return {
170
+ ...reader,
171
+ destroy,
172
+ search: {
173
+ search,
174
+ $: {
175
+ search$,
176
+ },
177
+ },
178
+ }
179
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ "useDefineForClassFields": true,
5
+ "module": "ESNext",
6
+ "lib": ["ESNext", "DOM"],
7
+ "moduleResolution": "Node",
8
+ "strict": true,
9
+ "resolveJsonModule": true,
10
+ "isolatedModules": true,
11
+ "esModuleInterop": true,
12
+ "noEmit": true,
13
+ "noUnusedLocals": true,
14
+ "noUnusedParameters": true,
15
+ "noImplicitReturns": true,
16
+ "skipLibCheck": true
17
+ },
18
+ "include": ["src"]
19
+ }
package/vite.config.js ADDED
@@ -0,0 +1,31 @@
1
+ import { defineConfig } from "vite"
2
+ import dts from "vite-plugin-dts"
3
+ import { resolve } from "path"
4
+ import { name } from "./package.json"
5
+ import { externals } from "rollup-plugin-node-externals"
6
+
7
+ const libName = name.replace(`@`, ``).replace(`/`, `-`)
8
+
9
+ export default defineConfig(({ mode }) => ({
10
+ build: {
11
+ minify: false,
12
+ lib: {
13
+ entry: resolve(__dirname, `src/index.ts`),
14
+ name: libName,
15
+ fileName: libName,
16
+ },
17
+ emptyOutDir: mode !== `development`,
18
+ sourcemap: true,
19
+ },
20
+ plugins: [
21
+ {
22
+ enforce: `pre`,
23
+ ...externals({
24
+ peerDeps: true,
25
+ deps: true,
26
+ devDeps: true,
27
+ }),
28
+ },
29
+ dts(),
30
+ ],
31
+ }))
@@ -0,0 +1,9 @@
1
+ import { defineConfig } from "vitest/config"
2
+
3
+ export default defineConfig(({ mode }) => ({
4
+ test: {
5
+ coverage: {
6
+ reportsDirectory: `./.test/coverage`,
7
+ },
8
+ },
9
+ }))
package/dist/index.d.ts DELETED
@@ -1,26 +0,0 @@
1
- import { Enhancer } from "@prose-reader/core";
2
- import { Observable } from "rxjs";
3
- declare type ResultItem = {
4
- spineItemIndex: number;
5
- startCfi: string;
6
- endCfi: string;
7
- pageIndex?: number;
8
- contextText: string;
9
- startOffset: number;
10
- endOffset: number;
11
- };
12
- export declare type SearchResult = ResultItem[];
13
- export declare const searchEnhancer: Enhancer<{}, {
14
- search: {
15
- search: (text: string) => void;
16
- $: {
17
- search$: Observable<{
18
- type: `start`;
19
- } | {
20
- type: `end`;
21
- data: SearchResult;
22
- }>;
23
- };
24
- };
25
- }>;
26
- export {};
package/dist/index.js DELETED
@@ -1,218 +0,0 @@
1
- /******/ (() => { // webpackBootstrap
2
- /******/ "use strict";
3
- /******/ var __webpack_modules__ = ({
4
-
5
- /***/ "rxjs":
6
- /*!***********************!*\
7
- !*** external "rxjs" ***!
8
- \***********************/
9
- /***/ ((module) => {
10
-
11
- module.exports = require("rxjs");
12
-
13
- /***/ }),
14
-
15
- /***/ "rxjs/operators":
16
- /*!*********************************!*\
17
- !*** external "rxjs/operators" ***!
18
- \*********************************/
19
- /***/ ((module) => {
20
-
21
- module.exports = require("rxjs/operators");
22
-
23
- /***/ })
24
-
25
- /******/ });
26
- /************************************************************************/
27
- /******/ // The module cache
28
- /******/ var __webpack_module_cache__ = {};
29
- /******/
30
- /******/ // The require function
31
- /******/ function __webpack_require__(moduleId) {
32
- /******/ // Check if module is in cache
33
- /******/ var cachedModule = __webpack_module_cache__[moduleId];
34
- /******/ if (cachedModule !== undefined) {
35
- /******/ return cachedModule.exports;
36
- /******/ }
37
- /******/ // Create a new module (and put it into the cache)
38
- /******/ var module = __webpack_module_cache__[moduleId] = {
39
- /******/ // no module.id needed
40
- /******/ // no module.loaded needed
41
- /******/ exports: {}
42
- /******/ };
43
- /******/
44
- /******/ // Execute the module function
45
- /******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
46
- /******/
47
- /******/ // Return the exports of the module
48
- /******/ return module.exports;
49
- /******/ }
50
- /******/
51
- /************************************************************************/
52
- /******/ /* webpack/runtime/compat get default export */
53
- /******/ (() => {
54
- /******/ // getDefaultExport function for compatibility with non-harmony modules
55
- /******/ __webpack_require__.n = (module) => {
56
- /******/ var getter = module && module.__esModule ?
57
- /******/ () => (module['default']) :
58
- /******/ () => (module);
59
- /******/ __webpack_require__.d(getter, { a: getter });
60
- /******/ return getter;
61
- /******/ };
62
- /******/ })();
63
- /******/
64
- /******/ /* webpack/runtime/define property getters */
65
- /******/ (() => {
66
- /******/ // define getter functions for harmony exports
67
- /******/ __webpack_require__.d = (exports, definition) => {
68
- /******/ for(var key in definition) {
69
- /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
70
- /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
71
- /******/ }
72
- /******/ }
73
- /******/ };
74
- /******/ })();
75
- /******/
76
- /******/ /* webpack/runtime/hasOwnProperty shorthand */
77
- /******/ (() => {
78
- /******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
79
- /******/ })();
80
- /******/
81
- /******/ /* webpack/runtime/make namespace object */
82
- /******/ (() => {
83
- /******/ // define __esModule on exports
84
- /******/ __webpack_require__.r = (exports) => {
85
- /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
86
- /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
87
- /******/ }
88
- /******/ Object.defineProperty(exports, '__esModule', { value: true });
89
- /******/ };
90
- /******/ })();
91
- /******/
92
- /************************************************************************/
93
- var __webpack_exports__ = {};
94
- // This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
95
- (() => {
96
- /*!**********************!*\
97
- !*** ./src/index.ts ***!
98
- \**********************/
99
- __webpack_require__.r(__webpack_exports__);
100
- /* harmony export */ __webpack_require__.d(__webpack_exports__, {
101
- /* harmony export */ "searchEnhancer": () => (/* binding */ searchEnhancer)
102
- /* harmony export */ });
103
- /* harmony import */ var rxjs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! rxjs */ "rxjs");
104
- /* harmony import */ var rxjs__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(rxjs__WEBPACK_IMPORTED_MODULE_0__);
105
- /* harmony import */ var rxjs_operators__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! rxjs/operators */ "rxjs/operators");
106
- /* harmony import */ var rxjs_operators__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(rxjs_operators__WEBPACK_IMPORTED_MODULE_1__);
107
-
108
-
109
- const supportedContentType = [`application/xhtml+xml`, `application/xml`, `image/svg+xml`, `text/html`, `text/xml`];
110
- const searchEnhancer = (next) => (options) => {
111
- const reader = next(options);
112
- const searchSubject$ = new rxjs__WEBPACK_IMPORTED_MODULE_0__.Subject();
113
- const searchNodeContainingText = (node, text) => {
114
- const nodeList = node.childNodes;
115
- if (node.nodeName === `head`)
116
- return [];
117
- const rangeList = [];
118
- for (let i = 0; i < nodeList.length; i++) {
119
- const subNode = nodeList[i];
120
- if (!subNode) {
121
- continue;
122
- }
123
- if (subNode === null || subNode === void 0 ? void 0 : subNode.hasChildNodes()) {
124
- rangeList.push(...searchNodeContainingText(subNode, text));
125
- }
126
- if (subNode.nodeType === 3) {
127
- const content = subNode.data.toLowerCase();
128
- if (content) {
129
- let match;
130
- const regexp = RegExp(`(${text})`, `g`);
131
- while ((match = regexp.exec(content)) !== null) {
132
- if (match.index >= 0 && subNode.ownerDocument) {
133
- const range = subNode.ownerDocument.createRange();
134
- range.setStart(subNode, match.index);
135
- range.setEnd(subNode, match.index + text.length);
136
- rangeList.push({
137
- startNode: subNode,
138
- start: match.index,
139
- endNode: subNode,
140
- end: match.index + text.length
141
- });
142
- }
143
- }
144
- }
145
- }
146
- }
147
- return rangeList;
148
- };
149
- const searchForItem = (index, text) => {
150
- const item = reader.getSpineItem(index);
151
- if (!item) {
152
- return (0,rxjs__WEBPACK_IMPORTED_MODULE_0__.of)([]);
153
- }
154
- return (0,rxjs__WEBPACK_IMPORTED_MODULE_0__.from)(item.getResource())
155
- .pipe((0,rxjs_operators__WEBPACK_IMPORTED_MODULE_1__.switchMap)(response => {
156
- if (!supportedContentType.includes((response === null || response === void 0 ? void 0 : response.headers.get(`Content-Type`)) || ``))
157
- return (0,rxjs__WEBPACK_IMPORTED_MODULE_0__.of)([]);
158
- return (0,rxjs__WEBPACK_IMPORTED_MODULE_0__.from)(item.getHtmlFromResource(response))
159
- .pipe((0,rxjs_operators__WEBPACK_IMPORTED_MODULE_1__.map)(html => {
160
- const parser = new DOMParser();
161
- const doc = parser.parseFromString(html, `application/xhtml+xml`);
162
- const ranges = searchNodeContainingText(doc, text);
163
- const newResults = ranges.map(range => {
164
- var _a;
165
- const { end, start } = reader.generateCfi(range, item.item);
166
- const { node, offset, spineItemIndex } = reader.resolveCfi(start) || {};
167
- const pageIndex = node && spineItemIndex !== undefined ? reader.locator.getSpineItemPageIndexFromNode(node, offset, spineItemIndex) : undefined;
168
- return {
169
- spineItemIndex: index,
170
- startCfi: start,
171
- endCfi: end,
172
- pageIndex,
173
- contextText: ((_a = range.startNode.parentElement) === null || _a === void 0 ? void 0 : _a.textContent) || ``,
174
- startOffset: range.start,
175
- endOffset: range.end
176
- };
177
- });
178
- return newResults;
179
- }));
180
- }));
181
- };
182
- const search = (text) => {
183
- searchSubject$.next(text);
184
- };
185
- const search$ = (0,rxjs__WEBPACK_IMPORTED_MODULE_0__.merge)(searchSubject$.asObservable()
186
- .pipe((0,rxjs_operators__WEBPACK_IMPORTED_MODULE_1__.map)(() => ({ type: `start` }))), searchSubject$.asObservable()
187
- .pipe((0,rxjs_operators__WEBPACK_IMPORTED_MODULE_1__.switchMap)((text) => {
188
- var _a;
189
- if (text === ``) {
190
- return (0,rxjs__WEBPACK_IMPORTED_MODULE_0__.of)([]);
191
- }
192
- const searches$ = ((_a = reader.context.getManifest()) === null || _a === void 0 ? void 0 : _a.spineItems.map((_, index) => searchForItem(index, text))) || [];
193
- return (0,rxjs__WEBPACK_IMPORTED_MODULE_0__.forkJoin)(searches$)
194
- .pipe((0,rxjs_operators__WEBPACK_IMPORTED_MODULE_1__.map)(results => {
195
- return results.reduce((acc, value) => [...acc, ...value], []);
196
- }));
197
- }), (0,rxjs_operators__WEBPACK_IMPORTED_MODULE_1__.map)((data) => ({ type: `end`, data }))))
198
- .pipe((0,rxjs_operators__WEBPACK_IMPORTED_MODULE_1__.share)(), (0,rxjs_operators__WEBPACK_IMPORTED_MODULE_1__.takeUntil)(reader.$.destroy$));
199
- const destroy = () => {
200
- searchSubject$.complete();
201
- reader.destroy();
202
- };
203
- return Object.assign(Object.assign({}, reader), { destroy, search: {
204
- search,
205
- $: {
206
- search$
207
- }
208
- } });
209
- };
210
-
211
- })();
212
-
213
- var __webpack_export_target__ = exports;
214
- for(var i in __webpack_exports__) __webpack_export_target__[i] = __webpack_exports__[i];
215
- if(__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, "__esModule", { value: true });
216
- /******/ })()
217
- ;
218
- //# sourceMappingURL=index.js.map
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","mappings":";;;;;;;;;;AAAA;;;;;;;;;;ACAA;;;;;;UCAA;UACA;;UAEA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;;UAEA;UACA;;UAEA;UACA;UACA;;;;;WCtBA;WACA;WACA;WACA;WACA;WACA,iCAAiC,WAAW;WAC5C;WACA;;;;;WCPA;WACA;WACA;WACA;WACA,yCAAyC,wCAAwC;WACjF;WACA;WACA;;;;;WCPA;;;;;WCAA;WACA;WACA;WACA,uDAAuD,iBAAiB;WACxE;WACA,gDAAgD,aAAa;WAC7D;;;;;;;;;;;;;;;;;;ACN0D;AACQ;AAClE;AACO;AACP;AACA,+BAA+B,yCAAO;AACtC;AACA;AACA;AACA;AACA;AACA,wBAAwB,qBAAqB;AAC7C;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,8CAA8C,KAAK;AACnD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,6BAA6B;AAC7B;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,mBAAmB,wCAAE;AACrB;AACA,eAAe,0CAAI;AACnB,kBAAkB,yDAAS;AAC3B;AACA,uBAAuB,wCAAE;AACzB,mBAAmB,0CAAI;AACvB,sBAAsB,mDAAG;AACzB;AACA;AACA;AACA;AACA;AACA,4BAA4B,aAAa;AACzC,4BAA4B,+BAA+B;AAC3D;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,iBAAiB;AACjB;AACA,aAAa;AACb,SAAS;AACT;AACA;AACA;AACA;AACA,oBAAoB,2CAAK;AACzB,cAAc,mDAAG,UAAU,eAAe;AAC1C,cAAc,yDAAS;AACvB;AACA;AACA,mBAAmB,wCAAE;AACrB;AACA;AACA,eAAe,8CAAQ;AACvB,kBAAkB,mDAAG;AACrB;AACA,SAAS;AACT,KAAK,GAAG,mDAAG,cAAc,mBAAmB;AAC5C,cAAc,qDAAK,IAAI,yDAAS;AAChC;AACA;AACA;AACA;AACA,yCAAyC,aAAa;AACtD;AACA;AACA;AACA;AACA,WAAW;AACX","sources":["webpack://@prose-reader/enhancer-search/external commonjs \"rxjs\"","webpack://@prose-reader/enhancer-search/external commonjs \"rxjs/operators\"","webpack://@prose-reader/enhancer-search/webpack/bootstrap","webpack://@prose-reader/enhancer-search/webpack/runtime/compat get default export","webpack://@prose-reader/enhancer-search/webpack/runtime/define property getters","webpack://@prose-reader/enhancer-search/webpack/runtime/hasOwnProperty shorthand","webpack://@prose-reader/enhancer-search/webpack/runtime/make namespace object","webpack://@prose-reader/enhancer-search/./src/index.ts"],"sourcesContent":["module.exports = require(\"rxjs\");","module.exports = require(\"rxjs/operators\");","// The module cache\nvar __webpack_module_cache__ = {};\n\n// The require function\nfunction __webpack_require__(moduleId) {\n\t// Check if module is in cache\n\tvar cachedModule = __webpack_module_cache__[moduleId];\n\tif (cachedModule !== undefined) {\n\t\treturn cachedModule.exports;\n\t}\n\t// Create a new module (and put it into the cache)\n\tvar module = __webpack_module_cache__[moduleId] = {\n\t\t// no module.id needed\n\t\t// no module.loaded needed\n\t\texports: {}\n\t};\n\n\t// Execute the module function\n\t__webpack_modules__[moduleId](module, module.exports, __webpack_require__);\n\n\t// Return the exports of the module\n\treturn module.exports;\n}\n\n","// getDefaultExport function for compatibility with non-harmony modules\n__webpack_require__.n = (module) => {\n\tvar getter = module && module.__esModule ?\n\t\t() => (module['default']) :\n\t\t() => (module);\n\t__webpack_require__.d(getter, { a: getter });\n\treturn getter;\n};","// define getter functions for harmony exports\n__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n\t\tif(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n\t\t\tObject.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n\t\t}\n\t}\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","import { forkJoin, from, merge, of, Subject } from \"rxjs\";\nimport { map, share, switchMap, takeUntil } from \"rxjs/operators\";\nconst supportedContentType = [`application/xhtml+xml`, `application/xml`, `image/svg+xml`, `text/html`, `text/xml`];\nexport const searchEnhancer = (next) => (options) => {\n const reader = next(options);\n const searchSubject$ = new Subject();\n const searchNodeContainingText = (node, text) => {\n const nodeList = node.childNodes;\n if (node.nodeName === `head`)\n return [];\n const rangeList = [];\n for (let i = 0; i < nodeList.length; i++) {\n const subNode = nodeList[i];\n if (!subNode) {\n continue;\n }\n if (subNode === null || subNode === void 0 ? void 0 : subNode.hasChildNodes()) {\n rangeList.push(...searchNodeContainingText(subNode, text));\n }\n if (subNode.nodeType === 3) {\n const content = subNode.data.toLowerCase();\n if (content) {\n let match;\n const regexp = RegExp(`(${text})`, `g`);\n while ((match = regexp.exec(content)) !== null) {\n if (match.index >= 0 && subNode.ownerDocument) {\n const range = subNode.ownerDocument.createRange();\n range.setStart(subNode, match.index);\n range.setEnd(subNode, match.index + text.length);\n rangeList.push({\n startNode: subNode,\n start: match.index,\n endNode: subNode,\n end: match.index + text.length\n });\n }\n }\n }\n }\n }\n return rangeList;\n };\n const searchForItem = (index, text) => {\n const item = reader.getSpineItem(index);\n if (!item) {\n return of([]);\n }\n return from(item.getResource())\n .pipe(switchMap(response => {\n if (!supportedContentType.includes((response === null || response === void 0 ? void 0 : response.headers.get(`Content-Type`)) || ``))\n return of([]);\n return from(item.getHtmlFromResource(response))\n .pipe(map(html => {\n const parser = new DOMParser();\n const doc = parser.parseFromString(html, `application/xhtml+xml`);\n const ranges = searchNodeContainingText(doc, text);\n const newResults = ranges.map(range => {\n var _a;\n const { end, start } = reader.generateCfi(range, item.item);\n const { node, offset, spineItemIndex } = reader.resolveCfi(start) || {};\n const pageIndex = node && spineItemIndex !== undefined ? reader.locator.getSpineItemPageIndexFromNode(node, offset, spineItemIndex) : undefined;\n return {\n spineItemIndex: index,\n startCfi: start,\n endCfi: end,\n pageIndex,\n contextText: ((_a = range.startNode.parentElement) === null || _a === void 0 ? void 0 : _a.textContent) || ``,\n startOffset: range.start,\n endOffset: range.end\n };\n });\n return newResults;\n }));\n }));\n };\n const search = (text) => {\n searchSubject$.next(text);\n };\n const search$ = merge(searchSubject$.asObservable()\n .pipe(map(() => ({ type: `start` }))), searchSubject$.asObservable()\n .pipe(switchMap((text) => {\n var _a;\n if (text === ``) {\n return of([]);\n }\n const searches$ = ((_a = reader.context.getManifest()) === null || _a === void 0 ? void 0 : _a.spineItems.map((_, index) => searchForItem(index, text))) || [];\n return forkJoin(searches$)\n .pipe(map(results => {\n return results.reduce((acc, value) => [...acc, ...value], []);\n }));\n }), map((data) => ({ type: `end`, data }))))\n .pipe(share(), takeUntil(reader.$.destroy$));\n const destroy = () => {\n searchSubject$.complete();\n reader.destroy();\n };\n return Object.assign(Object.assign({}, reader), { destroy, search: {\n search,\n $: {\n search$\n }\n } });\n};\n"],"names":[],"sourceRoot":""}