Newer
Older
TriliumScripts / highlight_moodlog.js
0xRoM on 21 Mar 6 KB initial commit
  1. const TPL = `<div style="padding: 10px; border-top: 1px solid var(--main-border-color); contain: none;">
  2. <strong>Moodlog Highlighting Applied</strong>
  3. </div>`;
  4.  
  5. class MoodlogHighlighterWidget extends api.NoteContextAwareWidget {
  6. static get parentWidget() {
  7. //console.log("MDLG: Getting parent widget: center-pane");
  8. return 'center-pane';
  9. }
  10.  
  11. get position() {
  12. //console.log("MDLG: Setting position: 100");
  13. return 100;
  14. }
  15.  
  16. isEnabled() {
  17. const enabled = super.isEnabled()
  18. && this.note.type === 'text'
  19. && this.note.hasLabel('moodlog');
  20. //console.log(`MDLG: isEnabled: ${enabled}`);
  21. return enabled;
  22. }
  23.  
  24. doRender() {
  25. //console.log("MDLG: Rendering widget...");
  26. this.$widget = $(TPL);
  27. //console.log("MDLG: Widget rendered:", this.$widget);
  28. return this.$widget;
  29. }
  30.  
  31. async refreshWithNote(note) {
  32. //console.log("MDLG: Refreshing with note:", note);
  33. const { content } = await note.getNoteComplement();
  34. //console.log("MDLG: Note content fetched:", content);
  35.  
  36. // Check if content is fetched properly
  37. if (!content) {
  38. //console.log("MDLG: No content fetched. Exiting refresh.");
  39. return;
  40. }
  41.  
  42. const highlightedContent = this.applyHighlighting(content);
  43. //console.log("MDLG: Highlighted content:", highlightedContent);
  44.  
  45. if (highlightedContent !== content) {
  46. //console.log("MDLG: Updating note content with highlighted version");
  47.  
  48. // Use the setNoteContent function to update the note content
  49. try {
  50. await setNoteContent(note.noteId, highlightedContent);
  51. //console.log("MDLG: Content updated with highlighted version");
  52. } catch (error) {
  53. //console.error("MDLG: Error updating content:", error);
  54. }
  55. } else {
  56. //console.log("MDLG: No changes to content, skipping update.");
  57. }
  58. }
  59.  
  60.  
  61. applyHighlighting(content) {
  62. //console.log("MDLG: Applying highlighting to content...");
  63.  
  64. // Replace <br> with \n, then remove all other HTML tags
  65. const plainTextContent = content.replace(/<br\s*\/?>/gi, '\n').replace(/<[^>]*>/g, ' ');
  66. //console.log("MDLG: Stripped content:", plainTextContent);
  67.  
  68. // Combined regex for date & time
  69. const dateTimePattern = /([1-2]\d{3}[-/.](0?[1-9]|1[0-2])[-/.](0[1-9]|[12]\d|3[01]))\s+(\d{1,2}:\d{2})/g;
  70. const projectPattern = /\[\S+?\]/g;
  71. const contextPattern = /(\s|^)(@\S+)/g;
  72. const hashtagPattern = /(\s|^)(#\S+)/g;
  73. const positivePattern = /(\s|^)(\+\S+)/g;
  74. const negativePattern = /(\s|^)(-\S+)/g;
  75.  
  76. // Escape HTML characters to prevent issues with replacements
  77. function escapeHtml(str) {
  78. return str.replace(/[&<>"']/g, function (char) {
  79. return {
  80. '&': '&amp;',
  81. '<': '&lt;',
  82. '>': '&gt;',
  83. '"': '&quot;',
  84. "'": '&#039;'
  85. }[char];
  86. });
  87. }
  88.  
  89. // Helper function to map the number to a colour based on the gradient
  90. function getProjectColour(number) {
  91. const minColor = { r: 107, g: 109, b: 229 }; // #6b6de5
  92. const maxColor = { r: 70, g: 226, b: 96 }; // #46e260
  93.  
  94. const ratio = number / 9; // since we are working with 0-9
  95.  
  96. // Interpolate the RGB values between minColor and maxColor
  97. const r = Math.round(minColor.r + ratio * (maxColor.r - minColor.r));
  98. const g = Math.round(minColor.g + ratio * (maxColor.g - minColor.g));
  99. const b = Math.round(minColor.b + ratio * (maxColor.b - minColor.b));
  100.  
  101. return `rgb(${r}, ${g}, ${b})`;
  102. }
  103. // Process each line of the plain text content
  104. let highlightedLines = plainTextContent.split("\n").map(line => {
  105. //console.log("MDLG: Processing line:", line);
  106.  
  107. // Apply combined date & time highlight
  108. line = line.replace(dateTimePattern, `<span style="color: rgb(188, 55, 237);">$1</span> <span style="color: rgb(188, 55, 237);">$4</span>`);
  109.  
  110. // Apply other highlights
  111. line = line.replace(projectPattern, (match) => {
  112. // Extract the number inside square brackets
  113. const number = parseInt(match.match(/\d+/)[0], 10);
  114. const colour = getProjectColour(number);
  115. return `<span style="color: ${colour}; font-weight: bold;">${escapeHtml(match)}</span>`;
  116. });
  117. // Apply other highlights
  118. //line = line.replace(projectPattern, `<span style="color: rgb(224, 218, 36); font-weight: bold;">$&</span>`);
  119. line = line.replace(contextPattern, (_, space, tag) => `${space}<span style="color: rgb(255 165 10);">${escapeHtml(tag)}</span>`);
  120. line = line.replace(hashtagPattern, (_, space, tag) => `${space}<span style="color: rgb(41, 229, 242);">${escapeHtml(tag)}</span>`);
  121. line = line.replace(positivePattern, (_, space, tag) => `${space}<span style="color: rgb(44, 227, 34);">${escapeHtml(tag)}</span>`);
  122. line = line.replace(negativePattern, (_, space, tag) => `${space}<span style="color: rgb(230, 59, 44);">${escapeHtml(tag)}</span>`);
  123.  
  124. return line.trim();
  125. });
  126.  
  127. // Join lines with proper <br> handling
  128. let finalContent = highlightedLines.join("<br>");
  129.  
  130. //console.log("MDLG: Highlighted content:", finalContent);
  131. return finalContent;
  132. }
  133.  
  134.  
  135.  
  136.  
  137. async entitiesReloadedEvent({ loadResults }) {
  138. //console.log("MDLG: Entities reloaded event triggered");
  139. if (loadResults.isNoteContentReloaded(this.noteId)) {
  140. //console.log("MDLG: Note content reloaded, refreshing widget...");
  141. //this.refresh();
  142. } else {
  143. //console.log("MDLG: No content reload detected.");
  144. }
  145. }
  146.  
  147. }
  148.  
  149. console.log("MDLG: Starting Moodlog Highlighter Widget");
  150. module.exports = MoodlogHighlighterWidget;
  151.  
  152. function setNoteContent(noteId, content) {
  153. /*const noteElement = document.querySelector(`note-detail-readonly-text-content`);
  154.  
  155. if (noteElement) {
  156. noteElement.innerHTML = content; // Update the displayed content
  157. console.log(`MDLG: Updated note ${noteId} in the DOM.`);
  158. } else {
  159. console.warn(`MDLG: Note element with data-note-id='${noteId}' not found.`);
  160. }*/
  161. return api.runOnBackend((id, data) => api.getNote(id).setContent(data), [noteId, content]);
  162. }
Buy Me A Coffee