Skip to content

Commit

Permalink
feat: support save official account article
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaohongxuan committed Mar 5, 2024
1 parent 9068dac commit af228d5
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 22 deletions.
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.9.0",
"version": "0.10.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.9.0",
"version": "0.10.0",
"description": "This is a community plugin for tencent weread (https://r.qq.com)",
"main": "main.ts",
"scripts": {
Expand Down
29 changes: 25 additions & 4 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface HighlightResponse {
type: number;
createTime: number;
bookmarkId: string;
refMpReviewId?: string;
}[];
removed: any[];
chapters: {
Expand All @@ -23,6 +24,12 @@ export interface HighlightResponse {
chapterIdx: number;
title: string;
}[];
refMpInfos?: {
reviewId: string;
title: string;
pic_url: string;
createTime: number;
}[];
book: {
bookId: string;
version: number;
Expand Down Expand Up @@ -58,6 +65,12 @@ export interface BookReviewResponse {
atUserVids: any[];
bookId: string;
bookVersion: number;
refMpInfo: {
reviewId: string;
title: string;
pic_url: string;
createTime: number;
};
chapterName: string;
chapterUid: number;
content: string;
Expand Down Expand Up @@ -273,11 +286,12 @@ export type BookDetailResponse = {
};

export type Chapter = {
chapterUid: number;
chapterIdx: number;
chapterUid?: number;
chapterIdx?: number;
updateTime: number;
title: string;
isMPChapter: number;
refMpReviewId?: string;
level: number;
};

Expand Down Expand Up @@ -333,6 +347,7 @@ export type Highlight = {
colorStyle: number;
reviewContent?: string;
range: string;
refMpReviewId?: string;
};

export type BookReview = {
Expand All @@ -358,11 +373,17 @@ export type Review = {
abstract?: string;
range?: string;
type: number;
refMpInfo?: {
reviewId: string;
title: string;
pic_url: string;
createTime: number;
};
};

export type ChapterHighlightReview = {
chapterUid: number;
chapterIdx: number;
chapterUid?: number;
chapterIdx?: number;
chapterTitle: string;
level: number;
isMPChapter: number;
Expand Down
87 changes: 81 additions & 6 deletions src/parser/parseResponse.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
BookReview,
BookReviewResponse,
Chapter,
ChapterHighlightReview,
ChapterResponse,
ChapterReview,
Expand Down Expand Up @@ -72,6 +73,7 @@ export const parseHighlights = (
createTime: window.moment(highlight.createTime * 1000).format('YYYY-MM-DD HH:mm:ss'),
chapterUid: highlight.chapterUid,
chapterIdx: chapterInfo?.chapterIdx || highlight.chapterUid,
refMpReviewId: highlight.refMpReviewId,
range: highlight.range,
style: highlight.style,
colorStyle: highlight.colorStyle,
Expand All @@ -82,18 +84,65 @@ export const parseHighlights = (
});
};

export const parseChapterHighlightReview = (
chapterResp: ChapterResponse,
export const parseArticleHighlightReview = (
chapters: Chapter[],
highlights: Highlight[],
reviews?: Review[]
): ChapterHighlightReview[] => {
const chapterResult: ChapterHighlightReview[] = [];

if (chapterResp === undefined || chapterResp.data[0] === undefined) {
return chapterResult;
for (const chapter of chapters) {
const refMpReviewId = chapter.refMpReviewId;
const chapterTitle = chapter.title;

// find highlights by chapterUid
const chapterHighlights = highlights
.filter((highlight) => highlight.refMpReviewId === refMpReviewId)
.sort((o1, o2) => {
const o1Start = parseInt(o1.range.split('-')[0]);
const o2Start = parseInt(o2.range.split('-')[0]);
return o1Start - o2Start;
});
let chapterReviews;
if (chapterHighlights && chapterHighlights.length > 0 && reviews) {
chapterReviews = reviews
.filter((review) => refMpReviewId == review.refMpInfo?.reviewId)
.sort((o1, o2) => {
if (o1.range === undefined && o2.range === undefined) {
return 0;
} else if (o1.range === undefined) {
return 1;
} else if (o2.range === undefined) {
return -1;
} else {
const o1Start = parseInt(o1.range.split('-')[0]);
const o2Start = parseInt(o2.range.split('-')[0]);
return o1Start - o2Start;
}
});
}

if (chapterHighlights && chapterHighlights.length > 0) {
chapterResult.push({
chapterTitle: chapterTitle,
level: chapter.level,
isMPChapter: chapter.isMPChapter,
chapterReviews: chapterReviews,
highlights: chapterHighlights
});
}
}

for (const chapter of chapterResp.data[0].updated) {
return chapterResult.sort((o1, o2) => o1.chapterIdx - o2.chapterIdx);
};
export const parseChapterHighlightReview = (
chapters: Chapter[],
highlights: Highlight[],
reviews?: Review[]
): ChapterHighlightReview[] => {
const chapterResult: ChapterHighlightReview[] = [];

for (const chapter of chapters) {
const chapterUid = chapter.chapterUid;
const chapterIdx = chapter.chapterIdx;
const chapterTitle = chapter.title;
Expand Down Expand Up @@ -143,6 +192,32 @@ export const parseChapterHighlightReview = (
return chapterResult.sort((o1, o2) => o1.chapterIdx - o2.chapterIdx);
};

export const parseChapterResp = (
chapterResp: ChapterResponse,
highlightResp: HighlightResponse
): Chapter[] => {
if (chapterResp === undefined) {
return [];
}

if (chapterResp.data !== undefined && chapterResp.data[0].updated.length > 0) {
return chapterResp.data[0].updated;
}

if (highlightResp.refMpInfos !== undefined) {
return highlightResp.refMpInfos.map((mpInfo) => {
return {
refMpReviewId: mpInfo.reviewId,
updateTime: mpInfo.createTime,
title: mpInfo.title,
isMPChapter: 1,
level: 2
};
});
}
return [];
};

export const parseDailyNoteReferences = (notebooks: Notebook[]): DailyNoteReferenece[] => {
const today = window.moment().format('YYYYMMDD');
const todayHighlightBlocks: DailyNoteReferenece[] = [];
Expand Down Expand Up @@ -193,7 +268,7 @@ export const parseReviews = (resp: BookReviewResponse): Review[] => {
created: created,
createTime: createTime,
chapterUid: review.chapterUid,
chapterTitle: review.chapterTitle,
chapterTitle: review.chapterTitle || review.refMpInfo?.title,
content: convertTags ? convertTagToBiLink(review.content) : review.content,
reviewId: reviewId?.replace(/_/gi, '-'),
mdContent: finalMdContent,
Expand Down
12 changes: 12 additions & 0 deletions src/settingTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class WereadSettingsTab extends PluginSettingTab {
this.fileNameType();
this.subFolderType();
this.convertTagToggle();
this.saveArticleToggle();
this.showEmptyChapterTitleToggle();
this.dailyNotes();
const dailyNotesToggle = get(settingsStore).dailyNotesToggle;
Expand Down Expand Up @@ -116,6 +117,17 @@ export class WereadSettingsTab extends PluginSettingTab {
});
}

private saveArticleToggle(): void {
new Setting(this.containerEl)
.setName('是否同步公众号文章?')
.setDesc('开启此选项会将同步公众号文章到单独的笔记中')
.addToggle((toggle) => {
return toggle.setValue(get(settingsStore).saveArticleToggle).onChange((value) => {
settingsStore.actions.setSaveArticleToggle(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 @@ -22,6 +22,7 @@ interface WereadPluginSettings {
notesBlacklist: string;
showEmptyChapterTitleToggle: boolean;
convertTags: boolean;
saveArticleToggle: boolean;
}

const DEFAULT_SETTINGS: WereadPluginSettings = {
Expand All @@ -42,7 +43,8 @@ const DEFAULT_SETTINGS: WereadPluginSettings = {
dailyNotesToggle: false,
notesBlacklist: '',
showEmptyChapterTitleToggle: false,
convertTags: false
convertTags: false,
saveArticleToggle: true
};

const createSettingsStore = () => {
Expand Down Expand Up @@ -209,6 +211,13 @@ const createSettingsStore = () => {
});
};

const setSaveArticleToggle = (saveArticleToggle: boolean) => {
store.update((state) => {
state.saveArticleToggle = saveArticleToggle;
return state;
});
};

return {
subscribe: store.subscribe,
initialise,
Expand All @@ -227,7 +236,8 @@ const createSettingsStore = () => {
setInsertBefore,
setNoteBlacklist,
setEmptyChapterTitleToggle,
setConvertTags
setConvertTags,
setSaveArticleToggle
}
};
};
Expand Down
23 changes: 16 additions & 7 deletions src/syncNotebooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import {
parseChapterHighlightReview,
parseChapterReviews,
parseDailyNoteReferences,
parseReviews
parseReviews,
parseChapterResp,
parseArticleHighlightReview
} from './parser/parseResponse';
import { settingsStore } from './settings';
import { get } from 'svelte/store';
Expand Down Expand Up @@ -96,11 +98,17 @@ export default class SyncNotebooks {

const highlights = parseHighlights(highlightResp, reviewResp);
const reviews = parseReviews(reviewResp);
const chapterHighlightReview = parseChapterHighlightReview(
chapterResp,
highlights,
reviews
);
const chapters = parseChapterResp(chapterResp, highlightResp);
let chapterHighlightReview;
if (metaData.bookType === 3) {
//公众号文章
console.log('sync 公众号:', metaData.title);
chapterHighlightReview = parseArticleHighlightReview(chapters, highlights, reviews);
console.log('sync 公众号 result', metaData.title, chapterHighlightReview);
} else {
chapterHighlightReview = parseChapterHighlightReview(chapters, highlights, reviews);
}
console.log('chapters:', chapters, chapterHighlightReview, highlightResp);
const bookReview = parseChapterReviews(reviewResp);
return {
metaData: metaData,
Expand All @@ -115,7 +123,8 @@ export default class SyncNotebooks {
const filterMetaArr: Metadata[] = [];
for (const metaData of metaDataArr) {
// skip 公众号
if (metaData.bookType === 3) {
const saveArticle = get(settingsStore).saveArticleToggle;
if (!saveArticle && metaData.bookType === 3) {
continue;
}
if (metaData.noteCount < +get(settingsStore).noteCountLimit) {
Expand Down
4 changes: 3 additions & 1 deletion src/utils/frontmatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ export const buildFrontMatter = (
};

const readInfo = noteBook.metaData.readInfo;

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

0 comments on commit af228d5

Please sign in to comment.