Skip to content

Commit

Permalink
feat: 支持 epub 格式 (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
aooiuu committed Apr 28, 2024
1 parent 425ee23 commit 5bce67d
Show file tree
Hide file tree
Showing 7 changed files with 420 additions and 28 deletions.
1 change: 1 addition & 0 deletions packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"dependencies": {
"@any-reader/core": "workspace:^",
"encoding-japanese": "^2.1.0",
"epub": "^1.2.1",
"fs-extra": "^11.1.1",
"iconv-lite": "^0.6.3",
"uuid": "^9.0.1"
Expand Down
1 change: 0 additions & 1 deletion packages/shared/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './favoritesManager'
export * from './historyManager'
export * as ruleFileManager from './ruleFileManager'
export * as localBookManager from './localBookManager'
export * as CONSTANTS from './constants'
49 changes: 43 additions & 6 deletions packages/shared/src/localBookManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as path from 'node:path'
import * as fs from 'fs-extra'
import EPub from 'epub'
import Encoding from 'encoding-japanese'
import * as iconv from 'iconv-lite'
import { LOCAL_BOOK_DIR } from './constants'
Expand All @@ -17,16 +18,23 @@ export interface BookFile {

export interface BookChapter {
name: string
path: string
file: BookFile
}

export type TreeNode = BookFile | BookChapter

// 检查目录
export function checkDir() {
if (!fs.existsSync(LOCAL_BOOK_DIR) || !fs.lstatSync(LOCAL_BOOK_DIR).isDirectory())
fs.mkdirSync(LOCAL_BOOK_DIR)
}

// 获取书籍类型
function getBookType(extname: string) {
return extname === '.txt' ? BOOK_TYPE.TXT : BOOK_TYPE.EPUB
}

// 获取所有书籍
export async function getBookList(): Promise<BookFile[]> {
const dir = LOCAL_BOOK_DIR
Expand All @@ -35,30 +43,47 @@ export async function getBookList(): Promise<BookFile[]> {
const files = fs.readdirSync(dir)

return files
.filter(f => ['.txt'].includes(path.extname(f)))
.filter(f => ['.txt', '.epub'].includes(path.extname(f)))
.map((f) => {
return {
type: BOOK_TYPE.TXT,
type: getBookType(path.extname(f)),
name: path.basename(f, path.extname(f)),
path: path.join(dir, f),
}
})
}

// 获取章节
export function getChapter(bookFile: BookFile): BookChapter[] {
export async function getChapter(bookFile: BookFile): Promise<BookChapter[]> {
if (bookFile.type === BOOK_TYPE.TXT) {
return [
{
name: '正文',
path: '',
file: bookFile,
},
]
}
return []

return new Promise((resolve) => {
const book = new EPub(bookFile.path)
book.on('end', () => {
resolve(
book.flow.map((e) => {
return {
name: e.title || e.id,
path: e.id,
file: bookFile,
}
}),
)
})
book.parse()
})
}

// 获取内容
export function getContent(item: BookChapter) {
export async function getContent(item: BookChapter): Promise<string> {
if (item.file.type === BOOK_TYPE.TXT) {
const buffer = fs.readFileSync(item.file.path)
const encoding = Encoding.detect(buffer)
Expand All @@ -67,5 +92,17 @@ export function getContent(item: BookChapter) {
else
return iconv.decode(buffer, 'GB2312')
}
return ''

return new Promise((resolve, reject) => {
const book = new EPub(item.file.path)
book.on('end', () => {
book.getChapter(item.path, (error, text) => {
if (error)
reject(error)

resolve(text)
})
})
book.parse()
})
}
2 changes: 1 addition & 1 deletion packages/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "any-reader",
"description": "any-reader for vscode",
"icon": "resources/icon.png",
"version": "0.6.0",
"version": "0.6.1",
"preview": true,
"publisher": "aooiu",
"qna": "https://github.com/aooiuu/any-reader/issues",
Expand Down
2 changes: 1 addition & 1 deletion packages/vscode/src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class App {
cancellable: false
},
async () => {
const content = getContent(item);
const content = await getContent(item).catch(() => '');
this.openWebviewPanel(item.name, content);
}
);
Expand Down
10 changes: 5 additions & 5 deletions packages/vscode/src/treeview/localBook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class TreeDataProvider implements vscode.TreeDataProvider<TreeNode> {

// 节点展示
getTreeItem(item: TreeNode): vscode.TreeItem {
if ((<BookFile>item).path) {
return this.getTreeItemBookFile(item as BookFile);
} else {
if ((<BookChapter>item).file) {
return this.getTreeItemBookChapter(item as BookChapter);
} else {
return this.getTreeItemBookFile(item as BookFile);
}
}

Expand Down Expand Up @@ -43,9 +43,9 @@ class TreeDataProvider implements vscode.TreeDataProvider<TreeNode> {
}

// 获取目录
async getChildren(item?: TreeNode): Promise<TreeNode[]> {
getChildren(item?: TreeNode): Promise<TreeNode[]> {
if (!item) {
return await getBookList();
return getBookList();
} else {
return getChapter(item as BookFile);
}
Expand Down
Loading

0 comments on commit 5bce67d

Please sign in to comment.