@@ -112,99 +112,99 @@ export default class QJSON extends Plugin {
112
112
statusBarItemEl . setText ( 'QJSON: ' + qjCount ) ;
113
113
} ) ;
114
114
115
- // this.registerEvent(this.app.workspace.on('editor-change', async (editor, info) => {
116
- // const cursor = editor.getCursor();
117
- // const line = editor.getLine(cursor.line);
115
+ this . registerEvent ( this . app . workspace . on ( 'editor-change' , async ( editor , info ) => {
116
+ const cursor = editor . getCursor ( ) ;
117
+ const line = editor . getLine ( cursor . line ) ;
118
118
119
- // const lastChar = line[line.length - 1];
119
+ const lastChar = line [ line . length - 1 ] ;
120
120
121
- // const match = line.match(/@(.+)>(.+)|@(.+)>/);
122
- // if (!match) return;
121
+ const match = line . match ( / @ ( .+ ) > ( .+ ) | @ ( .+ ) > / ) ;
122
+ if ( ! match ) return ;
123
123
124
- // if (lastChar !== ';' && lastChar !== '.' && lastChar !== '>') return;
124
+ if ( lastChar !== ';' && lastChar !== '.' && lastChar !== '>' ) return ;
125
125
126
- // const id = match[1] || match[3];
127
- // let path = "";
126
+ const id = match [ 1 ] || match [ 3 ] ;
127
+ let path = "" ;
128
128
129
- // if (match[2] !== undefined) {
130
- // path = match[2].slice(0, -1).replace(/>/, '')
131
- // }
132
-
133
- // let json;
134
-
135
- // if (!isNaN(parseInt(id)) && !id.includes('.json')) {
136
- // const el = document.querySelector('.QJSON-' + id);
137
- // if (!el) return;
138
- // json = JSON.parse(el.innerText);
139
- // } else {
140
- // json = JSON.parse(await this.app.vault.adapter.read(id));
141
- // }
142
-
143
- // const value = getJSONPath(json, path);
144
-
145
- // if (lastChar !== ';') {
146
- // if (value !== undefined) {
147
- // const notice = document.querySelector('.notice');
148
- // if (notice) notice.remove();
129
+ if ( match [ 2 ] !== undefined ) {
130
+ path = match [ 2 ] . slice ( 0 , - 1 ) . replace ( / > / , '' )
131
+ }
132
+
133
+ let json ;
134
+
135
+ if ( ! isNaN ( parseInt ( id ) ) && ! id . includes ( '.json' ) ) {
136
+ const el = document . querySelector ( '.QJSON-' + id ) ;
137
+ if ( ! el ) return ;
138
+ json = JSON . parse ( el . innerText ) ;
139
+ } else {
140
+ json = JSON . parse ( await this . app . vault . adapter . read ( id ) ) ;
141
+ }
142
+
143
+ const value = getJSONPath ( json , path ) ;
144
+
145
+ if ( lastChar !== ';' ) {
146
+ if ( value !== undefined ) {
147
+ const notice = document . querySelector ( '.notice' ) ;
148
+ if ( notice ) notice . remove ( ) ;
149
149
150
- // const keys = Object.keys(value);
151
- // const keysAreNumbers = keys.every(key => !isNaN(parseInt(key)));
152
-
153
- // if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
154
- // new Notice(value.toString());
155
- // } else if (keysAreNumbers) {
156
- // new Notice('Total Keys: ' + (keys.length- 1 ));
157
- // } else {
158
- // new Notice('Total Keys: ' + keys.length + '\n' + '____________' + '\n' + keys.join('\n'));
159
- // }
160
- // }
161
- // } else {
162
- // const atIndex = line.indexOf('@');
163
- // const replaceEnd = { line: cursor.line, ch: line.length }; // Replace to end of line
164
- // const stringValue = typeof value === 'string' ? value : JSON.stringify(value);
165
- // editor.replaceRange(stringValue, { line: cursor.line, ch: atIndex }, replaceEnd);
166
- // }
167
- // }));
168
-
169
- this . registerMarkdownPostProcessor ( async ( element , context ) => {
170
- const codeblocks = element . findAll ( "code" ) ;
171
-
172
- for ( let codeblock of codeblocks ) {
173
- if ( codeblock . classList . length >= 1 ) return ;
174
-
175
- const text = codeblock . innerText ;
176
- const regex = / @ ( .+ ) \. j s o n > ( .+ ) ; / ;
177
- const match = text . match ( regex ) ;
178
-
179
- if ( match ) {
180
- let result ;
181
-
182
- try {
183
- let file = await this . app . vault . adapter . read ( match [ 1 ] + ".json" ) ;
184
- file = JSON . parse ( file ) ;
185
- result = getJSONPath ( file , match [ 2 ] ) ;
186
- } catch ( e ) {
187
- console . error ( e ) ;
188
- new Notice ( "Error! Something went wrong!" ) ;
189
- result = "Error!" ;
190
- }
191
-
192
- let stringResult ;
193
- let tagName ;
194
-
195
- if ( typeof result === "string" || typeof result === "number" || typeof result === "boolean" ) {
196
- stringResult = result ;
197
- tagName = "span" ;
198
- } else {
199
- stringResult = JSON . stringify ( result , null , 2 ) ;
200
- tagName = "pre" ;
201
- }
202
-
203
- const resultEl = codeblock . createEl ( tagName , { text : stringResult } ) ;
204
- codeblock . replaceWith ( resultEl ) ;
205
- }
206
- }
207
- } ) ;
150
+ const keys = Object . keys ( value ) ;
151
+ const keysAreNumbers = keys . every ( key => ! isNaN ( parseInt ( key ) ) ) ;
152
+
153
+ if ( typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' ) {
154
+ new Notice ( value . toString ( ) ) ;
155
+ } else if ( keysAreNumbers ) {
156
+ new Notice ( 'Total Keys: ' + ( keys . length - 1 ) ) ;
157
+ } else {
158
+ new Notice ( 'Total Keys: ' + keys . length + '\n' + '____________' + '\n' + keys . join ( '\n' ) ) ;
159
+ }
160
+ }
161
+ } else {
162
+ const atIndex = line . indexOf ( '@' ) ;
163
+ const replaceEnd = { line : cursor . line , ch : line . length } ; // Replace to end of line
164
+ const stringValue = typeof value === 'string' ? value : JSON . stringify ( value ) ;
165
+ editor . replaceRange ( stringValue , { line : cursor . line , ch : atIndex } , replaceEnd ) ;
166
+ }
167
+ } ) ) ;
168
+
169
+ // this.registerMarkdownPostProcessor( async (element, context) => {
170
+ // const codeblocks = element.findAll("code");
171
+
172
+ // for (let codeblock of codeblocks) {
173
+ // if (codeblock.classList.length >= 1) return;
174
+
175
+ // const text = codeblock.innerText;
176
+ // const regex = /@(.+)\.json>(.+);/;
177
+ // const match = text.match(regex);
178
+
179
+ // if (match) {
180
+ // let result;
181
+
182
+ // try {
183
+ // let file = await this.app.vault.adapter.read(match[1] + ".json");
184
+ // file = JSON.parse(file);
185
+ // result = getJSONPath(file, match[2]);
186
+ // } catch (e) {
187
+ // console.error(e);
188
+ // new Notice("Error! Something went wrong!");
189
+ // result = "Error!";
190
+ // }
191
+
192
+ // let stringResult;
193
+ // let tagName;
194
+
195
+ // if (typeof result === "string" || typeof result === "number" || typeof result === "boolean") {
196
+ // stringResult = result;
197
+ // tagName = "span";
198
+ // } else {
199
+ // stringResult = JSON.stringify(result, null, 2);
200
+ // tagName = "pre";
201
+ // }
202
+
203
+ // const resultEl = codeblock.createEl(tagName, {text: stringResult});
204
+ // codeblock.replaceWith(resultEl);
205
+ // }
206
+ // }
207
+ // });
208
208
}
209
209
210
210
}
0 commit comments