Created
February 11, 2023 15:06
-
-
Save naosim/8f1cf48be4eb6e71517306b830123a9c to your computer and use it in GitHub Desktop.
markdownに今日の日付のタイトル(H1)をいれる
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* @param {string} markdownText | |
* @param {Date} now | |
*/ | |
function insertTodaysTitle(markdownText, now) { | |
markdownText = markdownText.trim(); | |
const firstLine = markdownText.split("\n")[0].trim(); | |
const zerofill2 = (num) => ("0" + num).slice(-2); | |
const month = zerofill2(now.getMonth() + 1); | |
const date = zerofill2(now.getDate()); | |
const title = `# ${now.getFullYear()}-${month}-${date}`; | |
if (firstLine.indexOf(title) === 0) { | |
return markdownText; | |
} | |
return title + "\n\n\n" + markdownText.trim(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment