Obsidian タイムスタンプ
行頭にタイムスタンプを追加
[[YYYY-MM-DD]] hh:mm 形式
カーソルが行頭にある場合、日時スタンプを挿入し、その後にカーソルが移動するようにする
カーソルが行頭にない場合は、日時スタンプを追加するが、カーソル位置はそのままにする
テスト
code:txt
Test
-
- Test
期待する結果
code:txt
2024-09-20 15:23
2024-09-20 15:23 Test
- 2024-09-20 15:23
- 2024-09-20 15:22 Test
カーソル位置の調整
code:js
<%*
function addTimestampToCurrentLine() {
// Get the current date and time
const now = new Date();
const yyyy = now.getFullYear();
const mm = String(now.getMonth() + 1).padStart(2, '0');
const dd = String(now.getDate()).padStart(2, '0');
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
// Format as YYYY-MM-DD hh:mm
const timestamp = [[${yyyy}-${mm}-${dd}]] ${hours}:${minutes} ;
// Get the current editor and cursor position
const editor = this.app.workspace.activeLeaf.view.sourceMode.cmEditor;
const cursor = editor.getCursor();
// Get the content of the current line
const line = editor.getLine(cursor.line);
// Check if the line starts with "- "
if (line.trim() === "-") {
// Insert timestamp after the bullet ("- ")
const newLine = - ${timestamp};
editor.replaceRange(newLine, { line: cursor.line, ch: 0 }, { line: cursor.line, ch: line.length });
// Place the cursor after the timestamp
editor.setCursor({ line: cursor.line, ch: 2 + timestamp.length });
} else if (line.startsWith("- ")) {
// If the line starts with "- ", insert timestamp after it
const newLine = - ${timestamp}${line.slice(2)};
editor.replaceRange(newLine, { line: cursor.line, ch: 0 }, { line: cursor.line, ch: line.length });
// If the cursor is at the start, place the cursor after the timestamp
if (cursor.ch === 0) {
editor.setCursor({ line: cursor.line, ch: 2 + timestamp.length });
}
} else if (cursor.ch === 0) {
// If cursor is at the beginning of the line, insert timestamp and move cursor after it
editor.replaceRange(timestamp + line, { line: cursor.line, ch: 0 }, { line: cursor.line, ch: line.length });
editor.setCursor({ line: cursor.line, ch: timestamp.length });
} else {
// If cursor is not at the beginning, insert timestamp at the beginning and keep the cursor position
editor.replaceRange(timestamp + line, { line: cursor.line, ch: 0 }, { line: cursor.line, ch: line.length });
}
}
// Call the function to add the timestamp
addTimestampToCurrentLine();
%>
code:js
<%*
function addTimestampToCurrentLine() {
// Get the current date and time
const now = new Date();
const yyyy = now.getFullYear();
const mm = String(now.getMonth() + 1).padStart(2, '0');
const dd = String(now.getDate()).padStart(2, '0');
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
// Format as YYYY-MM-DD hh:mm
const timestamp = [[${yyyy}-${mm}-${dd}]] ${hours}:${minutes} ;
// Get the current editor and cursor position
const editor = this.app.workspace.activeLeaf.view.sourceMode.cmEditor;
const cursor = editor.getCursor();
// Get the content of the current line
const line = editor.getLine(cursor.line);
// Add the timestamp at the start of the line
editor.replaceRange(timestamp + line, { line: cursor.line, ch: 0 }, { line: cursor.line, ch: line.length });
}
// Call the function to add the timestamp
addTimestampToCurrentLine();
%>