Skip to content

Commit

Permalink
fix&perf: handle content issues caused by DONE and optimize a little …
Browse files Browse the repository at this point in the history
…performance
  • Loading branch information
mlhiter committed Aug 1, 2024
1 parent ec29031 commit 7b67348
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/libs/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const settingSchema: Array<SettingSchemaDesc> = [
default: '{content} - [[{date}]]',
title: 'Done内容',
description:
'插入的内容,支持几个变量,如`{content}`表示未填充信息之前的块内容(仅在content模式可用),`{date}`指当前日期,`{time}`指当前时间',
'插入的内容,支持几个变量,如`{content}`表示未填充信息之前的块内容(仅在content模式可用,注意这个内容不包括DONE关键字),`{date}`指当前日期,`{time}`指当前时间',
},
{
key: 'displayMode',
Expand Down
17 changes: 11 additions & 6 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ async function main() {
const datePattern = getDatePattern(preferredDateFormat)
const combinedPattern = new RegExp(`- \\[\\[${datePattern.source}\\]\\]`)

// 1. 已经加入过内容,而且状态为DONE,则不操作(这个改到靠上的位置了,减少执行代码的数量,优化性能)
// 1. 已经加入过内容,而且状态为DONE,则不操作
if (combinedPattern.test(block.content) && isDoneStatus) {
return
}
Expand All @@ -111,16 +111,21 @@ async function main() {
return
}

// 3. 没有加入过内容,而且状态为DONE则加入内容
// 3. 没有加入过内容,而且状态不为DONE则不操作
if (!isDoneStatus) return

// 4. 没有加入过内容,而且状态为DONE则加入内容
// 首先检测doneContent是否包含{date},{content},{time}等变量并分别替换为真实值
const contentForReplace = doneContent
// 并且注意处理block.content开头的DONE字符串
const contentWithoutDone = block.content.replace(/^DONE\s*/, '')
const tempContentForReplace = doneContent
.replace(/\{date\}/g, format(new Date(), preferredDateFormat))
.replace(/\{content\}/g, block.content)
.replace(/\{content\}/g, contentWithoutDone)
.replace(/\{time\}/g, format(new Date(), 'HH:mm'))
console.log(contentForReplace)
const finalContentForReplace = `DONE ${tempContentForReplace}`

if (isDoneStatus) {
const newContent = contentForReplace
const newContent = finalContentForReplace
await logseq.Editor.updateBlock(block.uuid, newContent)
}
})
Expand Down

0 comments on commit 7b67348

Please sign in to comment.