Skip to content

Commit 18d213c

Browse files
committed
update(ref): take back way of rendering inline code
1 parent fb7074d commit 18d213c

File tree

3 files changed

+89
-89
lines changed

3 files changed

+89
-89
lines changed

Diff for: manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "query-json",
33
"name": "Query JSON",
4-
"version": "0.0.8",
4+
"version": "0.0.9",
55
"minAppVersion": "0.15.0",
66
"description": "Read, query and work with JSON.",
77
"author": "rooyca",

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "query-json",
3-
"version": "0.0.8",
3+
"version": "0.0.9",
44
"description": "Read, query and work with JSON.",
55
"main": "main.js",
66
"scripts": {

Diff for: src/main.ts

+87-87
Original file line numberDiff line numberDiff line change
@@ -112,99 +112,99 @@ export default class QJSON extends Plugin {
112112
statusBarItemEl.setText('QJSON: ' + qjCount);
113113
});
114114

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);
118118

119-
// const lastChar = line[line.length - 1];
119+
const lastChar = line[line.length - 1];
120120

121-
// const match = line.match(/@(.+)>(.+)|@(.+)>/);
122-
// if (!match) return;
121+
const match = line.match(/@(.+)>(.+)|@(.+)>/);
122+
if (!match) return;
123123

124-
// if (lastChar !== ';' && lastChar !== '.' && lastChar !== '>') return;
124+
if (lastChar !== ';' && lastChar !== '.' && lastChar !== '>') return;
125125

126-
// const id = match[1] || match[3];
127-
// let path = "";
126+
const id = match[1] || match[3];
127+
let path = "";
128128

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();
149149

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-
});
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+
// });
208208
}
209209

210210
}

0 commit comments

Comments
 (0)