Skip to content

Commit

Permalink
update(feat): update frontmatter properties (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rooyca committed Jun 14, 2024
1 parent ba2ea2b commit c25996d
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 28 deletions.
15 changes: 11 additions & 4 deletions docs/docs/en/codeblocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Flags are the way to specify the parameters of our request and also the format i
| req-repeat | 1t@1s |
| notify-if | |
| save-to | |
| properties | |

### url

Expand Down Expand Up @@ -254,11 +255,17 @@ save-to: posts/1.json
```
~~~

### properties

!!! warning "To use this flag you need a JSON response and the `show` flag"

Specifies the frontmatter properties to update with the response. The data should be strings separated by commas.





~~~markdown
```req
url: https://jsonplaceholder.typicode.com/posts/1
show: id, title
properties: id, title
```
~~~

16 changes: 15 additions & 1 deletion docs/docs/es/codeblocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,18 @@ Especifica la ruta para guardar la respuesta. Guardará toda la respuesta. Se re
url: https://jsonplaceholder.typicode.com/posts/1
save-to: posts/1.json
```
~~~
~~~

### properties

!!! warning "Para usar esta bandera necesitas una respuesta de tipo JSON y la bandera `show`"

Especifica las propiedades del frontmatter que se actualizarán con la respuesta. Los datos deben ser cadenas separadas por comas.

~~~markdown
```req
url: https://jsonplaceholder.typicode.com/posts/1
show: id, title
properties: id, title
```
~~~
9 changes: 0 additions & 9 deletions docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,3 @@ nav:
- Uso:
- bloques de código: es/codeblocks.md
- configuraciones: es/settings.md

extra:
alternate:
- name: English
link: /en/
lang: en
- name: Spanish
link: /es/
lang: es
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "api-request",
"name": "APIRequest",
"version": "1.3.1",
"version": "1.3.2",
"minAppVersion": "0.15.0",
"description": "Fetch data from APIs or other sources. Responses in JSON, MD or HTML directly in your notes.",
"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": "api-request",
"version": "1.3.1",
"version": "1.3.2",
"description": "Fetch data from APIs or other sources. Responses in JSON, MD or HTML directly in your notes.",
"main": "main.js",
"scripts": {
Expand Down
50 changes: 38 additions & 12 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default class MainAPIR extends Plugin {
const sourceLines = source.split("\n");
let method = "GET", allowedMethods = ["GET", "POST", "PUT", "DELETE"], URL = "", show = "",
headers = {}, body = {}, format = "{}", responseType = "json", responseAllow = ["json", "other"], reqID = "req-general",
reqRepeat = { "times": 1, "every": 1000 }, notifyIf = "", saveTo = "";
reqRepeat = { "times": 1, "every": 1000 }, notifyIf = "", saveTo = "", properties = "";

for (const line of sourceLines) {
const lowercaseLine = line.toLowerCase();
Expand Down Expand Up @@ -145,6 +145,10 @@ export default class MainAPIR extends Plugin {
el.createEl("strong", { text: "Error: save-to value is empty. Please provide a filename" });
return;
}
} else if (lowercaseLine.includes("properties:")) {
properties = line.replace(/properties:/i, "");
properties = properties.replace(/\s/g, "");
properties = properties.split(",");
}
if (URL === "") {
el.createEl("strong", { text: "Error: URL not found" });
Expand Down Expand Up @@ -242,17 +246,39 @@ export default class MainAPIR extends Plugin {
}
}

const values = show.includes(",") ? show.split(",").map(key => {
let value = JSON.stringify(responseData.json[key.trim()]);
if (key.includes("->")) value = nestedValue(responseData, key);
return value;
}) : [show.trim().includes("->") ? nestedValue(responseData, show.trim()) : JSON.stringify(responseData.json[show.trim()])];
const replacedText = replaceOrder(format, values);
el.innerHTML = parser.parse(replacedText);

saveToID(reqID, replacedText);
addBtnCopy(el, replacedText);
}
// adding properties to frontmatter
if (properties) {
const showArray = show.split(",");
const propertiesArray = properties;
const activeView = this.app.workspace.getActiveViewOfType(MarkdownView);
const file = activeView.file;

showArray.forEach(async (key, index) => {
let val = "";
if (key.includes("->")) {
val = nestedValue(responseData, key);
} else if (responseData.json && responseData.json[key.trim()]) {
val = responseData.json[key.trim()];
}
const propertyName = propertiesArray[index].trim();
if (propertyName) {
await this.app.fileManager.processFrontMatter(file, (existingFrontmatter) => {
existingFrontmatter[propertyName] = val;
});
}
});
}
const values = show.includes(",") ? show.split(",").map(key => {
let value = responseData.json[key.trim()];
if (key.includes("->")) value = nestedValue(responseData, key);
return value;
}) : [show.trim().includes("->") ? nestedValue(responseData, show.trim()) : responseData.json[show.trim()]];
const replacedText = replaceOrder(format, values);
el.innerHTML = parser.parse(replacedText);

saveToID(reqID, replacedText);
addBtnCopy(el, replacedText);
}
} catch (error) {
console.error(error);
el.createEl("strong", { text: "Error: " + error.message });
Expand Down

0 comments on commit c25996d

Please sign in to comment.