diff --git a/readme.md b/readme.md index 67ca89a..1567313 100644 --- a/readme.md +++ b/readme.md @@ -8,6 +8,7 @@ - Pnpm ## How to get started + 1. Clone the repository or use the button "Use this template" on GitHub to create your own version of the repository 🔨 2. Make sure you have pnpm installed, [install](https://pnpm.io/installation) if necessary 🛠 3. Execute `pnpm install` 📦 @@ -16,3 +17,9 @@ 6. Enable developer-mode in Logseq, go to plugins, select "Load unpacked plugin" 🔌 7. Select the directory of your plugin (not the `/dist`-directory, but the directory which includes your package.json) 📂 8. Enjoy! 🎉 + +## Some you should know + +1. Preferred date page format maybe have some error map,because I didn't test it.If it has error,please tell me about it in issues. + +2. I update block when your block changed,if it caused some performance problems,please tell me. diff --git a/src/libs/date.ts b/src/libs/date.ts new file mode 100644 index 0000000..0e3d243 --- /dev/null +++ b/src/libs/date.ts @@ -0,0 +1,34 @@ +// 日期格式映射到正则表达式 +const dateFormatPatterns: Record = { + 'E,dd-MM-yyyy': '\\w,\\d{2}-\\d{2}-\\d{4}', + 'E,dd.MM.yyyy': '\\w,\\d{2}\\.\\d{2}\\.\\d{4}', + 'E,yyyy/MM/dd': '\\w,\\d{4}/\\d{2}/\\d{2}', + 'EEE,MM/dd/yyyy': '\\w{3},\\d{2}/\\d{2}/\\d{4}', + 'EEE,dd-MM-yyyy': '\\w{3},\\d{2}-\\d{2}-\\d{4}', + 'EEE,dd.MM.yyyy': '\\w{3},\\d{2}\\.\\d{2}\\.\\d{4}', + 'EEE,yyyy/MM/dd': '\\w{3},\\d{4}/\\d{2}/\\d{2}', + 'EEEE,MM/dd/yyyy': '\\w{4,},\\d{2}/\\d{2}/\\d{4}', + 'EEEE,dd-MM-yyyy': '\\w{4,},\\d{2}-\\d{2}-\\d{4}', + 'EEEE,dd.MM.yyyy': '\\w{4,},\\d{2}\\.\\d{2}\\.\\d{4}', + 'EEEE,yyyy/MM/dd': '\\w{4,},\\d{4}/\\d{2}/\\d{2}', + 'MM-dd-yyyy': '\\d{2}-\\d{2}-\\d{4}', + 'MM/dd/yyyy': '\\d{2}/\\d{2}/\\d{4}', + 'MMM do,yyyy': '\\w{3} \\d{1,2},\\d{4}', + 'MMMM do,yyyy': '\\w{4,} \\d{1,2},\\d{4}', + MM_dd_yyyy: '\\d{2}_\\d{2}_\\d{4}', + 'dd-MM-yyyy': '\\d{2}-\\d{2}-\\d{4}', + 'do MMM yyyy': '\\d{1,2} \\w{3} \\d{4}', + 'do MMMM yyyy': '\\d{1,2} \\w{4,} \\d{4}', + 'yyyy-MM-dd': '\\d{4}-\\d{2}-\\d{2}', + 'yyyy/MM/dd EEEE': '\\d{4}/\\d{2}/\\d{2} \\w{4,}', + yyyyMMdd: '\\d{8}', + yyyy_MM_dd: '\\d{4}_\\d{2}_\\d{2}', + yyyy年MM月dd日: '\\d{4}年\\d{2}月\\d{2}日', +} + +// 根据用户的日期格式生成正则表达式 +export function getDatePattern(preferredDateFormat: string): RegExp { + const pattern = + dateFormatPatterns[preferredDateFormat] || '\\d{4}-\\d{2}-\\d{2}' + return new RegExp(pattern) +} diff --git a/src/main.tsx b/src/main.tsx index 56854b8..2754e6f 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -4,9 +4,9 @@ import React from 'react' import { format } from 'date-fns' import * as ReactDOM from 'react-dom/client' -import App from './App' - import './index.css' +import App from './App' +import { getDatePattern } from './libs/date' import { logseq as pluginInfo } from '../package.json' // @ts-expect-error @@ -81,7 +81,7 @@ async function main() { const addedContent = `- [[${formattedUserDate}]]` // 检查是否已经加入过内容 - const datePattern = /\d{4}-\d{2}-\d{2}/ + const datePattern = getDatePattern(preferredDateFormat) const combinedPattern = new RegExp(`- \\[\\[${datePattern.source}\\]\\]`) // 已经加入过内容,而且状态为DONE,则不操作