マンスリーログ
code:script.js
const newDate = (yyyymm, offset) => {
const dt = yyyymm ? new Date(yyyymm) : new Date()
dt.setMonth(dt.getMonth() + offset)
const locale = 'ja-JP'
const options = { year: 'numeric', month: '2-digit', day: '2-digit' }
return dt.toLocaleDateString(locale, options).replace(/\//g, '-').substring(0, 7)
}
const listDates = yyyymm => {
const result = []
const dt = yyyymm ? new Date(yyyymm) : new Date()
const m = dt.getMonth()
for (const index of Array.from(Array(31).keys())) {
dt.setDate(index + 1)
if (dt.getMonth() !== m) { break }
result.push([${dt.toISOString().substring(0, 10)}])
}
return result
}
scrapbox.PageMenu().addItem({
title: 'マンスリーログ',
image: null,
onClick: (e) => {
const thisMonth = window.prompt('マンスリーログを作ります: yyyy-mm', newDate('', 0))
if (!thisMonth) { return }
const prevMonth = newDate(thisMonth, -1)
const nextMonth = newDate(thisMonth, +1)
const title = encodeURIComponent(thisMonth)
const body = encodeURIComponent([
[${prevMonth}] | [${nextMonth}],
''
].concat(listDates(thisMonth)).concat([
'',
'#マンスリーログ'
]).join('\n'))
const created = scrapbox.Project.pages.some(p => p.exists && p.title === thisMonth)
if (created) {
window.open(/${scrapbox.Project.name}/${title})
} else {
window.open(/${scrapbox.Project.name}/${title}?body=${body})
}
}
})