Skip to content

Commit

Permalink
fix #692
Browse files Browse the repository at this point in the history
  • Loading branch information
ldm0206 committed Nov 19, 2024
1 parent 3773718 commit 6901e0a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 32 deletions.
53 changes: 28 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;
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;
}
```

Expand Down Expand Up @@ -521,7 +510,17 @@ const saveOptions = {
};
window.saveOptions = saveOptions;
```
epub 文档删除章节空行

```javascript
const saveOptions = {
genChapterEpub: (contentXHTML) => {
return contentXHTML.replaceAll("<p><br /></p>", "")
.replaceAll("<p><br/></p>", "");
},
};
window.saveOptions = saveOptions;
```
保存章节时倒序排列

```javascript
Expand Down Expand Up @@ -637,6 +636,10 @@ window.customFinishCallback = customFinishCallback;
return `${chapter.chapterNumber.toString()}`;
} // 按 第i章 XXX 命名章节名字
},
genChapterEpub: (contentXHTML) => {
return contentXHTML.replaceAll("<p><br /></p>", "")
.replaceAll("<p><br/></p>", "");
},
};
//保存设置结束

Expand Down
9 changes: 4 additions & 5 deletions src/save/epub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[
`<?xml version="1.0" encoding="utf-8"?>`,
htmlText
.replaceAll("<p><br /></p>", "")
.replaceAll("<p><br/></p>", "")
.replaceAll("data-src-address", "src")
.replaceAll(/[\u{0000}-\u{001f}]/gu, "")
.replaceAll(/[\u{007f}-\u{009f}]/gu, "")
Expand Down Expand Up @@ -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");
Expand Down
6 changes: 6 additions & 0 deletions src/save/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface SaveOptions {
getchapterName?: Options["getchapterName"];
genSectionText?: Options["genSectionText"];
genChapterText?: Options["genChapterText"];
genChapterEpub?: Options["genChapterEpub"];
chapterSort?: Options["chapterSort"];
}

Expand All @@ -44,6 +45,7 @@ export function saveOptionsValidate(data: any) {
"getchapterName",
"genSectionText",
"genChapterText",
"genChapterEpub",
"chapterSort",
];

Expand Down Expand Up @@ -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;
}
Expand Down
16 changes: 14 additions & 2 deletions src/ui/setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ export const vm = createApp({
},
},
},
{
key: "epub_space",
value: "epub文档删除章节空行",
options: {
genChapterEpub: (contentXHTML) => {
return contentXHTML.replaceAll("<p><br /></p>", "")
.replaceAll("<p><br/></p>", "");
},
},
},
{
key: "reverse_chapters",
value: "保存章节时倒序排列",
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 6901e0a

Please sign in to comment.