1Writer Journal Link 2
曜日を追加
code:md
<< 2024-10-07 | 2024-10-09 >>
<< 2024-10-01 | 2024-10-15 >>
<< 2024-09-08 | 2024-11-08 >>
<< 2023-10-08 | 2025-10-08 >>
code:js
// ファイル名から日付を取得する関数
function getDateFromFileName(fileName) {
const datePattern = /^\d{4}-\d{2}-\d{2}$/; // YYYY-MM-DD形式のパターン
if (datePattern.test(fileName)) {
return new Date(fileName);
}
return null;
}
// 曜日を日本語で取得する関数
function getJapaneseDayOfWeek(date) {
}
// 日付リンクを生成する関数
function formatDateLink(date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0'); // 月は0から始まるので+1
const day = String(date.getDate()).padStart(2, '0');
return ${year}-${month}-${day};
}
// 現在開いているファイルのタイトルを取得
const fileTitle = editor.getFileName().replace('.md', ''); // ".md"を取り除く
// タイトルから日付を取得
const fileDate = getDateFromFileName(fileTitle);
if (fileDate) {
// 曜日を取得
const fileDayOfWeek = getJapaneseDayOfWeek(fileDate);
// 日付リンクを生成
const dayBefore = new Date(fileDate);
dayBefore.setDate(fileDate.getDate() - 1);
const dayAfter = new Date(fileDate);
dayAfter.setDate(fileDate.getDate() + 1);
const weekBefore = new Date(fileDate);
weekBefore.setDate(fileDate.getDate() - 7);
const weekAfter = new Date(fileDate);
weekAfter.setDate(fileDate.getDate() + 7);
const monthBefore = new Date(fileDate);
monthBefore.setMonth(fileDate.getMonth() - 1);
const monthAfter = new Date(fileDate);
monthAfter.setMonth(fileDate.getMonth() + 1);
const yearBefore = new Date(fileDate);
yearBefore.setFullYear(fileDate.getFullYear() - 1);
const yearAfter = new Date(fileDate);
yearAfter.setFullYear(fileDate.getFullYear() + 1);
// 出力する内容を整形
const output = `<< ${formatDateLink(dayBefore)} | ${formatDateLink(dayAfter)} >>
<< ${formatDateLink(weekBefore)} | ${formatDateLink(weekAfter)} >>
<< ${formatDateLink(monthBefore)} | ${formatDateLink(monthAfter)} >>
<< ${formatDateLink(yearBefore)} | ${formatDateLink(yearAfter)} >>
// 結果をエディタに出力
editor.replaceSelection(output);
} else {
ui.alert("ファイル名から日付を取得できませんでした。タイトルは 'YYYY-MM-DD' 形式にしてください。");
}