Skip to content

Instantly share code, notes, and snippets.

@naosim
Created February 11, 2023 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naosim/8f1cf48be4eb6e71517306b830123a9c to your computer and use it in GitHub Desktop.
Save naosim/8f1cf48be4eb6e71517306b830123a9c to your computer and use it in GitHub Desktop.
markdownに今日の日付のタイトル(H1)をいれる
/**
*
* @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