Skip to content

Commit

Permalink
update(ref): take back way of rendering inline code
Browse files Browse the repository at this point in the history
  • Loading branch information
Rooyca committed May 27, 2024
1 parent fb7074d commit 18d213c
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 89 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
174 changes: 87 additions & 87 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
// }
// }
// });
}

}
Expand Down

0 comments on commit 18d213c

Please sign in to comment.