Obsidian Templater Estimate estimate\actual ver
作成日: 2025/09/02
code:js
<%*
const editor = app.workspace.activeLeaf.view.editor; // 現在のエディタを取得
const cursor = editor.getCursor(); // カーソル位置を取得
const cursorLine = cursor.line; // カーソル行を取得
const currentLineText = editor.getLine(cursorLine); // カーソル行のテキストを取得
let updatedLine = currentLineText;
let newCursorPos = cursor.ch; // 新しいカーソル位置
// パターン2: - hh:mm- タスク名 → バックスラッシュをタスク名の直前に挿入
let pattern2WithTask = /^- \ \ \d{2}:\d{2}-\s*(.*)$/;
// パターン3: - x hh:mm-hh:mm タスク名 → バックスラッシュをタスク名の直前に挿入
let pattern3WithTask = /^- \x\ \d{2}:\d{2}-\d{2}:\d{2}\s*(.*)$/;
// パターン4: - タスク名 → タスク名の前にバックスラッシュを挿入
let pattern4WithTask = /^- \ \\s+(.*)$/;
// バックスラッシュが既に存在する場合の処理
let existingBackslashPattern = /\\(\d+)?/;
// 既存のバックスラッシュがある場合の処理を最優先
if (existingBackslashPattern.test(currentLineText)) {
// 既存のバックスラッシュがある場合は、その直後の数字の末尾にカーソルを移動
let match = existingBackslashPattern.exec(currentLineText);
if (match && match1) {
// バックスラッシュの後に数字がある場合
newCursorPos = currentLineText.indexOf(match1) + match1.length;
} else {
// 数字がない場合
newCursorPos = currentLineText.indexOf("\\") + 1;
}
} else if (pattern3WithTask.test(currentLineText)) {
// パターン3に一致: バックスラッシュをタスク名の前に挿入
updatedLine = currentLineText.replace(/^(- \x\ \d{2}:\d{2}-\d{2}:\d{2})\s*(.*)$/, "$1 \\$2");
newCursorPos = updatedLine.indexOf("\\") + 1;
} else if (pattern2WithTask.test(currentLineText)) {
// パターン2に一致: バックスラッシュをタスク名の前に挿入
updatedLine = currentLineText.replace(/^(- \ \ \d{2}:\d{2}-)\s*(.*)$/, "$1 \\$2");
newCursorPos = updatedLine.indexOf("\\") + 1;
} else if (pattern4WithTask.test(currentLineText)) {
// パターン4に一致: タスク名の前にバックスラッシュを挿入
updatedLine = currentLineText.replace(/^(- \ \)\s*(.*)$/, "$1 \\$2");
newCursorPos = updatedLine.indexOf("\\") + 1;
}
// 行を更新してカーソルを新しい位置に設定
editor.setLine(cursorLine, updatedLine);
editor.setCursor(cursorLine, newCursorPos);
%>