|
1 | 1 | import {
|
2 | 2 | MarkdownPostProcessor,
|
3 | 3 | MarkdownPostProcessorContext,
|
4 |
| - MarkdownPreviewRenderer, |
5 | 4 | Plugin,
|
6 | 5 | } from "obsidian";
|
7 | 6 |
|
8 | 7 | enum TaskType {
|
9 |
| - TODO, |
10 |
| - DONE, |
11 |
| - DOING, |
12 |
| - LATER, |
13 |
| - CANCELED, |
14 |
| - UNKNOWN, |
| 8 | + TODO = "TODO", |
| 9 | + DONE = "DONE", |
| 10 | + DOING = "DOING", |
| 11 | + LATER = "LATER", |
| 12 | + CANCELED = "CANCELED", |
| 13 | + UNKNOWN = "UNKNOWN", |
15 | 14 | }
|
16 | 15 |
|
17 |
| -const HEADING_REGEX = { |
18 |
| - h1: /(?:\s+)?- # (?:.*)$/gms, |
19 |
| - h2: /(?:\s+)?- ## (?:.*)$/gms, |
20 |
| - h3: /(?:\s+)?- ### (?:.*)$/gms, |
21 |
| - h4: /(?:\s+)?- #### (?:.*)$/gms, |
22 |
| - h5: /(?:\s+)?- ##### (?:.*)$/gms, |
23 |
| -}; |
24 |
| - |
25 |
| -const VERSION = "0.0.3"; |
26 |
| - |
27 |
| -function parseTaskType(content: string): TaskType | null { |
28 |
| - if (content.startsWith("DONE ")) { |
29 |
| - return TaskType.DONE; |
30 |
| - } else if (content.startsWith("TODO ")) { |
31 |
| - return TaskType.TODO; |
32 |
| - } else if (content.startsWith("DOING ")) { |
33 |
| - return TaskType.DOING; |
34 |
| - } else if (content.startsWith("LATER ")) { |
35 |
| - return TaskType.LATER; |
36 |
| - } else if (content.startsWith("CANCELED ")) { |
37 |
| - return TaskType.CANCELED; |
38 |
| - } else { |
39 |
| - return TaskType.UNKNOWN; |
40 |
| - } |
| 16 | +enum TaskCSSClass { |
| 17 | + COMPLETE = "logseq-complete-task", |
| 18 | + INCOMPLETE = "logseq-incomplete-task", |
| 19 | + KEYWORD = "logseq-keyword", |
41 | 20 | }
|
42 | 21 |
|
43 |
| -function removeTimestamps(content: string): string { |
44 |
| - return content |
45 |
| - .replace(/doing:: (?:\d{13})/gms, "") |
46 |
| - .replace(/done:: (?:\d{13})/gms, "") |
47 |
| - .replace(/todo:: (?:\d{13})/gms, "") |
48 |
| - .replace(/doing:: (?:\d{13})/gms, "") |
49 |
| - .replace(/later:: (?:\d{13})/gms, "") |
50 |
| - .replace(/canceled:: (?:\d{13})/gms, "") |
51 |
| - .replace( |
52 |
| - /id:: (?:[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12})/gims, |
53 |
| - "" |
54 |
| - ) |
55 |
| - .replace(/collapsed:: (?:true|false)/gms, "") |
56 |
| - .replace("<br>", ""); |
57 |
| -} |
| 22 | +const VERSION = "0.0.4"; |
58 | 23 |
|
59 |
| -const blockTest = new RegExp(/\#\+BEGIN_(WARNING|IMPORTANT|QUOTE|CAUTION)/gms); |
| 24 | +class LogSeqRegExes { |
| 25 | + static parseTaskType(content: string): TaskType { |
| 26 | + if (content.startsWith("DONE ")) { |
| 27 | + return TaskType.DONE; |
| 28 | + } else if (content.startsWith("TODO ")) { |
| 29 | + return TaskType.TODO; |
| 30 | + } else if (content.startsWith("DOING ")) { |
| 31 | + return TaskType.DOING; |
| 32 | + } else if (content.startsWith("LATER ")) { |
| 33 | + return TaskType.LATER; |
| 34 | + } else if (content.startsWith("CANCELED ")) { |
| 35 | + return TaskType.CANCELED; |
| 36 | + } else { |
| 37 | + return TaskType.UNKNOWN; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + static HEADING_REGEX = { |
| 42 | + h1: /(?:\s+)?- # (?:.*)$/gms, |
| 43 | + h2: /(?:\s+)?- ## (?:.*)$/gms, |
| 44 | + h3: /(?:\s+)?- ### (?:.*)$/gms, |
| 45 | + h4: /(?:\s+)?- #### (?:.*)$/gms, |
| 46 | + h5: /(?:\s+)?- ##### (?:.*)$/gms, |
| 47 | + }; |
60 | 48 |
|
61 |
| -function isBlock(content: string): boolean { |
62 |
| - return blockTest.test(content); |
| 49 | + static BEGIN_BLOCK_REGEX = new RegExp( |
| 50 | + /\#\+BEGIN_(WARNING|IMPORTANT|QUOTE|CAUTION)/gms |
| 51 | + ); |
| 52 | + static END_BLOCK_REGEX = new RegExp( |
| 53 | + /\#\+END_(WARNING|IMPORTANT|QUOTE|CAUTION)/gms |
| 54 | + ); |
| 55 | + |
| 56 | + static isBlock(content: string): boolean { |
| 57 | + return LogSeqRegExes.BEGIN_BLOCK_REGEX.test(content); |
| 58 | + } |
63 | 59 | }
|
64 | 60 |
|
65 |
| -function cmHeadingOverlay(cm: CodeMirror.Editor) { |
66 |
| - cm.addOverlay({ |
| 61 | +class CodeMirrorOverlays { |
| 62 | + static headingsOverlay = { |
67 | 63 | token: (stream: any) => {
|
68 |
| - if (stream.match(HEADING_REGEX["h1"])) { |
| 64 | + if (stream.match(LogSeqRegExes.HEADING_REGEX["h1"])) { |
69 | 65 | return "header-1";
|
70 |
| - } else if (stream.match(HEADING_REGEX["h2"])) { |
| 66 | + } else if (stream.match(LogSeqRegExes.HEADING_REGEX["h2"])) { |
71 | 67 | return "header-2";
|
72 |
| - } else if (stream.match(HEADING_REGEX["h3"])) { |
| 68 | + } else if (stream.match(LogSeqRegExes.HEADING_REGEX["h3"])) { |
73 | 69 | return "header-3";
|
74 |
| - } else if (stream.match(HEADING_REGEX["h4"])) { |
| 70 | + } else if (stream.match(LogSeqRegExes.HEADING_REGEX["h4"])) { |
75 | 71 | return "header-4";
|
76 |
| - } else if (stream.match(HEADING_REGEX["h5"])) { |
| 72 | + } else if (stream.match(LogSeqRegExes.HEADING_REGEX["h5"])) { |
77 | 73 | return "header-5";
|
78 | 74 | } else {
|
79 | 75 | stream.next();
|
80 | 76 | }
|
81 | 77 | },
|
82 |
| - }); |
| 78 | + }; |
| 79 | + static cmAddHeadingOverlay(cm: CodeMirror.Editor) { |
| 80 | + cm.addOverlay(CodeMirrorOverlays.headingsOverlay); |
| 81 | + } |
| 82 | + |
| 83 | + static cmRemoveHeadingOverlay(cm: CodeMirror.Editor) { |
| 84 | + cm.removeOverlay(CodeMirrorOverlays.headingsOverlay); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +function createKeywordElement(keyword: string): HTMLElement { |
| 89 | + const element = document.createElement("span"); |
| 90 | + element.classList.add(TaskCSSClass.KEYWORD); |
| 91 | + element.textContent = keyword; |
| 92 | + return element; |
| 93 | +} |
| 94 | + |
| 95 | +function createCheckboxElement(checked: boolean = false): HTMLElement { |
| 96 | + const element = document.createElement("input"); |
| 97 | + element.type = "checkbox"; |
| 98 | + element.checked = checked; |
| 99 | + return element; |
83 | 100 | }
|
84 | 101 |
|
85 | 102 | export default class LogSeqPlugin extends Plugin {
|
| 103 | + static removeProperties(content: string): string { |
| 104 | + return content |
| 105 | + .replace(/doing:: (?:\d{13})/, "") |
| 106 | + .replace(/done:: (?:\d{13})/, "") |
| 107 | + .replace(/todo:: (?:\d{13})/, "") |
| 108 | + .replace(/doing:: (?:\d{13})/, "") |
| 109 | + .replace(/later:: (?:\d{13})/, "") |
| 110 | + .replace(/canceled:: (?:\d{13})/, "") |
| 111 | + .replace( |
| 112 | + /id:: (?:[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12})/i, |
| 113 | + "" |
| 114 | + ) |
| 115 | + .replace(/collapsed:: (?:true|false)/gms, ""); |
| 116 | + } |
| 117 | + |
| 118 | + static processChildren(el: Element, keyword: string) { |
| 119 | + el.childNodes.forEach((child) => { |
| 120 | + if (child.nodeType == Node.TEXT_NODE) { |
| 121 | + if (child.nodeValue.startsWith(keyword)) { |
| 122 | + child.nodeValue = child.nodeValue.replace(keyword, ""); |
| 123 | + } |
| 124 | + child.nodeValue = LogSeqPlugin.removeProperties(child.nodeValue); |
| 125 | + } |
| 126 | + }); |
| 127 | + } |
| 128 | + |
| 129 | + static styleNode(el: Element, classname: TaskCSSClass) { |
| 130 | + el.querySelectorAll("li[data-line]").forEach((child) => { |
| 131 | + // Do not "complete" the child tasks, since this is LogSeq's behaviour |
| 132 | + child.classList.add(TaskCSSClass.INCOMPLETE); |
| 133 | + }); |
| 134 | + el.classList.add(classname); |
| 135 | + } |
| 136 | + |
86 | 137 | static postprocessor: MarkdownPostProcessor = (
|
87 | 138 | el: HTMLElement,
|
88 | 139 | ctx: MarkdownPostProcessorContext
|
89 | 140 | ) => {
|
90 | 141 | const entries = el.querySelectorAll("li[data-line]");
|
91 | 142 |
|
92 | 143 | entries.forEach((entry) => {
|
93 |
| - const taskType = parseTaskType(entry.textContent); |
94 |
| - |
95 | 144 | // Check if the entry is a org-mode block
|
96 |
| - if (isBlock(entry.innerHTML)) { |
| 145 | + if (LogSeqRegExes.isBlock(entry.innerHTML)) { |
97 | 146 | let replacedBlock = entry.innerHTML.replace(
|
98 |
| - /\#\+BEGIN_(WARNING|IMPORTANT|QUOTE|CAUTION)/, |
| 147 | + LogSeqRegExes.BEGIN_BLOCK_REGEX, |
99 | 148 | "<blockquote> ☟"
|
100 | 149 | );
|
101 | 150 | replacedBlock = replacedBlock.replace(
|
102 |
| - /\#\+END_(WARNING|IMPORTANT|QUOTE|CAUTION)/, |
| 151 | + LogSeqRegExes.END_BLOCK_REGEX, |
103 | 152 | "</blockquote>"
|
104 | 153 | );
|
105 | 154 | entry.innerHTML = replacedBlock;
|
106 | 155 | }
|
| 156 | + const taskType = LogSeqRegExes.parseTaskType(entry.textContent); |
107 | 157 |
|
108 | 158 | if (taskType == TaskType.DONE) {
|
109 |
| - const replacedHTML = removeTimestamps( |
110 |
| - entry.innerHTML.replace("DONE", "") |
111 |
| - ); |
112 |
| - entry.innerHTML = `<span class="logseq-done-task"><input type="checkbox" checked> ${replacedHTML}</span>`; |
| 159 | + LogSeqPlugin.processChildren(entry, TaskType.DONE); |
| 160 | + |
| 161 | + entry.insertAdjacentElement("afterbegin", createCheckboxElement(true)); |
| 162 | + LogSeqPlugin.styleNode(entry, TaskCSSClass.COMPLETE); |
113 | 163 | } else if (taskType == TaskType.TODO) {
|
114 |
| - const replacedHTML = removeTimestamps( |
115 |
| - entry.innerHTML.replace("TODO", "") |
| 164 | + LogSeqPlugin.processChildren(entry, TaskType.TODO); |
| 165 | + |
| 166 | + entry.insertAdjacentElement( |
| 167 | + "afterbegin", |
| 168 | + createKeywordElement(TaskType.TODO) |
116 | 169 | );
|
117 |
| - entry.innerHTML = `<input type="checkbox"> <span class="logseq-status-task">TODO</span> ${replacedHTML}`; |
| 170 | + |
| 171 | + entry.insertAdjacentElement("afterbegin", createCheckboxElement()); |
| 172 | + LogSeqPlugin.styleNode(entry, TaskCSSClass.INCOMPLETE); |
118 | 173 | } else if (taskType == TaskType.DOING) {
|
119 |
| - const replacedHTML = removeTimestamps( |
120 |
| - entry.innerHTML.replace("DOING", "") |
| 174 | + LogSeqPlugin.processChildren(entry, TaskType.DOING); |
| 175 | + |
| 176 | + entry.insertAdjacentElement( |
| 177 | + "afterbegin", |
| 178 | + createKeywordElement(TaskType.DOING) |
121 | 179 | );
|
122 |
| - entry.innerHTML = `<input type="checkbox"> <span class="logseq-status-task">DOING</span> ${replacedHTML}`; |
| 180 | + |
| 181 | + entry.insertAdjacentElement("afterbegin", createCheckboxElement()); |
123 | 182 | } else if (taskType == TaskType.LATER) {
|
124 |
| - const replacedHTML = removeTimestamps( |
125 |
| - entry.innerHTML.replace("LATER", "") |
| 183 | + LogSeqPlugin.processChildren(entry, TaskType.LATER); |
| 184 | + |
| 185 | + entry.insertAdjacentElement( |
| 186 | + "afterbegin", |
| 187 | + createKeywordElement(TaskType.LATER) |
126 | 188 | );
|
127 |
| - entry.innerHTML = `<input type="checkbox"> <span class="logseq-status-task">LATER</span> ${replacedHTML}`; |
| 189 | + |
| 190 | + entry.insertAdjacentElement("afterbegin", createCheckboxElement()); |
| 191 | + LogSeqPlugin.styleNode(entry, TaskCSSClass.INCOMPLETE); |
128 | 192 | } else if (taskType == TaskType.CANCELED) {
|
129 |
| - const replacedHTML = removeTimestamps( |
130 |
| - entry.innerHTML.replace("CANCELED", "") |
131 |
| - ); |
132 |
| - entry.innerHTML = `<span class="logseq-done-task">${replacedHTML}</span>`; |
| 193 | + LogSeqPlugin.processChildren(entry, TaskType.CANCELED); |
| 194 | + LogSeqPlugin.styleNode(entry, TaskCSSClass.COMPLETE); |
133 | 195 | }
|
134 | 196 | });
|
135 | 197 | };
|
136 | 198 |
|
137 | 199 | onload() {
|
138 |
| - console.log(`Loading LogSeq plugin ${VERSION}`); |
139 |
| - MarkdownPreviewRenderer.registerPostProcessor(LogSeqPlugin.postprocessor); |
| 200 | + console.log(`Loading logseq-compat plugin ${VERSION}`); |
| 201 | + this.registerMarkdownPostProcessor(LogSeqPlugin.postprocessor); |
140 | 202 | // Style headings in source editing
|
141 |
| - this.registerCodeMirror(cmHeadingOverlay); |
| 203 | + this.registerCodeMirror(CodeMirrorOverlays.cmAddHeadingOverlay); |
142 | 204 | }
|
143 | 205 |
|
144 | 206 | onunload() {
|
145 |
| - console.log(`unloading LogSeq plugin ${VERSION}`); |
146 |
| - MarkdownPreviewRenderer.unregisterPostProcessor(LogSeqPlugin.postprocessor); |
| 207 | + console.log(`unloading logseq-compat plugin ${VERSION}`); |
| 208 | + this.registerCodeMirror(CodeMirrorOverlays.cmRemoveHeadingOverlay); |
147 | 209 | }
|
148 | 210 | }
|
0 commit comments