LoginSignup
0
0

[Obsidian] Calendarが日付以外も書いたタイトルのDaily Notesを認識しないので、直す

Last updated at Posted at 2024-05-21

本稿でやれること

Daily Notesのタイトルにその日やったことを追記して一覧から内容がわかりやすくできる上、カレンダーからアクセスできるようになる。
具体的には、MM-DD ほげほげという形式のファイルの日付を正しく認識できるようにする。

問題と解決方針

Daily Notesのタイトルにその日のメモの概要を書くと一目で確認が可能になるが、カレンダー機能はJavaScriptの.moment()を使ってタイトルの日付を見て同定しているので日付以外の文字列が入ると認識しなくなる。

Screenshot 2024-05-21 at 16.31.42.png
図1 Daily Notesのタイトル一覧。04-05と04-06は追加の文字列が入っている。

Screenshot 2024-05-21 at 16.31.47.png
図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という関数がある場所

calendar/main.js
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()を使って以下のようにすると良い:

calendar/main.js
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日のように、タイトルに日本語が含まれていても点が表示されれば成功です!

Screenshot 2024-05-21 at 17.02.31.png
図3 更新後のカレンダー。5日と6日も点が表示されているのがわかる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0