Skip to content

Commit

Permalink
feat: 添加发现页功能
Browse files Browse the repository at this point in the history
  • Loading branch information
aooiuu committed Mar 27, 2024
1 parent 7e6c024 commit fe49ba3
Show file tree
Hide file tree
Showing 14 changed files with 301 additions and 17 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[![satr][satr-src]][satr-href]
[![issues][issues-src]][issues-href]

多站点自定义规则聚合阅读工具, 支持小说、漫画。包含 [JS 解析库](https://aooiuu.github.io/any-reader/core/)[VSCode 插件](https://aooiuu.github.io/any-reader/vsc/)
多站点自定义规则聚合阅读工具, 支持小说、漫画、视频。包含 [JS 解析库](https://aooiuu.github.io/any-reader/core/)[VSCode 插件](https://aooiuu.github.io/any-reader/vsc/)

> 软件不提供内容, 也不提供任何规则, 但你可以通过编写 [规则](https://aooiuu.github.io/any-reader/rule/), 控制呈现内容, [查看文档](https://aooiuu.github.io/any-reader/)
Expand All @@ -33,6 +33,14 @@

![2](https://github.com/aooiuu/any-reader/assets/28108111/ed5544d6-ec4f-4b52-a75a-a1f618b8383d)

### 发现页

![1](https://github.com/aooiuu/any-reader/assets/28108111/23d81ce8-4de7-4e1b-b668-df5dd384c907)

![1](https://github.com/aooiuu/any-reader/assets/28108111/8f85e896-3001-44cd-8c14-28e7140d10a1)

![1](https://github.com/aooiuu/any-reader/assets/28108111/01eabe01-84eb-4113-a10e-fdfd9a82f169)

<!-- Badges -->

[vsc-src]: https://img.shields.io/visual-studio-marketplace/v/aooiu.any-reader
Expand Down
8 changes: 8 additions & 0 deletions docs/vsc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ outline: deep

![2](https://github.com/aooiuu/any-reader/assets/28108111/ed5544d6-ec4f-4b52-a75a-a1f618b8383d)

### 发现页

![1](https://github.com/aooiuu/any-reader/assets/28108111/23d81ce8-4de7-4e1b-b668-df5dd384c907)

![1](https://github.com/aooiuu/any-reader/assets/28108111/8f85e896-3001-44cd-8c14-28e7140d10a1)

![1](https://github.com/aooiuu/any-reader/assets/28108111/01eabe01-84eb-4113-a10e-fdfd9a82f169)

## 使用方式

1. 插件市场搜索 `any-reader` 安装 (安装后, `VSCode` 侧边栏会出现插件入口)
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@any-reader/core",
"version": "0.1.0-alpha.5",
"version": "0.2.0",
"description": "",
"type": "module",
"main": "index.js",
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/analyzer/AnalyzerJSONPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ export class AnalyzerJSONPath implements Analyzer {
}

async getElements(rule: string) {
return JSONPath({
const rows = JSONPath({
path: rule,
json: this._content,
})
if (Array.isArray(rows) && rows.length === 1)
return rows[0]

return rows
}
}
74 changes: 74 additions & 0 deletions packages/core/src/analyzer/RuleManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,11 @@ const http = axios.create()

export class RuleManager {
rule: Rule
_nextUrl: Map<string, string>

constructor(rule: Rule) {
this.rule = rule
this._nextUrl = new Map()
}

/**
Expand Down Expand Up @@ -249,6 +251,7 @@ export class RuleManager {
return list
}

// 获取获取分类
async discoverMap() {
const map = []
const table = new Map()
Expand Down Expand Up @@ -320,6 +323,77 @@ export class RuleManager {

return map
}

// 获取分类下内容
async discover(url: string, page = 1) {
const hasNextUrlRule = this.rule.discoverNextUrl !== undefined && this.rule.discoverNextUrl.length > 0
let discoverRule

if (page === 1) {
discoverRule = url
}
else if (hasNextUrlRule && page > 1) {
const next = this._nextUrl.get(url)
if (next !== undefined && next.length > 0)
discoverRule = next
}
else if (/(\$page)|((^|[^a-zA-Z'"_/-])page([^a-zA-Z0-9'"]|$))/.test(url)) {
discoverRule = url
}

if (!discoverRule)
return []

const discoverUrl = ''
let body = ''

if (discoverRule !== 'null') {
const { body: res } = await this.fetch(discoverRule)
body = res
}

JSEngine.setEnvironment({
page,
rule: this.rule,
result: discoverUrl,
baseUrl: this.rule.host,
keyword: '',
lastResult: '',
})

const bodyAnalyzer = new AnalyzerManager(body)
if (hasNextUrlRule)
this._nextUrl.set(url, await bodyAnalyzer.getString(this.rule.discoverNextUrl as string))
else
this._nextUrl.delete(url)

const list = await bodyAnalyzer.getElements(this.rule.discoverList)
const result = []

for (const item of list) {
const analyzer = new AnalyzerManager(item)
const tag = await analyzer.getString(this.rule.discoverTags)

let tags: string[] = []
if (tag !== undefined && tag.trim() !== '') {
tags = tag.split(' ')
.filter(tag => tag !== '')
}

result.push({
searchUrl: discoverUrl,
cover: await analyzer.getString(this.rule.discoverCover),
name: await analyzer.getString(this.rule.discoverName),
author: await analyzer.getString(this.rule.discoverAuthor),
chapter: await analyzer.getString(this.rule.discoverChapter),
description: await analyzer.getString(this.rule.discoverDescription),
url: await analyzer.getString(this.rule.discoverResult),
tags,
})
}

return result
}
}

class DiscoverMap {
Expand Down
4 changes: 4 additions & 0 deletions packages/vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 0.4.0 (2024-03-27)

- 添加发现页

## 0.3.0 (2024-03-25)

- 添加聚合搜索
Expand Down
8 changes: 8 additions & 0 deletions packages/vscode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,12 @@

![2](https://github.com/aooiuu/any-reader/assets/28108111/ed5544d6-ec4f-4b52-a75a-a1f618b8383d)

### 发现页

![1](https://github.com/aooiuu/any-reader/assets/28108111/23d81ce8-4de7-4e1b-b668-df5dd384c907)

![1](https://github.com/aooiuu/any-reader/assets/28108111/8f85e896-3001-44cd-8c14-28e7140d10a1)

![1](https://github.com/aooiuu/any-reader/assets/28108111/01eabe01-84eb-4113-a10e-fdfd9a82f169)

点个 `star` 可以关注插件更新动态, 也欢迎大家提交 `PR`.
24 changes: 12 additions & 12 deletions 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.3.0",
"version": "0.4.0",
"preview": true,
"publisher": "aooiu",
"qna": "https://github.com/aooiuu/any-reader/issues",
Expand Down Expand Up @@ -47,12 +47,6 @@
},
"contributes": {
"commands": [
{
"command": "any-reader.editBookSource",
"title": "书源编辑",
"icon": "$(settings-edit)",
"category": "any-reader"
},
{
"command": "any-reader.getBookSource",
"title": "刷新书源",
Expand All @@ -71,6 +65,12 @@
"icon": "$(search)",
"category": "any-reader"
},
{
"command": "any-reader.discover",
"title": "discover",
"icon": "$(flame)",
"category": "any-reader"
},
{
"command": "any-reader.home",
"title": "Home",
Expand Down Expand Up @@ -108,13 +108,13 @@
"menus": {
"view/title": [
{
"command": "any-reader.searchBook",
"when": "view == any-reader-book || view == any-reader-source",
"command": "any-reader.discover",
"when": "view == any-reader-source",
"group": "navigation"
},
{
"command": "any-reader.editBookSource",
"when": "view == any-reader-book || view == any-reader-source",
"command": "any-reader.searchBook",
"when": "view == any-reader-source",
"group": "navigation"
},
{
Expand All @@ -124,7 +124,7 @@
},
{
"command": "any-reader.home",
"when": "view == any-reader-book",
"when": "view == any-reader-source",
"group": "navigation"
}
],
Expand Down
8 changes: 7 additions & 1 deletion packages/vscode/src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ class App {
vscode.window.registerFileDecorationProvider(treeItemDecorationProvider),
registerCommand(COMMANDS.editBookSource, this.editBookSource, this),
registerCommand(COMMANDS.searchBook, this.searchBook, this),
registerCommand(COMMANDS.discover, this.discover, this),
registerCommand(COMMANDS.searchBookByRule, this.searchBookByRule, this),
registerCommand(COMMANDS.getContent, this.getContent, this),
registerCommand(COMMANDS.home, () => this.webView.navigateTo('/'), this.webView),
registerCommand(COMMANDS.getBookSource, this.getBookSource, this),
registerCommand(COMMANDS.gamePlay, (node: any) => this.webView.navigateTo('/iframe?url=' + node.host, node.name), this.webView)
].forEach((command) => context.subscriptions.push(command));
vscode.window.createTreeView('any-reader-book', { treeDataProvider: bookProvider });
vscode.window.createTreeView('any-reader-source', { treeDataProvider: sourceProvider });
vscode.window.createTreeView('any-reader-book', { treeDataProvider: bookProvider });
vscode.commands.executeCommand(COMMANDS.getBookSource);
}

Expand All @@ -45,6 +46,11 @@ class App {
this.webView.navigateTo('/search');
}

// 发现页
discover() {
this.webView.navigateTo('/discover');
}

async searchBookByRule(rule: Rule) {
await bookManager.searchBook(rule);
bookProvider.refresh();
Expand Down
1 change: 1 addition & 0 deletions packages/vscode/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export enum COMMANDS {
searchBook = 'any-reader.searchBook',
searchBookByRule = 'any-reader.searchBookByRule',
getContent = 'any-reader.getContent',
discover = 'any-reader.discover',

// 读取所有书源
getBookSource = 'any-reader.getBookSource',
Expand Down
32 changes: 31 additions & 1 deletion packages/vscode/src/webview/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,43 @@ export class WebView {
bookProvider.refresh();
}
break;

case 'discover':
{
const { rule, data: params } = data;
const ruleManager = new RuleManager(rule);
const res = await ruleManager.discover(params.value);
this.webviewPanel!.webview.postMessage({
type,
data: {
rule,
data: res
}
});
}
break;
case 'discoverMap':
{
const { rule } = data;
const ruleManager = new RuleManager(rule);
const res = await ruleManager.discoverMap();
this.webviewPanel!.webview.postMessage({
type,
data: {
rule,
data: res
}
});
}
break;

default:
break;
}
});
}

async navigateTo(routePath = '', title = 'Home') {
async navigateTo(routePath = '', title = 'AnyReader') {
this.initWebviewPanel(title);
if (!this.isVue) {
this.webviewPanel!.webview.html = this.getWebViewContent(path.join('template-dist', 'index.html'));
Expand Down
39 changes: 39 additions & 0 deletions packages/web/src/pages/discover/Category.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<template>
<div class="w-full">
<a-radio-group :model-value="value" type="button" class="w-full overflow-auto" @update:model-value="onInput">
<a-radio v-for="item in props.list" :key="item.name" :value="item.name" class="block flex-shrink-0">{{ item.name }}</a-radio>
</a-radio-group>

<Category v-if="nextList.length > 1" :list="nextList" @change="(row) => emit('change', row)" />
</div>
</template>

<script setup lang="ts">
const props = defineProps<{
list: any[];
}>();
const emit = defineEmits(['change']);
const value = ref<string>('');
const nextList = ref([]);
function onInput(v: any) {
value.value = v;
const row = props.list.find((e) => e.name === value.value);
nextList.value = row.pairs || [];
if (nextList.value.length === 0) {
emit('change', row);
}
}
watch(
() => props.list,
(v) => {
onInput(v?.[0]?.name || '');
},
{
immediate: true
}
);
</script>
Loading

0 comments on commit fe49ba3

Please sign in to comment.