From 382ecb603fbdf126121768542b9d5c9287d853d7 Mon Sep 17 00:00:00 2001 From: aooiuu Date: Fri, 16 Aug 2024 20:08:33 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20epub=20=E5=9B=BE=E7=89=87=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=20(#54)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/epub/src/index.ts | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/packages/epub/src/index.ts b/packages/epub/src/index.ts index 57559a7e..d54fd534 100644 --- a/packages/epub/src/index.ts +++ b/packages/epub/src/index.ts @@ -3,6 +3,7 @@ */ import { EventEmitter } from 'events'; +import { join, dirname } from 'path'; import { defaults as xml2js, Parser } from 'xml2js'; import AdmZip from 'adm-zip'; import { fromByteArray } from 'base64-js'; @@ -623,6 +624,7 @@ class EPub extends EventEmitter { const path = this.rootFile.split('/'); const keys = Object.keys(this.manifest); path.pop(); + const filePath = this.manifest[id].href; // html href // remove linebreaks (no multi line matches in JS regex!) str = str.replace(/\r?\n/g, '\u0000'); @@ -649,27 +651,14 @@ class EPub extends EventEmitter { // replace images str = str.replace(/(\ssrc\s*=\s*["']?)([^"'\s>]*?)(["'\s>])/g, (o: any, a: string, b: string, c: any) => { - const img = path.concat([b]).join('/').trim(); + const imgPath = join(dirname(filePath), b).replace(/\\/g, '/'); try { - const src = 'data:image/png;base64,' + fromByteArray(this.zip.readFile(img.replace('/../', '/')) as unknown as Uint8Array); + const src = 'data:image/png;base64,' + fromByteArray(this.zip.readFile(imgPath) as unknown as Uint8Array); return `${a}${src}${c}`; } catch (error) { console.warn(error); } - - let element; - - for (i = 0, len = keys.length; i < len; i++) { - if (this.manifest[keys[i]].href == img) { - element = this.manifest[keys[i]]; - break; - } - } - - // include only images from manifest - if (element) { - return `${a + this.imageroot + element.id}/${img}${c}`; - } else return ''; + return ''; }); // replace links