Skip to content

Commit

Permalink
feat: added support for different date formats
Browse files Browse the repository at this point in the history
  • Loading branch information
mlhiter committed Aug 1, 2024
1 parent 0910bf8 commit 4689e07
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` 📦
Expand All @@ -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.
34 changes: 34 additions & 0 deletions src/libs/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 日期格式映射到正则表达式
const dateFormatPatterns: Record<string, string> = {
'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)
}
6 changes: 3 additions & 3 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,则不操作
Expand Down

0 comments on commit 4689e07

Please sign in to comment.