Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Commit

Permalink
优化第二种同步模式的资源导入方案
Browse files Browse the repository at this point in the history
  • Loading branch information
Yimien committed Jan 4, 2024
1 parent 22c3c8e commit c04f786
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v0.1.4
1. 增加:同步至笔记本或文档模式支持图片布局调整
2. 优化:同步至笔记本或文档模式的文档生成排序

## v0.1.3

1. 优化:资源链接保存方案
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
- 笔记本:配置保存 Memos 的数据在哪个笔记本。
- 文档路径:仅在同步方案为 `同步至指定笔记本或文档` 时有效,不填写则会将数据直接保存在笔记本下,如果需要填写时,以 `/` 开头进行填写。例如:/test 就代表着将数据保存到 名称为test 的文档下。
- 引用处理方案:以引用块或者嵌入块的形式保存 Memos 的引用。
- 图片块布局:将资源中的图片单独提取为一块,允许自由选择图片的排列模式。

### 特别感谢

Expand Down
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "plugin-memos-sync",
"author": "Yimien",
"url": "https://github.com/Yimien/plugin-memos-sync.git",
"version": "0.1.3",
"version": "0.1.4",
"minAppVersion": "2.11.3",
"backends": [
"all"
Expand Down
66 changes: 42 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,20 +250,20 @@ export default class MemosSync extends Plugin {
let resourceMap = this.handleResource(resource);
let mdLink = resourceMap.mdLink;
let resourceTypeText = resourceMap.resourceTypeText;
if (mdLink){
if (mdLink) {
// 针对保存到块的处理
if (resourceTypeText == "image"){
if (resourceTypeText == "image") {
imageLinks += `${mdLink}`;
if (imageLayout === IMAGE_LAYOUT.direction && (resource !== resourceList[resourceList.length -1])){
if (imageLayout === IMAGE_LAYOUT.direction && (resource !== resourceList[resourceList.length - 1])) {
imageLinks += "\n";
}
}else{
}
} else {
resources.push(mdLink);
}

// 针对保存到文档的处理
resourceLinks += `${mdLink}`;
if (resource !== resourceList[resourceList.length -1]){
if (resource !== resourceList[resourceList.length - 1]) {
resourceLinks += "\n";
}
}
Expand Down Expand Up @@ -570,17 +570,17 @@ export default class MemosSync extends Plugin {
* @param pageId - 文档ID
* @returns
*/
async delEmptyBlock(pageId){
async delEmptyBlock(pageId) {
let response = await api.getChildBlocks(pageId);
print('response', response);
if (!api.isOK(response)){
if (!api.isOK(response)) {
return;
}


let blockList = response.data;
print('blockList.length', blockList.length)
if (blockList.length === 0){
if (blockList.length === 0) {
return;
}

Expand All @@ -589,13 +589,13 @@ export default class MemosSync extends Plugin {

let blocks = await this.getBlockContentById(lastBlockId);
print('blocks', blocks)
if (blocks.length === 0){
if (blocks.length === 0) {
return;
}

let lastBlockContent = blocks[0].content;
print(lastBlockContent);
if (lastBlockContent){
if (lastBlockContent) {
return;
}

Expand All @@ -612,7 +612,7 @@ export default class MemosSync extends Plugin {
*/
async batchHandleContentBlock(pageId, memoObjList) {
let blockIdMap = {};

for (let memoObj of memoObjList) {
let memoId = memoObj.memoId;
let response = await this.handleContentBlock(pageId, memoObj);
Expand Down Expand Up @@ -657,18 +657,18 @@ export default class MemosSync extends Plugin {
let childId = childResponse.data[0].id;
// 文本
let contentText = memoObj.contentText;
if (contentText){
if (contentText) {
await api.appendBlock(childId, contentText);
}
// 图片
let imageLinks = memoObj.imageLinks;
if (imageLinks){
if (imageLinks) {
await api.appendBlock(childId, imageLinks);
}
// 其它资源
let resources = memoObj.resources;
if (resources.length > 0){
for (let r of resources){
if (resources.length > 0) {
for (let r of resources) {
await api.appendBlock(childId, r);
}
}
Expand Down Expand Up @@ -767,7 +767,7 @@ export default class MemosSync extends Plugin {
* @param blockId
* @returns
*/
async getBlockContentById(blockId){
async getBlockContentById(blockId) {
print('getBlockContentById blockId', blockId);
let sql = `SELECT * FROM blocks WHERE id="${blockId}";`
let response = await api.querySql(sql);
Expand Down Expand Up @@ -835,19 +835,37 @@ export default class MemosSync extends Plugin {
await api.pushErrMsg("你选择的笔记本当前不存在!");
}

// 排序
memoObjList.sort((a, b) => +a.displayts - +b.displayts);

// 保存为页面
for (let memoObj of memoObjList) {
print(memoObj);
let memoId = memoObj.memoId;
let title = memoObj.title;
let path = `${pagePath}/${title}`
let md = memoObj.content;
let md = (memoObj.contentText) ? memoObj.contentText : "";
let response = await api.createDocWithMd(notebookId, path, md);

if (!api.isOK(response)){
if (!api.isOK(response)) {
continue;
}

let blockId = response.data;

// 图片
let imageLinks = memoObj.imageLinks;
if (imageLinks) {
await api.appendBlock(blockId, imageLinks);
}
// 其它资源
let resources = memoObj.resources;
if (resources.length > 0) {
for (let r of resources) {
await api.appendBlock(blockId, r);
}
}

blockIdMaps[memoId] = blockId;
}

Expand Down Expand Up @@ -917,7 +935,7 @@ export default class MemosSync extends Plugin {
this.syncing = false;
this.topBarElement.innerHTML = getSvgHtml("memos", this.isMobile);
return;
}else{
} else {
await api.pushMsg("同步中,请稍候...");
}

Expand Down Expand Up @@ -1089,7 +1107,7 @@ export default class MemosSync extends Plugin {
async eventBusHandler(detail) {
await this.checkNew() // 检查 Memos 是否有新数据
}

async onload() {
// 获取本地配置
let conResponse = await api.getLocalStorage();
Expand Down Expand Up @@ -1332,7 +1350,7 @@ export default class MemosSync extends Plugin {
await this.checkNew();
}

async openSetting(){
async openSetting() {
this.nowNotebooks = await this.getNotebooks();
super.openSetting();
}
Expand Down

0 comments on commit c04f786

Please sign in to comment.