diff --git a/manifest.json b/manifest.json index bb55256..26b5e76 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "query-json", "name": "Query JSON", - "version": "0.0.8", + "version": "0.0.9", "minAppVersion": "0.15.0", "description": "Read, query and work with JSON.", "author": "rooyca", diff --git a/package.json b/package.json index 5f7b173..84cbe75 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "query-json", - "version": "0.0.8", + "version": "0.0.9", "description": "Read, query and work with JSON.", "main": "main.js", "scripts": { diff --git a/src/main.ts b/src/main.ts index d8a6a01..d42529c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -112,99 +112,99 @@ export default class QJSON extends Plugin { statusBarItemEl.setText('QJSON: ' + qjCount); }); - // this.registerEvent(this.app.workspace.on('editor-change', async (editor, info) => { - // const cursor = editor.getCursor(); - // const line = editor.getLine(cursor.line); + this.registerEvent(this.app.workspace.on('editor-change', async (editor, info) => { + const cursor = editor.getCursor(); + const line = editor.getLine(cursor.line); - // const lastChar = line[line.length - 1]; + const lastChar = line[line.length - 1]; - // const match = line.match(/@(.+)>(.+)|@(.+)>/); - // if (!match) return; + const match = line.match(/@(.+)>(.+)|@(.+)>/); + if (!match) return; - // if (lastChar !== ';' && lastChar !== '.' && lastChar !== '>') return; + if (lastChar !== ';' && lastChar !== '.' && lastChar !== '>') return; - // const id = match[1] || match[3]; - // let path = ""; + const id = match[1] || match[3]; + let path = ""; - // if (match[2] !== undefined) { - // path = match[2].slice(0, -1).replace(/>/, '') - // } - - // let json; - - // if (!isNaN(parseInt(id)) && !id.includes('.json')) { - // const el = document.querySelector('.QJSON-' + id); - // if (!el) return; - // json = JSON.parse(el.innerText); - // } else { - // json = JSON.parse(await this.app.vault.adapter.read(id)); - // } - - // const value = getJSONPath(json, path); - - // if (lastChar !== ';') { - // if (value !== undefined) { - // const notice = document.querySelector('.notice'); - // if (notice) notice.remove(); + if (match[2] !== undefined) { + path = match[2].slice(0, -1).replace(/>/, '') + } + + let json; + + if (!isNaN(parseInt(id)) && !id.includes('.json')) { + const el = document.querySelector('.QJSON-' + id); + if (!el) return; + json = JSON.parse(el.innerText); + } else { + json = JSON.parse(await this.app.vault.adapter.read(id)); + } + + const value = getJSONPath(json, path); + + if (lastChar !== ';') { + if (value !== undefined) { + const notice = document.querySelector('.notice'); + if (notice) notice.remove(); - // const keys = Object.keys(value); - // const keysAreNumbers = keys.every(key => !isNaN(parseInt(key))); - - // if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') { - // new Notice(value.toString()); - // } else if (keysAreNumbers) { - // new Notice('Total Keys: ' + (keys.length- 1 )); - // } else { - // new Notice('Total Keys: ' + keys.length + '\n' + '____________' + '\n' + keys.join('\n')); - // } - // } - // } else { - // const atIndex = line.indexOf('@'); - // const replaceEnd = { line: cursor.line, ch: line.length }; // Replace to end of line - // const stringValue = typeof value === 'string' ? value : JSON.stringify(value); - // editor.replaceRange(stringValue, { line: cursor.line, ch: atIndex }, replaceEnd); - // } - // })); - - this.registerMarkdownPostProcessor( async (element, context) => { - const codeblocks = element.findAll("code"); - - for (let codeblock of codeblocks) { - if (codeblock.classList.length >= 1) return; - - const text = codeblock.innerText; - const regex = /@(.+)\.json>(.+);/; - const match = text.match(regex); - - if (match) { - let result; - - try { - let file = await this.app.vault.adapter.read(match[1] + ".json"); - file = JSON.parse(file); - result = getJSONPath(file, match[2]); - } catch (e) { - console.error(e); - new Notice("Error! Something went wrong!"); - result = "Error!"; - } - - let stringResult; - let tagName; - - if (typeof result === "string" || typeof result === "number" || typeof result === "boolean") { - stringResult = result; - tagName = "span"; - } else { - stringResult = JSON.stringify(result, null, 2); - tagName = "pre"; - } - - const resultEl = codeblock.createEl(tagName, {text: stringResult}); - codeblock.replaceWith(resultEl); - } - } - }); + const keys = Object.keys(value); + const keysAreNumbers = keys.every(key => !isNaN(parseInt(key))); + + if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') { + new Notice(value.toString()); + } else if (keysAreNumbers) { + new Notice('Total Keys: ' + (keys.length- 1 )); + } else { + new Notice('Total Keys: ' + keys.length + '\n' + '____________' + '\n' + keys.join('\n')); + } + } + } else { + const atIndex = line.indexOf('@'); + const replaceEnd = { line: cursor.line, ch: line.length }; // Replace to end of line + const stringValue = typeof value === 'string' ? value : JSON.stringify(value); + editor.replaceRange(stringValue, { line: cursor.line, ch: atIndex }, replaceEnd); + } + })); + + // this.registerMarkdownPostProcessor( async (element, context) => { + // const codeblocks = element.findAll("code"); + + // for (let codeblock of codeblocks) { + // if (codeblock.classList.length >= 1) return; + + // const text = codeblock.innerText; + // const regex = /@(.+)\.json>(.+);/; + // const match = text.match(regex); + + // if (match) { + // let result; + + // try { + // let file = await this.app.vault.adapter.read(match[1] + ".json"); + // file = JSON.parse(file); + // result = getJSONPath(file, match[2]); + // } catch (e) { + // console.error(e); + // new Notice("Error! Something went wrong!"); + // result = "Error!"; + // } + + // let stringResult; + // let tagName; + + // if (typeof result === "string" || typeof result === "number" || typeof result === "boolean") { + // stringResult = result; + // tagName = "span"; + // } else { + // stringResult = JSON.stringify(result, null, 2); + // tagName = "pre"; + // } + + // const resultEl = codeblock.createEl(tagName, {text: stringResult}); + // codeblock.replaceWith(resultEl); + // } + // } + // }); } }