Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add toggle to save read meta info to frontmatter #322

Merged
merged 3 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-weread-plugin",
"name": "Weread",
"version": "0.10.0",
"version": "0.11.0",
"minAppVersion": "0.12.0",
"description": "This is obsidian plugin for Tencent weread.",
"author": "hankzhao",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-weread-plugin",
"version": "0.10.0",
"version": "0.11.0",
"description": "This is a community plugin for tencent weread (https://r.qq.com)",
"main": "main.ts",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/assets/templateInstructions.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ <h2>可用变量</h2>
<li><span class="u-pop">{{isbn}}</span> - ISBN</li>
<li><span class="u-pop">{{category}}</span> - 分类</li>
<li><span class="u-pop">{{publisher}}</span> - 出版社</li>
<li><span class="u-pop">{{finish}}</span> - 是否读完(还未支持)</li>
<li><span class="u-pop">{{readInfo}}</span> - 阅读信息, 点击查看详情</li>
</ul>
划线变量(chapterHighlights)
<ul>
Expand Down
22 changes: 20 additions & 2 deletions src/parser/parseResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const parseHighlights = (
const chapterInfo = highlightData.chapters
.filter((chapter) => chapter.chapterUid === highlight.chapterUid)
.first();
const intentMarkText = addIndentToParagraphs(highlight.markText)
return {
bookmarkId: highlight.bookmarkId?.replace(/_/gi, '-'),
created: highlight.createTime,
Expand All @@ -78,12 +79,29 @@ export const parseHighlights = (
style: highlight.style,
colorStyle: highlight.colorStyle,
chapterTitle: chapterInfo?.title || '未知章节',
markText: highlight.markText,
reviewContent: reviewContent
markText: intentMarkText,
reviewContent: addIndentToParagraphs(reviewContent)
};
});
};


const addIndentToParagraphs = (content: string): string =>{
if(content === undefined || content == ""){
return content;
}
// 将字符串按换行符分割成段落数组
const paragraphs = content.split('\n');

// 遍历段落数组,从第二个段落开始前面加上两个空格
for (let i = 1; i < paragraphs.length; i++) {
paragraphs[i] = ' ' + paragraphs[i];
}

// 将段落数组重新组合成一个字符串
return paragraphs.join('\n');
}

export const parseArticleHighlightReview = (
chapters: Chapter[],
highlights: Highlight[],
Expand Down
12 changes: 12 additions & 0 deletions src/settingTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class WereadSettingsTab extends PluginSettingTab {
this.subFolderType();
this.convertTagToggle();
this.saveArticleToggle();
this.saveReadingInfoToggle();
this.showEmptyChapterTitleToggle();
this.dailyNotes();
const dailyNotesToggle = get(settingsStore).dailyNotesToggle;
Expand Down Expand Up @@ -128,6 +129,17 @@ export class WereadSettingsTab extends PluginSettingTab {
});
});
}
private saveReadingInfoToggle(): void {
new Setting(this.containerEl)
.setName('保存阅读元数据?')
.setDesc('开启此选项会阅读数据写入frontmatter')
.addToggle((toggle) => {
return toggle.setValue(get(settingsStore).saveReadingInfoToggle).onChange((value) => {
settingsStore.actions.setSaveReadingInfoToggle(value);
this.display();
});
});
}
private convertTagToggle(): void {
new Setting(this.containerEl)
.setName('将标签转换为双链?')
Expand Down
14 changes: 12 additions & 2 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ interface WereadPluginSettings {
showEmptyChapterTitleToggle: boolean;
convertTags: boolean;
saveArticleToggle: boolean;
saveReadingInfoToggle: boolean;
}

const DEFAULT_SETTINGS: WereadPluginSettings = {
Expand All @@ -44,7 +45,9 @@ const DEFAULT_SETTINGS: WereadPluginSettings = {
notesBlacklist: '',
showEmptyChapterTitleToggle: false,
convertTags: false,
saveArticleToggle: true
saveArticleToggle: true,
saveReadingInfoToggle: true,

};

const createSettingsStore = () => {
Expand Down Expand Up @@ -218,6 +221,12 @@ const createSettingsStore = () => {
});
};

const setSaveReadingInfoToggle = (saveReadingInfoToggle: boolean) => {
store.update((state) => {
state.saveReadingInfoToggle = saveReadingInfoToggle;
return state;
});
};
return {
subscribe: store.subscribe,
initialise,
Expand All @@ -237,7 +246,8 @@ const createSettingsStore = () => {
setNoteBlacklist,
setEmptyChapterTitleToggle,
setConvertTags,
setSaveArticleToggle
setSaveArticleToggle,
setSaveReadingInfoToggle,
}
};
};
Expand Down
37 changes: 23 additions & 14 deletions src/utils/frontmatter.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { Notice, parseYaml, stringifyYaml, TFile } from 'obsidian';
import type { Notebook } from '../models';
import { formatTimeDuration, formatTimestampToDate } from './dateUtil';
import { settingsStore } from '../settings';
import { get } from 'svelte/store';

type FrontMatterContent = {
doc_type: string;
bookId: string;
author: string;
cover: string;
noteCount: number;
reviewCount: number;
author?: string;
cover?: string;
readingStatus?: string;
progress?: string;
readingTime?: string;
Expand All @@ -34,24 +36,31 @@ export const buildFrontMatter = (
const frontMatter: FrontMatterContent = {
doc_type: frontMatterDocType,
bookId: noteBook.metaData.bookId,
author: noteBook.metaData.author,
cover: noteBook.metaData.cover,
reviewCount: noteBook.metaData.reviewCount,
noteCount: noteBook.metaData.noteCount
};

const readInfo = noteBook.metaData.readInfo;

if (readInfo) {
frontMatter.readingStatus = ReadingStatus[readInfo.markedStatus];
frontMatter.progress =
readInfo.readingProgress === undefined ? '-1' : readInfo.readingProgress + '%';
frontMatter.totalReadDay = readInfo.totalReadDay;
frontMatter.readingTime = formatTimeDuration(readInfo.readingTime);
frontMatter.readingDate = formatTimestampToDate(readInfo.readingBookDate);
if (readInfo.finishedDate) {
frontMatter.finishedDate = formatTimestampToDate(readInfo.finishedDate);
const saveReadingInfoToggle = get(settingsStore).saveReadingInfoToggle;

if (saveReadingInfoToggle) {
frontMatter.author = noteBook.metaData.author,
frontMatter.cover = noteBook.metaData.cover

const readInfo = noteBook.metaData.readInfo;
if (readInfo) {
frontMatter.readingStatus = ReadingStatus[readInfo.markedStatus];
frontMatter.progress =
readInfo.readingProgress === undefined ? '-1' : readInfo.readingProgress + '%';
frontMatter.totalReadDay = readInfo.totalReadDay;
frontMatter.readingTime = formatTimeDuration(readInfo.readingTime);
frontMatter.readingDate = formatTimestampToDate(readInfo.readingBookDate);
if (readInfo.finishedDate) {
frontMatter.finishedDate = formatTimestampToDate(readInfo.finishedDate);
}

}

}
let existFrontMatter = Object();
if (existFile) {
Expand Down
Loading