Skip to content

Commit

Permalink
ref: inline query arrays(#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rooyca committed Jul 23, 2024
1 parent 7d64305 commit d8ecba1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ This flag suppresses the display of the identifier after rendering.

#### `#qj-desc` (optional)

Short for "id description," this flag provides a way to describe the JSON object. It is particularly useful for identifying the purpose of the object. The default value is `»»» Query JSON «««`.
Short for "id description," this flag provides a way to describe the JSON object. It is particularly useful for identifying the purpose of the object. The default value is `»»» QJSON «««`.

#### `#qj-show-json` (optional)

Expand Down
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.1.1",
"version": "0.1.2",
"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.1.1",
"version": "0.1.2",
"description": "Read, query and work with JSON.",
"main": "main.js",
"scripts": {
Expand Down
1 change: 0 additions & 1 deletion src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ function evaluateConditions(item, conditions, operators) {
}

function evaluateCondition(item, condition) {
console.log(item)
const { key, operator, value } = condition;
switch (operator) {
case '*': return true;
Expand Down
31 changes: 23 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default class QJSON extends Plugin {
desc = source.match(/#qj-desc: (.+)/);
if (desc) desc = desc[1];
} catch (e) {
desc = "»»» Query JSON «««";
desc = "»»» QJSON «««";
}
el.createEl('h3', { text: desc!, cls: 'centerQJtext' });
el.createEl('h4', { text: "ID: " + id, cls: 'centerQJtext' });
Expand All @@ -93,9 +93,7 @@ export default class QJSON extends Plugin {
const json = JSON.parse(source);

if (query) {
console.log(query);
const result = executeQuery(json, query);
console.log(result);

if (format && query[query.length - 1].type === "field") {
if (format === "list") {
Expand Down Expand Up @@ -143,17 +141,16 @@ export default class QJSON extends Plugin {
this.registerEvent(this.app.workspace.on('editor-change', async (editor) => {
const cursor = editor.getCursor();
const line = editor.getLine(cursor.line);

const lastChar = line[line.length - 1];

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

if (lastChar !== ';' && lastChar !== '.' && lastChar !== '>') return;
if (lastChar !== ';' && lastChar !== '.' && lastChar !== '>' && lastChar !== ']') return;

const id = match[1] || match[3];
let path = "";

if (match[2] !== undefined) {
path = match[2].slice(0, -1).replace(/>/, '')
}
Expand All @@ -168,13 +165,14 @@ export default class QJSON extends Plugin {
json = JSON.parse(await this.app.vault.adapter.read(id));
}

const value = getJSONPath(json, path);
path = path.replace(/\*/g, '').replace(/\[\]/g, '');
let 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)));

Expand All @@ -187,6 +185,23 @@ export default class QJSON extends Plugin {
}
}
} else {
if (line.includes('[*]')) {
const temp_data = [];
const arrayMatch = line.match(/(.+)\[\*\](?:\.(\w+))?/);
if (arrayMatch) {
const arrayKey = arrayMatch[1].slice(arrayMatch[1].indexOf('>') + 1);
const property = arrayMatch[2];
const arrayElements = getJSONPath(json, arrayKey);
for (let i = 0; i < arrayElements.length; i++) {
if (property) {
temp_data.push(arrayElements[i][property]);
} else {
temp_data.push(arrayElements[i]);
}
}
}
value = temp_data;
}
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);
Expand Down

0 comments on commit d8ecba1

Please sign in to comment.