mirror of
https://github.com/flyer1b/LightRead-master.git
synced 2026-07-22 09:00:26 +00:00
1.1.0
This commit is contained in:
parent
48777bd5bb
commit
d1ec6fffa9
6 changed files with 259 additions and 154 deletions
152
main.js
152
main.js
File diff suppressed because one or more lines are too long
245
main.ts
245
main.ts
|
|
@ -1,4 +1,3 @@
|
|||
import { time } from 'console';
|
||||
import { App, Notice, Plugin, PluginSettingTab, Setting,Vault,TFile, normalizePath, requestUrl, FileManager } from 'obsidian';
|
||||
|
||||
// Remember to rename these classes and interfaces!
|
||||
|
|
@ -8,6 +7,7 @@ interface SyncReadPluginSettings {
|
|||
password: string;
|
||||
token: string;
|
||||
time: string;
|
||||
noteTime: string;
|
||||
apiKey: string;
|
||||
}
|
||||
|
||||
|
|
@ -16,6 +16,7 @@ const DEFAULT_SETTINGS: SyncReadPluginSettings = {
|
|||
password: '',
|
||||
token: '',
|
||||
time: '2020-01-01 00:00:00',
|
||||
noteTime: '2024-03-09T10:37:49',
|
||||
apiKey: ''
|
||||
}
|
||||
|
||||
|
|
@ -28,14 +29,15 @@ export default class SyncReadPlugin extends Plugin {
|
|||
await this.loadSettings();
|
||||
|
||||
// This creates an icon in the left ribbon.
|
||||
const ribbonIconEl = this.addRibbonIcon('sync', 'SyncRead', (evt: MouseEvent) => {
|
||||
const ribbonIconEl = this.addRibbonIcon('sync', 'SyncRead',async (evt: MouseEvent) => {
|
||||
// Called when the user clicks the icon.
|
||||
// new Notice('This is a notice!');
|
||||
// new Notice(`path: ${this.app.vault.getRoot().path}`);
|
||||
// craeteFile(this.app.vault);
|
||||
// new Notice(`token: ${this.settings.token}`);
|
||||
writeAllArticles(this.app,this);
|
||||
});
|
||||
await writeAllArticles(this.app,this);
|
||||
writeAllNote(this.app,this);
|
||||
});
|
||||
|
||||
|
||||
// This adds a settings tab so the user can configure various aspects of the plugin
|
||||
|
|
@ -75,69 +77,69 @@ class SyncReadSettingTab extends PluginSettingTab {
|
|||
|
||||
containerEl.empty();
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName('用户名')
|
||||
.setDesc('输入你在SyncRead注册的邮箱')
|
||||
.addText(text => text
|
||||
.setPlaceholder('请输入邮箱')
|
||||
.setValue(this.plugin.settings.email)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.email = value;
|
||||
await this.plugin.saveSettings();
|
||||
}));
|
||||
new Setting(containerEl)
|
||||
.setName('密码')
|
||||
.setDesc('')
|
||||
.addText(text => text
|
||||
.setPlaceholder('请输入密码')
|
||||
.setValue(this.plugin.settings.password)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.password = value;
|
||||
await this.plugin.saveSettings();
|
||||
}));
|
||||
new Setting(containerEl)
|
||||
.addButton(button => {
|
||||
button
|
||||
.setButtonText('登录')
|
||||
.onClick(async () => {
|
||||
button.disabled = true;
|
||||
button.setButtonText('登录中...');
|
||||
const {email, password} = this.plugin.settings;
|
||||
// new Notice(`email: ${email}`);
|
||||
try{
|
||||
const res = await requestUrl({
|
||||
url:'http://43.138.149.38:5050/login/',
|
||||
method: 'POST',
|
||||
contentType: 'application/json',
|
||||
body: JSON.stringify({
|
||||
'username':email,
|
||||
'password':password
|
||||
// new Setting(containerEl)
|
||||
// .setName('用户名')
|
||||
// .setDesc('输入你在SyncRead注册的邮箱')
|
||||
// .addText(text => text
|
||||
// .setPlaceholder('请输入邮箱')
|
||||
// .setValue(this.plugin.settings.email)
|
||||
// .onChange(async (value) => {
|
||||
// this.plugin.settings.email = value;
|
||||
// await this.plugin.saveSettings();
|
||||
// }));
|
||||
// new Setting(containerEl)
|
||||
// .setName('密码')
|
||||
// .setDesc('')
|
||||
// .addText(text => text
|
||||
// .setPlaceholder('请输入密码')
|
||||
// .setValue(this.plugin.settings.password)
|
||||
// .onChange(async (value) => {
|
||||
// this.plugin.settings.password = value;
|
||||
// await this.plugin.saveSettings();
|
||||
// }));
|
||||
// new Setting(containerEl)
|
||||
// .addButton(button => {
|
||||
// button
|
||||
// .setButtonText('登录')
|
||||
// .onClick(async () => {
|
||||
// button.disabled = true;
|
||||
// button.setButtonText('登录中...');
|
||||
// const {email, password} = this.plugin.settings;
|
||||
// // new Notice(`email: ${email}`);
|
||||
// try{
|
||||
// const res = await requestUrl({
|
||||
// url:'http://43.138.149.38:5050/login/',
|
||||
// method: 'POST',
|
||||
// contentType: 'application/json',
|
||||
// body: JSON.stringify({
|
||||
// 'username':email,
|
||||
// 'password':password
|
||||
|
||||
})});
|
||||
if (res.status !== 200) {
|
||||
new Notice('登录失败');
|
||||
button.disabled = false;
|
||||
button.setButtonText('登录');
|
||||
return;
|
||||
}
|
||||
const data = await res.json;
|
||||
// new Notice(`res: ${JSON.stringify(data)}`);
|
||||
this.plugin.settings.token = data.data.token;
|
||||
await this.plugin.saveSettings();
|
||||
new Notice('登录成功');
|
||||
button.setButtonText('登录成功');
|
||||
button.disabled = false;
|
||||
}catch(error){
|
||||
new Notice('登录失败: ' + error.message);
|
||||
button.disabled = false;
|
||||
button.setButtonText('登录');
|
||||
return;
|
||||
}
|
||||
});
|
||||
})
|
||||
// })});
|
||||
// if (res.status !== 200) {
|
||||
// new Notice('登录失败');
|
||||
// button.disabled = false;
|
||||
// button.setButtonText('登录');
|
||||
// return;
|
||||
// }
|
||||
// const data = await res.json;
|
||||
// // new Notice(`res: ${JSON.stringify(data)}`);
|
||||
// this.plugin.settings.token = data.data.token;
|
||||
// await this.plugin.saveSettings();
|
||||
// new Notice('登录成功');
|
||||
// button.setButtonText('登录成功');
|
||||
// button.disabled = false;
|
||||
// }catch(error){
|
||||
// new Notice('登录失败: ' + error.message);
|
||||
// button.disabled = false;
|
||||
// button.setButtonText('登录');
|
||||
// return;
|
||||
// }
|
||||
// });
|
||||
// })
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName('api key(可选)')
|
||||
.setName('api key')
|
||||
.addText(text => text
|
||||
.setPlaceholder('请输入api key')
|
||||
.setValue(this.plugin.settings.apiKey)
|
||||
|
|
@ -156,15 +158,8 @@ async function fetchArticle(settings: SyncReadPluginSettings,time: string,page:
|
|||
const apiKey = settings.apiKey;
|
||||
let res;
|
||||
if(apiKey === ''){
|
||||
res = await requestUrl(
|
||||
{
|
||||
url:`http://43.138.149.38:5050/get_synced_articles?time=${time}&page_number=${page}&page_size=50`,
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Authorization': 'Bearer ' + settings.token
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
new Notice('请填写api key');
|
||||
}else{
|
||||
res = await requestUrl(
|
||||
{
|
||||
|
|
@ -175,16 +170,52 @@ async function fetchArticle(settings: SyncReadPluginSettings,time: string,page:
|
|||
},
|
||||
}
|
||||
);
|
||||
if (res.status !== 200) {
|
||||
new Notice('同步文章失败');
|
||||
isWriting = false;
|
||||
return;
|
||||
}
|
||||
const data = await res.json;
|
||||
// new Notice(`res: ${data.data.length}`);
|
||||
return data.data;
|
||||
}
|
||||
// new Notice(`token: ${settings.token}`);
|
||||
if (res.status !== 200) {
|
||||
new Notice('同步失败');
|
||||
isWriting = false;
|
||||
return;
|
||||
|
||||
} catch (error) {
|
||||
new Notice('同步失败: ' + error.message);
|
||||
isWriting = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchNote(settings: SyncReadPluginSettings,time: string,page: number){
|
||||
|
||||
try {
|
||||
|
||||
const apiKey = settings.apiKey;
|
||||
let res;
|
||||
if(apiKey === ''){
|
||||
|
||||
new Notice('请填写api key');
|
||||
}else{
|
||||
res = await requestUrl(
|
||||
{
|
||||
url:`http://43.138.149.38:5050/get_notes_by_key?time=${time}&page_number=${page}&page_size=50`,
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'api_key': apiKey
|
||||
},
|
||||
}
|
||||
);
|
||||
if (res.status !== 200) {
|
||||
new Notice('同步标注失败');
|
||||
isWriting = false;
|
||||
return;
|
||||
}
|
||||
const data = await res.json;
|
||||
// new Notice(`res: ${data.data.length}`);
|
||||
return data.data;
|
||||
}
|
||||
const data = await res.json;
|
||||
// new Notice(`res: ${data.data.length}`);
|
||||
return data.data;
|
||||
} catch (error) {
|
||||
new Notice('同步失败: ' + error.message);
|
||||
isWriting = false;
|
||||
|
|
@ -195,7 +226,7 @@ async function fetchArticle(settings: SyncReadPluginSettings,time: string,page:
|
|||
async function writeArticle(vault: Vault,fileManager:FileManager, article: { title: string, content: string ,site:string}): Promise<void> {
|
||||
var folder = vault.getAbstractFileByPath('SyncRead同步文件夹');
|
||||
if(!folder) folder = await vault.createFolder('SyncRead同步文件夹');
|
||||
new Notice('url: ' + article.site);
|
||||
// new Notice('url: ' + article.site);
|
||||
let file = await vault.create(folder.path+'/'+filterIllegalChars(normalizePath(article.title)) + ".md", article.content);
|
||||
return fileManager.processFrontMatter(file,(frontMatter) => {
|
||||
frontMatter['created_at'] = new Date().toISOString();
|
||||
|
|
@ -203,6 +234,56 @@ async function writeArticle(vault: Vault,fileManager:FileManager, article: { tit
|
|||
});
|
||||
}
|
||||
|
||||
async function writeNote(vault: Vault,fileManager:FileManager, note: {id:string; text: string; annotation: string;articleTitle:string;update_at:string }): Promise<void> {
|
||||
var folder = vault.getAbstractFileByPath('SyncRead同步文件夹');
|
||||
if(!folder) folder = await vault.createFolder('SyncRead同步文件夹');
|
||||
var noteFolder = vault.getAbstractFileByPath(folder.path+'/标注')
|
||||
if(!noteFolder) noteFolder = await vault.createFolder(folder.path+'/标注');
|
||||
let noteFile = vault.getAbstractFileByPath(noteFolder.path+'/[标注]'+filterIllegalChars(normalizePath(note.articleTitle)) + ".md");
|
||||
if(!noteFile) {
|
||||
noteFile = await vault.create(noteFolder.path+'/[标注]'+filterIllegalChars(normalizePath(note.articleTitle)) + ".md", "\n");
|
||||
fileManager.processFrontMatter(noteFile as TFile,(frontMatter) => {
|
||||
frontMatter['originArticle'] = `[[${filterIllegalChars(normalizePath(note.articleTitle))}]]`;
|
||||
});
|
||||
}
|
||||
let noteText = note.text.replace(/\n+$/, ""); // Remove trailing newlines
|
||||
|
||||
let noteTextWithQuotes = "\n\n\n>" + noteText.replace(/\n/g, "\n>");
|
||||
new Notice(noteTextWithQuotes);
|
||||
return vault.append(noteFile as TFile,noteTextWithQuotes+"\n\n"+(note.annotation!==null?note.annotation:""));
|
||||
|
||||
}
|
||||
|
||||
async function writeAllNote(app: App,plugin: SyncReadPlugin){
|
||||
let vault = app.vault;
|
||||
let fileManager = app.fileManager;
|
||||
if(isWriting) return;
|
||||
isWriting = true;
|
||||
var time = plugin.settings.noteTime;
|
||||
var page = 1;
|
||||
do{
|
||||
|
||||
const notes = await fetchNote(plugin.settings,time,page);
|
||||
if (notes.length === 0) {
|
||||
isWriting = false;
|
||||
new Notice('同步标注完成');
|
||||
break;
|
||||
}
|
||||
try{
|
||||
await Promise.all(notes.map((note: {id:string; text: string; annotation: string;articleTitle:string;update_at:string }) => writeNote(vault, fileManager,note)));
|
||||
}catch(error){
|
||||
// new Notice(error.message);
|
||||
// isWriting = false;
|
||||
// return;
|
||||
}
|
||||
// new Notice(`page: ${page}`);
|
||||
plugin.settings.noteTime = notes[notes.length-1].update_at;
|
||||
plugin.saveSettings();
|
||||
page++;
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
// new Notice(`page: ${page}`);
|
||||
}while(true);
|
||||
}
|
||||
async function writeAllArticles(app: App,plugin: SyncReadPlugin){
|
||||
|
||||
let vault = app.vault;
|
||||
|
|
@ -218,7 +299,7 @@ async function writeAllArticles(app: App,plugin: SyncReadPlugin){
|
|||
const articles = await fetchArticle(plugin.settings,time,page);
|
||||
if (articles.length === 0) {
|
||||
isWriting = false;
|
||||
new Notice('同步完成');
|
||||
new Notice('同步文章完成');
|
||||
break;
|
||||
}
|
||||
try{
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"id": "syncread",
|
||||
"name": "syncread-assistant",
|
||||
"version": "1.0.3",
|
||||
"version": "1.1.0",
|
||||
"minAppVersion": "0.15.0",
|
||||
"description": "sync articles from syncread app",
|
||||
"author": "flyer1b",
|
||||
|
|
|
|||
8
package-lock.json
generated
8
package-lock.json
generated
|
|
@ -17,7 +17,7 @@
|
|||
"@typescript-eslint/parser": "5.29.0",
|
||||
"builtin-modules": "3.3.0",
|
||||
"esbuild": "0.17.3",
|
||||
"obsidian": "latest",
|
||||
"obsidian": "^1.5.7-1",
|
||||
"tslib": "2.4.0",
|
||||
"typescript": "4.7.4"
|
||||
}
|
||||
|
|
@ -1811,9 +1811,9 @@
|
|||
}
|
||||
},
|
||||
"node_modules/obsidian": {
|
||||
"version": "1.4.11",
|
||||
"resolved": "https://registry.npmjs.org/obsidian/-/obsidian-1.4.11.tgz",
|
||||
"integrity": "sha512-BCVYTvaXxElJMl6MMbDdY/CGK+aq18SdtDY/7vH8v6BxCBQ6KF4kKxL0vG9UZ0o5qh139KpUoJHNm+6O5dllKA==",
|
||||
"version": "1.5.7-1",
|
||||
"resolved": "https://registry.npmjs.org/obsidian/-/obsidian-1.5.7-1.tgz",
|
||||
"integrity": "sha512-T5ZRuQ1FnfXqEoakTTHVDYvzUEEoT8zSPnQCW31PVgYwG4D4tZCQfKHN2hTz1ifnCe8upvwa6mBTAP2WUA5Vng==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"@types/codemirror": "5.60.8",
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "syncread-assistant",
|
||||
"version": "1.0.0",
|
||||
"version": "1.1.0",
|
||||
"description": "sync articles from syncread app",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
|
|
@ -17,7 +17,7 @@
|
|||
"@typescript-eslint/parser": "5.29.0",
|
||||
"builtin-modules": "3.3.0",
|
||||
"esbuild": "0.17.3",
|
||||
"obsidian": "latest",
|
||||
"obsidian": "^1.5.7-1",
|
||||
"tslib": "2.4.0",
|
||||
"typescript": "4.7.4"
|
||||
},
|
||||
|
|
|
|||
|
|
@ -1,3 +1,3 @@
|
|||
{
|
||||
"1.0.3": "0.15.0"
|
||||
"1.1.0": "0.15.0"
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue