dhniceday

Untitled

Feb 16th, 2023
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. <%*
  2. const plannerHeader = "## Day Planner"
  3. const file = tp.file.find_tfile(tp.date.now("YYYY-MM-DD"))
  4.  
  5. var rxLogItem = /^([0-2][0-9]:[0-5][0-9] [a-zA-Z0-9]).*/
  6. var rxTaskItem = /^- \[[ |x]\] [0-2][0-9]:[0-5][0-9] [a-zA-Z0-9].*/
  7. var rxTime = /[0-2][0-9]:[0-5][0-9]/
  8. var insertPosition = -1
  9. var positionFound = -1
  10. var index = 0
  11.  
  12. function isEarlier(t1, t2) {
  13. const r = t1 < t2 ? true : false
  14. return r
  15. }
  16.  
  17. function isSame(t1, t2) {
  18. const r = t1.toString() == t2.toString() ? true : false;
  19. return r;
  20. }
  21.  
  22. async function executeCommand(name) {
  23. const commandName = name;
  24. // Get command
  25. const commands = app.commands.listCommands();
  26. const collatorCompare = new Intl.Collator(["en"], {sensitivity: "base", usage: "search", ignorePunctuation: true}).compare;
  27. const command = commands.find(c => collatorCompare(c.name, commandName) === 0);
  28. // Execute
  29. await app.commands.executeCommandById(command.id);
  30. }
  31.  
  32. console.log('file: ' + file.path);
  33.  
  34. if (file) { // Does the file exist ?
  35. logItem = await tp.system.prompt("Event for Day Planner: (HH:mm event)")
  36.  
  37. if (rxLogItem.test(logItem)) { // User input in correct format ?
  38. const content = (await app.vault.read(file)).split("\n")
  39. const rowNumberHeader = content.indexOf(plannerHeader)
  40.  
  41. if (rowNumberHeader >= 0) { // Does the day planner header exist ?
  42. index = rowNumberHeader // then start checking from here
  43.  
  44. // Find positionItem - Where to insert the log item
  45. let timeLogItem = rxTime.exec(logItem)
  46. do {
  47. positionFound = -1;
  48. index++
  49. let row = content[index]
  50.  
  51. if (rxTaskItem.test(row)) {
  52. let timeTask = rxTime.exec(row)
  53.  
  54. if (isSame(timeLogItem, timeTask)) {
  55. console.log('same.')
  56. new Notice("Entry already exists.")
  57. positionFound = -1
  58. break;
  59. } else {
  60. if (isEarlier(timeLogItem, timeTask)) {
  61. positionFound = index
  62. }
  63. }
  64. } else {
  65. positionFound = index
  66. break;
  67. }
  68.  
  69. }
  70. while (positionFound < 0)
  71.  
  72. console.log('positionFound: ' + positionFound)
  73.  
  74. if (positionFound > 0) {
  75. // insert entry
  76. let addContent = "- [ ] " + logItem
  77. content.splice(positionFound, 0 , addContent)
  78. await app.vault.modify(file, content.join("\n"))
  79. console.log('Task ' + logItem + ' added ' + ' in file ' + file.path + ' at position ' + positionFound)
  80.  
  81. //executeCommand()
  82. }
  83. } else {
  84. new Notice("Day Planner header not found");
  85. }
  86. } else {
  87. new Notice("Wrong task format (HH:mm event)")
  88. }
  89. } else {
  90. new Notice("Todays daily note does not exist.")
  91. }
  92. -%>
Add Comment
Please, Sign In to add comment