File size: 22,332 Bytes
fbe729d
 
5084a8e
4754d8a
 
766c6b5
 
 
 
 
 
 
3da3d2a
 
 
 
766c6b5
 
 
 
5084a8e
 
0b5445b
 
5084a8e
3da3d2a
f0cd651
76d2474
0b5445b
 
 
 
 
 
 
 
 
 
 
 
 
3da3d2a
 
 
 
 
 
 
0b5445b
 
3da3d2a
 
 
 
 
0b5445b
 
3da3d2a
 
 
 
 
 
 
766c6b5
0b5445b
766c6b5
 
5084a8e
766c6b5
b2210de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76d2474
b2210de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fbe729d
 
 
 
 
3da3d2a
fbe729d
 
3da3d2a
 
578492e
3da3d2a
 
 
 
 
 
 
 
 
 
 
 
1bab4c9
 
3da3d2a
 
 
 
 
1bab4c9
 
3da3d2a
 
 
 
 
 
1bab4c9
 
 
3da3d2a
 
 
1787788
 
fbe729d
 
3da3d2a
 
 
 
a293dcd
3da3d2a
 
 
 
 
578492e
 
 
 
 
 
 
 
3da3d2a
 
 
 
 
 
 
 
 
 
ac6d993
3da3d2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5084a8e
 
 
 
 
 
 
 
3da3d2a
 
 
 
 
 
 
1787788
 
 
 
 
 
 
 
3da3d2a
 
fbe729d
 
 
65b6901
fbe729d
 
65b6901
 
 
3da3d2a
5084a8e
3da3d2a
fbe729d
 
 
 
 
 
 
 
5084a8e
3c9c6d1
5084a8e
bbb9a5b
 
f3244ea
5084a8e
cd30830
43acb00
5084a8e
 
 
 
bbb9a5b
5084a8e
bbb9a5b
cd30830
f3244ea
84c65fc
5084a8e
 
 
 
 
2c98197
 
 
5084a8e
1787788
 
5084a8e
 
6db80d0
cd30830
6db80d0
d471240
 
 
2c98197
cd30830
5084a8e
 
 
 
 
 
 
 
 
578492e
5084a8e
 
 
578492e
 
 
669b332
 
5084a8e
 
84c65fc
 
5084a8e
578492e
669b332
 
578492e
5084a8e
 
 
 
 
 
 
 
 
 
578492e
5084a8e
d471240
84c65fc
 
669b332
5084a8e
 
bbb9a5b
535a23c
84c65fc
 
bbb9a5b
 
669b332
bbb9a5b
 
 
5084a8e
 
 
 
bbb9a5b
5084a8e
 
 
 
 
 
 
578492e
 
 
 
5084a8e
 
 
 
 
 
 
 
bbb9a5b
6db80d0
bbb9a5b
 
 
 
 
6db80d0
bbb9a5b
6db80d0
cd30830
5084a8e
 
 
 
535a23c
6db80d0
43acb00
5084a8e
 
 
 
 
 
 
d471240
5084a8e
578492e
 
5084a8e
 
2c98197
535a23c
2c98197
535a23c
bbb9a5b
5084a8e
43acb00
 
578492e
43acb00
 
bbb9a5b
 
8e2dddb
 
 
 
5084a8e
 
 
fbe729d
 
3da3d2a
 
 
fbe729d
5084a8e
 
c9913fc
 
78482d8
3da3d2a
 
 
 
 
 
 
 
 
 
 
 
bbb9a5b
 
3da3d2a
 
8e2dddb
3da3d2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f94ba4f
3da3d2a
 
1c38ef3
fbe729d
e16bd16
 
 
 
 
 
 
 
 
 
 
 
 
 
3da3d2a
 
e16bd16
3da3d2a
 
 
 
 
fbe729d
3da3d2a
 
 
 
 
 
5084a8e
 
 
 
578492e
5084a8e
e16bd16
5084a8e
3da3d2a
 
5084a8e
b2210de
fbe729d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
(function (root) {
  "use strict";
  function preloadPdfOpeningResources() {
    // Classic-script and module consumers share the same in-flight preload.
    if (root.__VOICE_PDF_PRELOAD__) return;
    try {
      const params = new URLSearchParams(location.search);
      const id = params.get("id") || "";
      let source = params.get("url") || "";
      if (!source && id) {
        for (const key of [`reader-source:${id}`, `reader-resolve:${id}`]) {
          const value = JSON.parse(sessionStorage.getItem(key) || "null");
          if (value?.url) {
            source = value.url;
            break;
          }
        }
      }
      if (!source) return;
      const manifestUrl = new URL(source, location.origin);
      const sourceInfo = pdfPageSource(manifestUrl.href, location.origin, location.origin);
      if (!sourceInfo) return;
      const security = root.VoiceOfMLReaderSecurity;
      if (!security) return;
      const pageUrl = new URL(sourceInfo.pageUrl(1));
      const controller = new AbortController(),
        directSource = ["huggingface.co", "hf-mirror.com"].includes(manifestUrl.hostname),
        timer = setTimeout(() => controller.abort(), directSource ? 1500 : 10000);
      let disposed = false;
      let preload;
      const dispose = () => {
        if (disposed) return;
        disposed = true;
        clearTimeout(timer);
        controller.abort();
        if (preload?.image) {
          preload.image.src = "";
          preload.image = null;
        }
        root.removeEventListener?.("pagehide", onPageHide);
      };
      const onPageHide = (event) => {
        if (!event.persisted) dispose();
      };
      const manifestRequest = fetch(manifestUrl.href, {
        priority: "low",
        signal: controller.signal
      })
        .then(async (response) => {
          const bytes = await security.readBytes(response, security.LIMITS.manifestBytes);
          return new Response(bytes, {
            status: response.status,
            statusText: response.statusText,
            headers: response.headers
          });
        })
        .finally(() => clearTimeout(timer));
      preload = {
        manifestUrl: manifestUrl.href,
        manifest: manifestRequest,
        pageUrl: pageUrl.href,
        image: null,
        dispose
      };
      preload.manifest.catch(() => {});
      root.addEventListener?.("pagehide", onPageHide);
      root.__VOICE_PDF_PRELOAD__ = preload;
    } catch (_) {}
  }

  function preloadChapterOpeningResources() {
    if (root.__VOICE_READER_CHAPTER_PRELOAD__) return;
    try {
      if (!location.pathname.endsWith("/reader.html")) return;
      const params = new URLSearchParams(location.search), id = params.get("id") || "";
      let source = params.get("chapter_manifest") || params.get("url") || "";
      if (!source && id) {
        for (const key of [`reader-source:${id}`, `reader-resolve:${id}`]) {
          const value = JSON.parse(sessionStorage.getItem(key) || "null");
          if (value?.url) { source = value.chapter_manifest || value.url; break; }
        }
      }
      const url = new URL(source, location.href), path = url.searchParams.get("path") || "";
      if ((url.origin !== location.origin && url.origin !== "https://voiceofml-search.hf.space") ||
          url.username || url.password || url.hash || url.pathname !== "/api/reader-bucket-resource" ||
          [...url.searchParams.keys()].length !== 1 || !isBucketPath(path, true) ||
          !path.startsWith("chapters/ebook/") || !path.endsWith("/chapter-manifest.json")) return;
      const security = root.VoiceOfMLReaderSecurity;
      if (!security) return;
      const controller = new AbortController(), timer = setTimeout(() => controller.abort(), 10000);
      let disposed = false;
      const onPageHide = event => { if (!event.persisted) dispose(); };
      const dispose = () => {
        if (disposed) return;
        disposed = true;
        clearTimeout(timer);
        controller.abort();
        root.removeEventListener?.("pagehide", onPageHide);
        root.removeEventListener?.("voice-reader-dispose", dispose);
      };
      const manifest = fetch(url.href, { signal: controller.signal, priority: "high", credentials: "omit" })
        .then(async response => {
          const bytes = await security.readBytes(response, security.LIMITS.manifestBytes);
          return new Response(bytes, { status: response.status, statusText: response.statusText, headers: response.headers });
        }).finally(() => clearTimeout(timer));
      manifest.catch(() => {});
      root.__VOICE_READER_CHAPTER_PRELOAD__ = { manifestUrl: url.href, manifest, dispose };
      root.addEventListener?.("pagehide", onPageHide);
      root.addEventListener?.("voice-reader-dispose", dispose);
    } catch (_) {}
  }

  const ReaderMode = Object.freeze({
    UNSUPPORTED: 0,
    ORIGINAL: 1,
    CONVERTED: 2,
    PENDING: 3,
    FAILED: 4
  });
  const modes = Object.freeze({
    pdf: "pdf",
    "pdf-pages": "pdf-pages",
    "image-pages": "image-pages",
    epub: "foliate",
    mobi: "foliate",
    azw: "foliate",
    azw3: "foliate",
    fb2: "foliate",
    fbz: "foliate",
    "epub-chapters": "epub-chapters",
    html: "html",
    htm: "html",
    txt: "text",
    md: "markdown",
    markdown: "markdown",
    vcf: "text",
    ini: "text",
    jpg: "image",
    jpeg: "image",
    png: "image",
    gif: "image",
    bmp: "image",
    tif: "image",
    tiff: "image",
    webp: "image",
    mp3: "audio",
    wav: "audio",
    m4a: "audio",
    flac: "audio",
    mpga: "audio",
    wma: "audio",
    ape: "audio",
    amr: "audio",
    audio: "audio",
    mp4: "video",
    mov: "video",
    video: "video",
    swf: "swf"
  });
  const articleExtensions = Object.freeze(Object.keys(modes));
  const features = Object.freeze({
    pdf: { toc: true, search: true, zoom: true, bookmarks: true, pagination: true, media: false },
    "pdf-pages": {
      toc: true,
      search: true,
      zoom: true,
      bookmarks: true,
      pagination: true,
      media: false
    },
    "image-pages": {
      toc: false,
      search: false,
      zoom: true,
      bookmarks: true,
      pagination: true,
      media: false
    },
    foliate: {
      toc: true,
      search: true,
      zoom: true,
      bookmarks: true,
      pagination: false,
      media: false
    },
    "epub-chapters": {
      toc: true,
      search: true,
      zoom: true,
      bookmarks: true,
      pagination: false,
      media: false
    },
    html: { toc: true, search: true, zoom: true, bookmarks: true, pagination: false, media: false },
    text: {
      toc: false,
      search: true,
      zoom: true,
      bookmarks: true,
      pagination: false,
      media: false
    },
    markdown: {
      toc: true,
      search: true,
      zoom: true,
      bookmarks: true,
      pagination: false,
      media: false
    },
    image: {
      toc: false,
      search: false,
      zoom: true,
      bookmarks: true,
      pagination: false,
      media: false
    },
    audio: {
      toc: false,
      search: false,
      zoom: false,
      bookmarks: true,
      pagination: false,
      media: true
    },
    unsupported: {
      toc: false,
      search: false,
      zoom: false,
      bookmarks: false,
      pagination: false,
      media: false
    },
    video: {
      toc: false,
      search: false,
      zoom: false,
      bookmarks: true,
      pagination: false,
      media: true
    },
    swf: {
      toc: false,
      search: false,
      zoom: false,
      bookmarks: true,
      pagination: false,
      media: true
    }
  });

  function capability(extension) {
    const normalized = String(extension || "").toLowerCase();
    const mode = modes[normalized];
    return Object.freeze({
      extension: normalized,
      mode: mode || null,
      readerMode: mode ? ReaderMode.ORIGINAL : ReaderMode.UNSUPPORTED,
      article: !!mode,
      features: Object.freeze({
        ...(features[mode] || features.unsupported)
      })
    });
  }

  function clampNumber(value, minimum, maximum, fallback) {
    const numeric = Math.round(Number(value));
    return Number.isFinite(numeric) ? Math.min(maximum, Math.max(minimum, numeric)) : fallback;
  }

  const assetRoot = String.raw`objects/[0-9a-f]{2}/[0-9a-f]{64}/`;
  const v3BucketPathPattern = new RegExp(`^${assetRoot}[0-9a-f]{16}/(?:reading-manifest\\.json|page-map\\.json\\.gz|text-layer-manifest\\.json|text-review-manifest\\.json|text/(?:page-[0-9]{6}\\.json\\.gz|book-text\\.json\\.gz|search-[0-9]{6}-[0-9]{6}\\.json\\.gz|partition-[0-9]{6}-[0-9]{6}-manifest\\.json)|preview/(?:page-[0-9]{6}\\.(?:png|webp|jpeg|svg)|partition-[0-9]{6}-[0-9]{6}-manifest\\.json))$`);
  const assetVersion = String.raw`(?:[0-9a-f]{16}/)?`;
  const assetPages = String.raw`(?:page-manifest\.json|pages/page-[0-9]{6}\.(?:webp|jxl))`;
  const assetOcr = String.raw`(?:ocr-manifest\.json|ocr/(?:page-[0-9]{6}\.json\.gz|book-text\.json\.gz))`;
  const assetDocument = String.raw`(?:linearized\.pdf|document\.(?:pdf|epub|mobi|azw|azw3|fb2|html|txt|md|webp|swf)|book\.epub|audio\.(?:mp3|wav|m4a|flac|mpga)|video\.(?:mp4|mov))`;
  const assetChapters = String.raw`(?:chapter-manifest\.json|epub-chapters/(?:chapter-manifest\.json|chapters/chapter-[0-9]{4}\.xhtml|resources/[A-Za-z0-9._~%+\-/]+|epub-search-index\.json\.gz))`;
  const v2ChapterPathPattern = new RegExp(`^chapters/ebook/(?:epub|mobi|azw3|fb2|chm)/[0-9a-f]{64}/[0-9a-f]{16}/(?:chapter-manifest\\.json|epub-search-index\\.json\\.gz|chapters/chapter-[0-9]{4}\\.xhtml|resources/(?!\\.{1,2}(?:/|$))[^/]+(?:/(?!\\.{1,2}(?:/|$))[^/]+)*)$`);
  const ebookBucketPathPattern = new RegExp(`^ebook-chapters/${assetRoot}[a-z0-9-]+/[a-z0-9-]+-epub-chapters-v[0-9]+-bucket/epub-chapters/(?:chapter-manifest\\.json|chapters/chapter-[0-9]{4}\\.xhtml|resources/(?!\\.{1,2}(?:/|$))[^/]+(?:/(?!\\.{1,2}(?:/|$))[^/]+)*|epub-search-index\\.json\\.gz)$`);
  const assetPrimaryPattern = new RegExp(
    `^${assetRoot}${assetVersion}(?:${assetPages}|(?:[a-z0-9-]+/)?${assetDocument})$`
  );
  const assetSourcePattern = new RegExp(
    `^(?:pdf_manifest\\.json|${assetRoot}${assetVersion}(?:${assetPages}|${assetOcr}|(?:[a-z0-9-]+/)?(?:${assetDocument}|${assetChapters})))$`
  );
  const bucketPathPattern = new RegExp(`^${assetRoot}${assetVersion}(?:${assetPages}|${assetOcr})$`);
  const v2ObjectPathPattern = new RegExp(`^${assetRoot}${assetVersion}(?:${assetPages}|${assetOcr})$`);
  const staticBucketPathPattern = new RegExp(`^(?:${assetRoot}(?:[0-9a-f]{16}/)?(?:[a-z0-9-]+/)?(?:document\\.(?:html|txt|md|webp|jpg|jpeg|png|gif|bmp|swf)|audio\\.(?:mp3|wav|m4a|flac|mpga)|video\\.(?:mp4|mov))|documents/(?:text|web|spreadsheet|office|pdf)/[a-z0-9_-]+/[0-9a-f]{64}/document\\.(?:txt|md|html|pdf|vcf|ini)|media/(?:audio|video|swf)/[a-z0-9_-]+/[0-9a-f]{64}/(?:audio\\.[a-z0-9]+|video\\.[a-z0-9]+|document\\.swf)|pages/image/(?:jpg|jpeg|png|bmp|tif|tiff|webp)/[0-9a-f]{64}/(?:page-manifest\\.json|pages/page-[0-9]{6}\\.webp)|derived/[A-Za-z0-9._-]+/[a-z0-9]{32}/document\\.pdf|chapters/ebook/(?:epub|mobi|azw3|fb2|chm)/[0-9a-f]{64}/[0-9a-f]{16}/(?:chapter-manifest\\.json|epub-search-index\\.json\\.gz))$`);
  const versionedBucketPathPattern = new RegExp(`^${assetRoot}[0-9a-f]{16}/(?:${assetPages}|${assetOcr})$`);
  const assetBase = "https://huggingface.co/datasets/vomebook/Reader-Assets/resolve/main/";
  const assetModes = Object.freeze({
    p: "pdf",
    e: "epub",
    h: "html",
    t: "txt",
    k: "md",
    i: "webp",
    a: "audio",
    v: "video",
    f: "swf"
  });

  function isBucketPath(path, versioned = false, bucket = "vomebook/pdf-pages-v2") {
    if (bucket === "vomebook/reader-assets-v2") return staticBucketPathPattern.test(path) || v2ChapterPathPattern.test(path) || v2ObjectPathPattern.test(path);
    if (bucket !== "vomebook/pdf-pages-v2") return false;
    return /^objects\/[0-9a-f]{2}\/[0-9a-f]{64}\/[0-9a-f]{16}\/document\.pdf$/.test(path) ||
      (versioned ? versionedBucketPathPattern : bucketPathPattern).test(path) ||
      v3BucketPathPattern.test(path) ||
      staticBucketPathPattern.test(path) ||
      false;
  }

  function isAssetSourcePath(path) {
    const prefix = /^\/datasets\/vomebook\/Reader-Assets\/resolve\/[^/]+\//.exec(path);
    return !!prefix && assetSourcePattern.test(path.slice(prefix[0].length));
  }

  // One parser for opening preload, manifest validation and page navigation.
  // Dataset assets may be unversioned; the Bucket API requires a version.
  function pdfPageSource(raw, base, bucketOrigin, filename = "page-manifest.json", bucketName = "vomebook/pdf-pages-v2") {
    try {
      const url = new URL(raw, base);
      if (url.username || url.password || url.hash) return null;
      const bucket = [bucketOrigin, "https://voiceofml-search.hf.space"].includes(url.origin) &&
        url.pathname === "/api/reader-bucket-resource";
      const bucketPrefix = `/buckets/${bucketName}/resolve/`;
      const directBucket = url.origin === "https://huggingface.co" &&
        url.pathname.startsWith(bucketPrefix) && !url.search;
      let path;
      if (bucket) {
        if (url.searchParams.getAll("path").length !== 1 ||
            [...url.searchParams.keys()].some((key) => key !== "path")) return null;
        path = url.searchParams.get("path") || "";
        if (!isBucketPath(path, true, bucketName)) return null;
      } else if (directBucket) {
        path = decodeURIComponent(url.pathname.slice(bucketPrefix.length));
        if (!isBucketPath(path, true, bucketName)) return null;
      } else {
        if (
          url.protocol !== "https:" ||
          !["huggingface.co", "hf-mirror.com"].includes(url.hostname) ||
          url.search
        )
          return null;
        const prefix = /^\/datasets\/vomebook\/Reader-Assets\/resolve\/[^/]+\//.exec(url.pathname);
        if (!prefix) return null;
        path = decodeURIComponent(url.pathname.slice(prefix[0].length));
        if (!isBucketPath(path, false, bucketName)) return null;
      }
      if (!["page-manifest.json", "ocr-manifest.json", "reading-manifest.json"].includes(filename) ||
          !path.endsWith(`/${filename}`)) return null;
      const rootPath = path.slice(0, -filename.length - 1);
      const pathPrefix = bucket || directBucket ? "" : url.pathname.slice(0, -path.length);
      return {
        root: rootPath,
        assetUrl(relativePath) {
          if (!isBucketPath(relativePath, bucket || directBucket, bucketName) ||
              !relativePath.startsWith(rootPath.split("/").slice(0, 3).join("/") + "/"))
            throw new Error("PDF_ASSET_INVALID");
          const target = new URL(url.href);
          if (bucket) target.searchParams.set("path", relativePath);
          else if (directBucket) target.pathname = `${bucketPrefix}${relativePath}`;
          else target.pathname = `${pathPrefix}${relativePath}`;
          return target.href;
        },
        pageUrl(page) {
          if (!Number.isInteger(page) || page < 1 || page > 999999)
            throw new Error("PDF_PAGE_INVALID");
          const filename = `pages/page-${String(page).padStart(6, "0")}.webp`;
          return this.assetUrl(`${rootPath}/${filename}`);
        }
      };
    } catch (_) {
      return null;
    }
  }

  function imagePageSource(raw, base, bucketOrigin) {
    return pdfPageSource(raw, base, bucketOrigin, "page-manifest.json", "vomebook/reader-assets-v2");
  }

  function txtRelativePath(path) {
    const value = String(path || "");
    const dot = value.lastIndexOf(".");
    return (dot > value.lastIndexOf("/") ? value.slice(0, dot) : value) + ".txt";
  }

  function assetFields(asset, bucketBase) {
    const path = String(asset?.p || "");
    const ocrPath = String(asset?.o || "");
    const ocrBucket = asset?.b === "vomebook/pdf-pages-v2" || asset?.ob === "vomebook/pdf-pages-v2";
    const ocrUrl = ocrPath.endsWith("/ocr-manifest.json") && ocrBucket && isBucketPath(ocrPath, true)
      ? `${bucketBase}?path=${encodeURIComponent(ocrPath)}`
      : "";
    if (asset?.s === 3 && ocrUrl)
      return { ReaderOcrManifest: ocrUrl, ReaderOcrMode: String(asset.om || "") };
    const bucketName = String(asset?.b || "vomebook/reader-assets-v2");
    const bucket = isBucketPath(path, true, bucketName);
    const chapterBucket = asset?.cb === "vomebook/reader-assets-v2" &&
      (ebookBucketPathPattern.test(String(asset?.c || "")) || v2ChapterPathPattern.test(String(asset?.c || "")));
    if (
      !asset ||
      asset.s !== 2 ||
      !Object.prototype.hasOwnProperty.call(assetModes, asset.m) ||
       !(asset.b ? bucket : bucket || assetPrimaryPattern.test(path)) ||
        (asset?.b && bucketName !== "vomebook/pdf-pages-v2" && bucketName !== "vomebook/reader-assets-v2") ||
       (asset?.cb && !chapterBucket)
    )
      return null;
    const nativeExtension =
      /(?:^|\/)document\.(epub|mobi|azw|azw3|fb2)$/i.exec(path)?.[1]?.toLowerCase() || "epub";
    const extension =
      asset.m === "e"
        ? nativeExtension
        : asset.m === "p" && /\/(?:page|reading)-manifest\.json$/.test(path)
          ? "pdf-pages"
          : asset.m === "i" && path.endsWith("page-manifest.json")
            ? "image-pages"
          : assetModes[asset.m];
    return {
      ReaderLink: chapterBucket
        ? `${bucketBase}?path=${encodeURIComponent(asset.c)}`
        : bucket
          ? `${bucketBase}?path=${encodeURIComponent(path)}`
        : assetBase + path,
      ReaderExtension: asset.c ? "epub-chapters" : extension,
      ReaderChapterManifest: asset.c
        ? chapterBucket
           ? `${bucketBase}?path=${encodeURIComponent(asset.c)}`
          : assetBase + asset.c
        : "",
      ReaderFallback: asset.f ? assetBase + asset.f : "",
      ReaderOcrManifest: ocrUrl || (ocrPath.endsWith("/ocr-manifest.json") ? assetBase + ocrPath : ""),
      ReaderOcrMode: String(asset.om || ""),
      ReaderPdfDocument: asset.pd && ["vomebook/reader-assets-v2", "vomebook/pdf-pages-v2"].includes(asset.pdb) &&
        isBucketPath(asset.pd, true, asset.pdb) && asset.pd.endsWith("/document.pdf")
        ? `${bucketBase}?path=${encodeURIComponent(asset.pd)}` : ""
    };
  }

  function readerUrl(record, basePath) {
    const source = record && (record.ReaderLink || record.readerLink || record.Link || record.link);
    const readerExtension =
      record &&
      (record.ReaderExtension || record.readerExtension || record.Extension || record.extension);
    if (!source || capability(readerExtension).readerMode === ReaderMode.UNSUPPORTED) return "";
    const originalSource =
      record.DownloadLink || record.downloadLink || record.Link || record.link || "";
    const shortId = /^https:\/\/huggingface\.co\/datasets\/VoiceOfML\//.test(originalSource)
      ? shortSourceId(originalSource)
      : "";
    const title =
      (record.File || record.name || "") +
      (record.Extension || record.extension ? "." + (record.Extension || record.extension) : "");
    const params = new URLSearchParams(
      shortId
        ? { id: shortId, title, ext: readerExtension || "" }
        : { url: source, title, ext: readerExtension || "" }
    );
    if (!shortId && (record.DownloadLink || record.downloadLink))
      params.set("download", record.DownloadLink || record.downloadLink);
    if (!shortId && (record.OcrUrl || record.ocrUrl))
      params.set("ocr", record.OcrUrl || record.ocrUrl);
    if (!shortId && (record.ReaderOcrManifest || record.readerOcrManifest))
      params.set("ocr_manifest", record.ReaderOcrManifest || record.readerOcrManifest);
    if (!shortId && (record.ReaderFallback || record.readerFallback))
      params.set("fallback", record.ReaderFallback || record.readerFallback);
    if (!shortId && record.ReaderPdfDocument) params.set("pdf_document", record.ReaderPdfDocument);
    if (
      !shortId &&
      (record.ReaderChapterManifest ||
        record.readerChapterManifest ||
        record.ChapterManifest ||
        record.chapterManifest)
    )
      params.set(
        "chapter_manifest",
        record.ReaderChapterManifest ||
          record.readerChapterManifest ||
          record.ChapterManifest ||
          record.chapterManifest
      );
    if (!shortId && (record.ReturnUrl || record.returnUrl))
      params.set("return", record.ReturnUrl || record.returnUrl);
    const repo = String(record.Repo || record.repo || "")
      .split("/")
      .pop();
    const folder = Array.isArray(record.Folder || record.folder)
      ? (record.Folder || record.folder).join("/")
      : "";
    if (!shortId && repo) params.set("path", repo + (folder ? "/" + folder : ""));
    if (!shortId && (record.FolderUrl || record.folderUrl))
      params.set("folder_url", record.FolderUrl || record.folderUrl);
    return (basePath || "/static/reader.html") + "?" + params.toString();
  }
  function canonicalSourceUrl(value) {
    // Normalize path bytes, not decoded URLs: %2F must remain distinct from /,
    // and %2528 must never become %28. Query/fragment delimiters stay intact.
    return value.replace(/^(https:\/\/huggingface\.co\/datasets\/VoiceOfML\/)([^?#]*)/, (_, prefix, path) =>
      prefix + path.replace(/%[0-9a-fA-F]{2}|[^A-Za-z0-9._~/-]/gu, (token) => {
        if (/^%[0-9a-f]{2}$/i.test(token)) {
          const char = String.fromCharCode(parseInt(token.slice(1), 16));
          return /^[A-Za-z0-9._~-]$/.test(char) ? char : token.toUpperCase();
        }
        return encodeURIComponent(token).replace(/[!'()*]/g, (char) =>
          "%" + char.charCodeAt(0).toString(16).toUpperCase());
      })
    );
  }
  function shortSourceId(value) {
    let hash = 1469598103934665603n;
    for (const byte of new TextEncoder().encode(canonicalSourceUrl(value))) {
      hash ^= BigInt(byte);
      hash = BigInt.asUintN(64, hash * 1099511628211n);
    }
    return hash.toString(36).padStart(13, "0");
  }

  root.VoiceOfMLReader = Object.freeze({
    ReaderMode,
    articleExtensions,
    capability,
    features,
    clampNumber,
    assetFields,
    isAssetSourcePath,
    isBucketPath,
    pdfPageSource,
    imagePageSource,
    txtRelativePath,
    canonicalSourceUrl,
    shortSourceId,
    readerUrl
  });
  preloadPdfOpeningResources();
  preloadChapterOpeningResources();
})(typeof self !== "undefined" ? self : window);