From c25996d2a12655f27234ed09c901f0524343802b Mon Sep 17 00:00:00 2001 From: rooyca Date: Fri, 14 Jun 2024 16:27:26 -0500 Subject: [PATCH] update(feat): update frontmatter properties (#24) --- docs/docs/en/codeblocks.md | 15 +++++++++--- docs/docs/es/codeblocks.md | 16 +++++++++++- docs/mkdocs.yml | 9 ------- manifest.json | 2 +- package.json | 2 +- src/main.ts | 50 +++++++++++++++++++++++++++++--------- 6 files changed, 66 insertions(+), 28 deletions(-) diff --git a/docs/docs/en/codeblocks.md b/docs/docs/en/codeblocks.md index d11fbdc..0e2a7d0 100644 --- a/docs/docs/en/codeblocks.md +++ b/docs/docs/en/codeblocks.md @@ -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 @@ -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 +``` +~~~ diff --git a/docs/docs/es/codeblocks.md b/docs/docs/es/codeblocks.md index 0c344b9..5cbddd1 100644 --- a/docs/docs/es/codeblocks.md +++ b/docs/docs/es/codeblocks.md @@ -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 ``` -~~~ \ No newline at end of file +~~~ + +### 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 +``` +~~~ diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index d8164dc..9460903 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -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 diff --git a/manifest.json b/manifest.json index 52bdd5d..830c913 100644 --- a/manifest.json +++ b/manifest.json @@ -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", diff --git a/package.json b/package.json index c36ba4d..77e5d63 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/main.ts b/src/main.ts index e546cd5..dd432f5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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(); @@ -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" }); @@ -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 });