本稿でやれること
Daily Notesのタイトルにその日やったことを追記して一覧から内容がわかりやすくできる上、カレンダーからアクセスできるようになる。
具体的には、MM-DD ほげほげ
という形式のファイルの日付を正しく認識できるようにする。
問題と解決方針
Daily Notesのタイトルにその日のメモの概要を書くと一目で確認が可能になるが、カレンダー機能はJavaScriptの.moment()
を使ってタイトルの日付を見て同定しているので日付以外の文字列が入ると認識しなくなる。
図1 Daily Notesのタイトル一覧。04-05と04-06は追加の文字列が入っている。
図2 Obsidian内のCalendarプラグインの表示。上記で日本語が追加された5日と6日には点が付いていない。
そこで、Calendarプラグインの設定ファイルを少しだけいじって、タイトル先頭の文字列だけで判定してもらうようにする。
幸い自動生成の日付は常にMM-DD
の5文字で構成されるので、先頭5文字のみを読むように設定変更すれば終わる。
ファイルの書き換え
Obsidianの設定ファイルはノートが保存されている場所と同じ場所に存在する。
隠しファイルになっていることがあるので、ターミナルでls -a
とすれば.obsidian
の存在が確認できる。
例えばMacだと以下のような場所にカレンダーに関する設定ファイルがある。
/Users/<username>/Obsidian/.obsidian/plugins/calendar/main.js
これを開き、132行目あたりのgetDateFromFile
という関数がある場所
function getDateFromFile(file, granularity) {
const getSettings = {
day: getDailyNoteSettings,
week: getWeeklyNoteSettings,
month: getMonthlyNoteSettings,
};
const format = getSettings[granularity]().format.split("/").pop();
const noteDate = window.moment(file.basename, format, true);
if (!noteDate.isValid()) {
return null;
}
if (isFormatAmbiguous(format, granularity)) {
if (granularity === "week") {
const cleanFormat = removeEscapedCharacters(format);
if (/w{1,2}/i.test(cleanFormat)) {
return window.moment(file.basename,
// If format contains week, remove day & month formatting
format.replace(/M{1,4}/g, "").replace(/D{1,4}/g, ""), false);
}
}
}
return noteDate;
}
を見つける。真ん中あたりの
const noteDate = window.moment(file.basename, format, true);
がタイトルを参照して(file.basename
)日付を得ているので、このときタイトルの先頭5文字だけを参照するように書き換える。
.slice()
を使って以下のようにすると良い:
function getDateFromFile(file, granularity) {
const getSettings = {
day: getDailyNoteSettings,
week: getWeeklyNoteSettings,
month: getMonthlyNoteSettings,
};
const format = getSettings[granularity]().format.split("/").pop();
// edited
const noteDate = window.moment(file.basename.slice(0, 5), format, true);
// edited until here
if (!noteDate.isValid()) {
return null;
}
if (isFormatAmbiguous(format, granularity)) {
if (granularity === "week") {
const cleanFormat = removeEscapedCharacters(format);
if (/w{1,2}/i.test(cleanFormat)) {
return window.moment(file.basename,
// If format contains week, remove day & month formatting
format.replace(/M{1,4}/g, "").replace(/D{1,4}/g, ""), false);
}
}
}
return noteDate;
}
ソースコードを書き換えると未知のバグが生じる可能性があるのでコメントに検索しやすいキーワード(ユーザーネームなど)を入れて修正しやすいようにすると良いでしょう。
結果
ファイルを保存した後にObsidianを再起動してカレンダーを確認します。
以下で5日と6日のように、タイトルに日本語が含まれていても点が表示されれば成功です!