Initial commit
This commit is contained in:
51
api/modules/todo/parser.ts
Normal file
51
api/modules/todo/parser.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import type { TodoItem, DayTodo } from './types.js'
|
||||
|
||||
export const parseTodoContent = (content: string): DayTodo[] => {
|
||||
const lines = content.split('\n')
|
||||
const result: DayTodo[] = []
|
||||
let currentDate: string | null = null
|
||||
let currentItems: TodoItem[] = []
|
||||
let itemId = 0
|
||||
|
||||
for (const line of lines) {
|
||||
const dateMatch = line.match(/^##\s*(\d{4}-\d{2}-\d{2})/)
|
||||
if (dateMatch) {
|
||||
if (currentDate) {
|
||||
result.push({ date: currentDate, items: currentItems })
|
||||
}
|
||||
currentDate = dateMatch[1]
|
||||
currentItems = []
|
||||
} else if (currentDate) {
|
||||
const todoMatch = line.match(/^- (√|○) (.*)$/)
|
||||
if (todoMatch) {
|
||||
currentItems.push({
|
||||
id: `${currentDate}-${itemId++}`,
|
||||
content: todoMatch[2],
|
||||
completed: todoMatch[1] === '√'
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (currentDate) {
|
||||
result.push({ date: currentDate, items: currentItems })
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
export const generateTodoContent = (dayTodos: DayTodo[]): string => {
|
||||
const lines: string[] = []
|
||||
const sortedDays = [...dayTodos].sort((a, b) => a.date.localeCompare(b.date))
|
||||
|
||||
for (const day of sortedDays) {
|
||||
lines.push(`## ${day.date}`)
|
||||
for (const item of day.items) {
|
||||
const checkbox = item.completed ? '√' : '○'
|
||||
lines.push(`- ${checkbox} ${item.content}`)
|
||||
}
|
||||
lines.push('')
|
||||
}
|
||||
|
||||
return lines.join('\n').trimEnd()
|
||||
}
|
||||
Reference in New Issue
Block a user