Skip to content

Instantly share code, notes, and snippets.

@Moyf
Last active April 16, 2025 07:21
Show Gist options
  • Save Moyf/8e915b64008a6e2807095297e919ba0a to your computer and use it in GitHub Desktop.
Save Moyf/8e915b64008a6e2807095297e919ba0a to your computer and use it in GitHub Desktop.

<%* // 获取编辑器实例 const editor = app.workspace.activeEditor.editor;

// 获取当前行 const cursor = editor.getCursor(); const line = editor.getLine(cursor.line);

// 1. 将普通文本或普通列表转换成未完成的任务 if (!/^[\s]- [[ x-]]/.test(line)) { // 将普通文本或普通列表转换为未完成的任务 let newLine; if (line.startsWith('- ') || line.trim().startsWith('- ')) { // 如果是普通列表项,在破折号后插入任务标记 newLine = line.replace(/^(\s- )/, '$1[ ] '); } else { // 如果是普通文本,转换为任务,保留原有缩进 const indentation = line.match(/^\s*/)[0]; // 获取开头的空白字符 newLine = ${indentation}- [ ] ${line.trim()}; } editor.replaceRange(newLine, { line: cursor.line, ch: 0 }, { line: cursor.line, ch: line.length }); // 将光标移动到行末尾 editor.setCursor({ line: cursor.line, ch: newLine.length }); return; }

// ! 检查当前任务状态 let newLine = line; if (line.includes('[ ]')) { // 如果是未完成状态,先检查是否有内容 if (line.replace('- [ ]', '').trim().length > 0) { // 标记为完成状态并添加日期 const today = new Date(); const dateStr = today.toISOString().split('T')[0].replace(/-/g, '-'); newLine = newLine.replace(/[ ]/, '[x]'); newLine = newLine + ✅ ${dateStr}; // 这里添加日期,去掉行末空格 } // 否则切换回普通列表 else { // 如果没有内容,直接删除任务标记 newLine = newLine.replace(/[ ]/, ''); } } else if (line.includes('[x]')) { // 如果是完成状态,切换回未完成状态 newLine = newLine.replace(/[[x]]/, '[ ]'); newLine = newLine.replace(/\s✅\s\d{4}-\d{2}-\d{2}\s*$/, ''); } else { // 如果是其他状态,切换为未完成状态 newLine = newLine.replace(/[[ x]]/, '[ ]'); }

// console.log(>>${newLine}<<);

// 替换当前行——注意是 trim 后的长度,不然可能导致多一个空格 editor.replaceRange( newLine.trimEnd(), { line: cursor.line, ch: 0 }, { line: cursor.line, ch: line.trimEnd().length } );

// 然后运行一下重新排序的任务 // window.open("obsidian://advanced-uri?vault=Obsinote&commandid=templater-obsidian%253A_global%252Ftemplates%252Fcommand%252FtpRunner.md&template=ReOrder List 重排序列表"); // 这里是不是该用那个用户函数了捏…… 🤔

-%>

<%* // Get the editor instance const editor = app.workspace.activeEditor.editor;

// Get the current line const cursor = editor.getCursor(); const line = editor.getLine(cursor.line);

// 1. Convert plain text or plain list into an uncompleted task if (!/^[\s]- [[ x-]]/.test(line)) { // Convert plain text or plain list into an uncompleted task let newLine; if (line.startsWith('- ') || line.trim().startsWith('- ')) { // If it's a plain list item, insert the task marker after the dash newLine = line.replace(/^(\s- )/, '$1[ ] '); } else { // If it's plain text, convert it into a task, preserving the original indentation const indentation = line.match(/^\s*/)[0]; // Get the leading whitespace newLine = ${indentation}- [ ] ${line.trim()}; } editor.replaceRange(newLine, { line: cursor.line, ch: 0 }, { line: cursor.line, ch: line.length }); // Move the cursor to the end of the line editor.setCursor({ line: cursor.line, ch: newLine.length }); return; }

// ! Check the current task status let newLine = line; if (line.includes('[ ]')) { // If it's in an uncompleted state, first check if there is content if (line.replace('- [ ]', '').trim().length > 0) { // Mark as completed and add the date const today = new Date(); const dateStr = today.toISOString().split('T')[0].replace(/-/g, '-'); newLine = newLine.replace(/[ ]/, '[x]'); newLine = newLine + ✅ ${dateStr}; // Add the date here, removing trailing spaces } // Otherwise, switch back to a plain list else { // If there is no content, directly remove the task marker newLine = newLine.replace(/[ ]/, ''); } } else if (line.includes('[x]')) { // If it's in a completed state, switch back to an uncompleted state newLine = newLine.replace(/[[x]]/, '[ ]'); newLine = newLine.replace(/\s✅\s\d{4}-\d{2}-\d{2}\s*$/, ''); } else { // If it's in another state, switch to an uncompleted state newLine = newLine.replace(/[[ x]]/, '[ ]'); }

// Replace the current line—note the trimmed length, otherwise it may result in an extra space editor.replaceRange( newLine.trimEnd(), { line: cursor.line, ch: 0 }, { line: cursor.line, ch: line.trimEnd().length } );

-%>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment