1Writer リピートタスク一括コピー6
2025/2/12
複数条件対応
code:md
@rp 平日
@rp 水,土
@rp 毎年\d{1,2}月\d{1,2}日
@rp 毎月\d{1,2}日
@rp 毎月\d{1,2}日,毎年\d{1,2}月\d{1,2}日
@rp 2日毎 2025年1月1日から
テスト用
code:md
2月12日 @rp 毎年2月12日
2月13日 @rp 毎年2月13日
12日 @rp 毎月12日
13日 @rp 毎月13日
1日ごと 10日 @rp 2日毎 2025年2月10日から
1日ごと 11日 @rp 2日毎 2025年2月11日から
1日ごと 12日 @rp 2日毎 2025年2月12日から
水曜 @rp 水
水曜木曜 @rp 水,木
code:js
function generateRepeatTasks() {
const filePath = editor.getFolderPath() + "/リピートタスク.md";
// 現在開いているデイリーノートの日付を取得
const dailyNoteDate = getDailyNoteDate();
if (!dailyNoteDate) {
ui.hudError("日付形式が無効です。デイリーノートのファイル名を確認してください。");
return;
}
// 現在開いているデイリーノートのカーソル位置を取得
const cursorPosition = editor.getSelectedRange()0; // リピートタスクファイルを開き、成功後にgetTextで内容を取得
editor.openFile(filePath, 'edit', () => {
const content = editor.getText();
if (!content) {
ui.hudError("リピートタスク.mdが読み込めませんでした。ファイルが存在するか確認してください。");
return;
}
const tasks = content.split('\n');
const newTasks = [];
tasks.forEach(task => {
const repeatMatch = task.match(/@rp (.+)/);
// @rp指定がないタスク
if (!repeatMatch) {
const taskText = task.trim();
if (taskText.startsWith("###")) {
// セクション見出しはそのまま出力
newTasks.push(taskText);
} else if (taskText.startsWith("■")) {
// ■ から始まる行は出力しない
} else if (/^\s*$/.test(taskText)){
// 空行は出力しない
} else {
// 通常のタスクはそのまま出力
newTasks.push(${taskText});
}
return;
}
// @rp指定があるタスクはデイリーノートの日付がリピート日に該当する場合のみコピー
const repeatRule = repeatMatch1; const isRepeatDay = checkRepeatDay(dailyNoteDate, repeatRule);
if (isRepeatDay) {
const taskText = task.replace(/@rp .+/, '').trim();
newTasks.push(${taskText});
}
});
// タスクリストの挿入
if (newTasks.length > 0) {
const newFilePath = editor.getFolderPath() + /${dailyNoteDate}.md;
// カーソル位置を取得し、そこに新しいタスクを挿入
editor.openFile(newFilePath, 'edit', () => {
const tasksToInsert = newTasks.join('\n') + '\n';
editor.replaceTextInRange(cursorPosition, cursorPosition, tasksToInsert);
ui.hudSuccess("次回のリピートタスクを生成しました!");
});
} else {
ui.hudError("一致するタスクがありません。");
}
});
}
// 開いているデイリーノートの日付を取得(ファイル名は"YYYY-MM-DD.md"形式であること)
function getDailyNoteDate() {
const fileName = editor.getFileName();
const dateMatch = fileName.match(/^(\d{4}-\d{2}-\d{2})/);
return dateMatch ? dateMatch1 : null; }
function checkRepeatDay(noteDate, rule) {
const noteDateObj = new Date(noteDate);
// ルールがカンマ区切りなら分割、単独ならそのまま配列化
const rules = rule.includes(',') ? rule.split(',').map(r => r.trim()) : rule; return rules.some(singleRule => {
const dayIntervalMatch = singleRule.match(/^(\d+)日毎 (\d{4})年(\d{1,2})月(\d{1,2})日から$/);
const weekdaysMatch = singleRule.match(/^(平日)$/);
const dayOfWeekMatch = singleRule.match(/^(日月火水木金土)$/); const monthlyMatch = singleRule.match(/^毎月(\d{1,2})日$/);
const yearlyMatch = singleRule.match(/^毎年(\d{1,2})月(\d{1,2})日$/);
if (dayIntervalMatch) {
// n日毎の判定
const nDays = parseInt(dayIntervalMatch1, 10); const baseDate = new Date(parseInt(dayIntervalMatch2), parseInt(dayIntervalMatch3) - 1, parseInt(dayIntervalMatch4)); const diffDays = Math.floor((noteDateObj - baseDate) / (1000 * 60 * 60 * 24));
return diffDays >= 0 && (diffDays % nDays) === 0;
} else if (weekdaysMatch) {
// 平日(月〜金)
const dayOfWeek = noteDateObj.getDay();
return dayOfWeek >= 1 && dayOfWeek <= 5;
} else if (dayOfWeekMatch) {
// 特定の曜日(例: 月, 水, 金)
const repeatDayOfWeek = dayOfWeekMatch1; return todayDayOfWeek === repeatDayOfWeek;
} else if (monthlyMatch) {
// 毎月特定の日
const dayOfMonth = parseInt(monthlyMatch1, 10); return noteDateObj.getDate() === dayOfMonth;
} else if (yearlyMatch) {
// 毎年特定の日付
const month = parseInt(yearlyMatch1, 10) - 1; const day = parseInt(yearlyMatch2, 10); return noteDateObj.getMonth() === month && noteDateObj.getDate() === day;
}
return false;
});
}
// 日付を "YYYY-MM-DD" フォーマットで文字列に変換
function formatDate(date) {
const yyyy = date.getFullYear();
const mm = String(date.getMonth() + 1).padStart(2, '0');
const dd = String(date.getDate()).padStart(2, '0');
return ${yyyy}-${mm}-${dd};
}
generateRepeatTasks();