From 6901e0a6f5fb7192e505b2f751b86b73fa3b0c01 Mon Sep 17 00:00:00 2001 From: ldm2060 Date: Tue, 19 Nov 2024 16:02:31 +0800 Subject: [PATCH] fix #692 --- README.md | 53 ++++++++++++++++++++++++--------------------- src/save/epub.ts | 9 ++++---- src/save/options.ts | 6 +++++ src/ui/setting.ts | 16 ++++++++++++-- 4 files changed, 52 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 594e9836..88802206 100644 --- a/README.md +++ b/README.md @@ -441,31 +441,20 @@ function chapterFilter(chapter) { 使用方法大致同自定义筛选函数,即在 `window` 下创建 `saveOptions` 对象,具体格式如下: ```typescript -declare class saveBook { - protected book: Book; - mainStyleText: string; - tocStyleText: string; - constructor(book: Book); - saveTxt(): void; - saveLog(): void; - saveZip(runSaveChapters?: boolean): Promise; - addChapter(chapter: Chapter): void; - getchapterName(chapter: Chapter): string; - genSectionText(sectionName: string): string; - genChapterText(chapterName: string, contentText: string): string; - genSectionHtmlFile(chapterObj: Chapter): Blob; - genChapterHtmlFile(chapterObj: Chapter): Blob; - chapterSort(a: Chapter, b: Chapter): 0 | 1 | -1; -} -interface saveOptions { - mainStyleText?: saveBook["mainStyleText"]; - tocStyleText?: saveBook["tocStyleText"]; - getchapterName?: saveBook["getchapterName"]; - genSectionText?: saveBook["genSectionText"]; - genChapterText?: saveBook["genChapterText"]; - genSectionHtmlFile?: saveBook["genSectionHtmlFile"]; - genChapterHtmlFile?: saveBook["genChapterHtmlFile"]; - chapterSort?: saveBook["chapterSort"]; + +interface SaveOptions { + mainStyleText?: string; + tocStyleText?: string; + getchapterName?: Options["getchapterName"]; + //函数定义为:getchapterName(chapter: Chapter): string; + genSectionText?: Options["genSectionText"]; + //函数定义为:genSectionText(sectionName: string): string; + genChapterText?: Options["genChapterText"]; + //函数定义为:genChapterText(chapterName: string, contentText: string): string; + genChapterEpub?: Options["genChapterEpub"]; + //函数定义为:genChapterEpub(contentXHTML: string):string; + chapterSort?: Options["chapterSort"]; + //函数定义为:chapterSort(a: Chapter, b: Chapter): 0 | 1 | -1; } ``` @@ -521,7 +510,17 @@ const saveOptions = { }; window.saveOptions = saveOptions; ``` +epub 文档删除章节空行 +```javascript +const saveOptions = { + genChapterEpub: (contentXHTML) => { + return contentXHTML.replaceAll("


", "") + .replaceAll("


", ""); + }, +}; +window.saveOptions = saveOptions; +``` 保存章节时倒序排列 ```javascript @@ -637,6 +636,10 @@ window.customFinishCallback = customFinishCallback; return `第${chapter.chapterNumber.toString()}章`; } // 按 第i章 XXX 命名章节名字 }, + genChapterEpub: (contentXHTML) => { + return contentXHTML.replaceAll("


", "") + .replaceAll("


", ""); + }, }; //保存设置结束 diff --git a/src/save/epub.ts b/src/save/epub.ts index 5bdad38f..8f3b28e0 100644 --- a/src/save/epub.ts +++ b/src/save/epub.ts @@ -267,19 +267,18 @@ export class EPUB extends Options { } } - private static genChapterHtmlFile(chapterObj: Chapter) { + private genChapterHtmlFile(chapterObj: Chapter) { const _htmlText = chapterTemplt.render({ chapterUrl: chapterObj.chapterUrl, chapterName: chapterObj.chapterName, outerHTML: chapterObj.contentHTML?.outerHTML ?? "", }); - const htmlText = convertHTMLtoXHTML(_htmlText); + let htmlText = convertHTMLtoXHTML(_htmlText); + htmlText = this.genChapterEpub(htmlText); return new Blob( [ ``, htmlText - .replaceAll("


", "") - .replaceAll("


", "") .replaceAll("data-src-address", "src") .replaceAll(/[\u{0000}-\u{001f}]/gu, "") .replaceAll(/[\u{007f}-\u{009f}]/gu, "") @@ -799,7 +798,7 @@ export class EPUB extends Options { chapter.chapterHtmlFileName = chapterHtmlFileName; log.debug(`[save-epub]保存章HTML文件:${chapterName}`); - const chapterHTMLBlob = EPUB.genChapterHtmlFile(chapter); + const chapterHTMLBlob = this.genChapterHtmlFile(chapter); await this.epubZip.file(`OEBPS/${chapterHtmlFileName}`, chapterHTMLBlob); const item = this.contentOpf.createElement("item"); diff --git a/src/save/options.ts b/src/save/options.ts index 63c92ee5..d389d3b2 100644 --- a/src/save/options.ts +++ b/src/save/options.ts @@ -35,6 +35,7 @@ export interface SaveOptions { getchapterName?: Options["getchapterName"]; genSectionText?: Options["genSectionText"]; genChapterText?: Options["genChapterText"]; + genChapterEpub?: Options["genChapterEpub"]; chapterSort?: Options["chapterSort"]; } @@ -44,6 +45,7 @@ export function saveOptionsValidate(data: any) { "getchapterName", "genSectionText", "genChapterText", + "genChapterEpub", "chapterSort", ]; @@ -114,6 +116,10 @@ export class Options extends Common { )}\n\n${contentText}\n\n`; } + public genChapterEpub(contentXHTML: string) { + return contentXHTML; + } + public chapterSort(a: Chapter, b: Chapter) { return a.chapterNumber - b.chapterNumber; } diff --git a/src/ui/setting.ts b/src/ui/setting.ts index 169a67c1..1137554c 100644 --- a/src/ui/setting.ts +++ b/src/ui/setting.ts @@ -81,6 +81,16 @@ export const vm = createApp({ }, }, }, + { + key: "epub_space", + value: "epub文档删除章节空行", + options: { + genChapterEpub: (contentXHTML) => { + return contentXHTML.replaceAll("


", "") + .replaceAll("


", ""); + }, + }, + }, { key: "reverse_chapters", value: "保存章节时倒序排列", @@ -116,16 +126,18 @@ export const vm = createApp({ setting.chooseSaveOption = GM_getValue('chooseSaveOption', 'null'); setting.filterSetting = GM_getValue('filterSetting', undefined); setting.currentTab = GM_getValue('currentTab', 'tab-1'); - + let isOverWriteSaveOptions = false; const curSaveOption = () => { const _s = saveOptions.find((s) => s.key === setting.chooseSaveOption); if (_s) { + isOverWriteSaveOptions = true; return _s.options; } else { return saveOptions[0].options; } }; - (unsafeWindow as UnsafeWindow).saveOptions = curSaveOption(); + if (isOverWriteSaveOptions) + (unsafeWindow as UnsafeWindow).saveOptions = curSaveOption(); const saveFilter = (filterSetting: filterSettingGlobal) => { setting.filterSetting = deepcopy(filterSetting); GM_setValue('filterSetting', setting.filterSetting);