diff --git a/manifest.json b/manifest.json index 535f253..9abd97a 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "api-request", "name": "APIRequest", - "version": "1.2.4", + "version": "1.2.5", "minAppVersion": "0.15.0", "description": "Request and retrieve data from APIs. The responses are delivered in a JSON format for easy integration with your notes.", "author": "rooyca", diff --git a/package.json b/package.json index a86d953..77ea367 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "api-request", - "version": "1.2.4", + "version": "1.2.5", "description": "Request and retrieve data from APIs. The responses are delivered in a JSON format for easy integration with your notes.", "main": "main.js", "scripts": { diff --git a/src/functions.ts b/src/functions.ts index f9301b3..f4bfe8c 100644 --- a/src/functions.ts +++ b/src/functions.ts @@ -1,30 +1,3 @@ -// Checks if the frontmatter is present in the request property -// If it is, it will replace the variable (this.VAR) with the frontmatter value -export function checkFrontmatter(req_prop: string) { - const regex = /{{this\.([^{}]*)}}/g; - const match = req_prop.match(regex); - - if (match) { - - for (let i = 0; i < match.length; i++) { - const var_name = match[i].replace(/{{this\.|}}/g, ""); - - const activeView = this.app.workspace.getActiveViewOfType(MarkdownView); - const markdownContent = activeView.editor.getValue(); - - try { - const frontmatterData = parseFrontmatter(readFrontmatter(markdownContent)); - req_prop = req_prop.replace(match[i], frontmatterData[var_name] || ""); - } catch (e) { - console.error(e.message); - new Notice("Error: " + e.message); - return; - } - } - } - return req_prop; -} - // Saves the response to the localStorage export function saveToID(reqID: any, reqText: any) { localStorage.setItem(reqID, reqText); diff --git a/src/main.ts b/src/main.ts index b66b3be..b900804 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,6 +2,7 @@ import { App, Editor, MarkdownView, Modal, Plugin, PluginSettingTab, Setting, No import { readFrontmatter, parseFrontmatter } from './frontmatterUtils'; import { MarkdownParser } from './mdparse'; import { checkFrontmatter, saveToID, addBtnCopy, replaceOrder, nestedValue, toDocument } from './functions'; +import { num_braces_regx, num_hyphen_regx, nums_rex, in_braces_regx, varname_regx, no_varname_regx } from './regx'; const parser = new MarkdownParser(); @@ -25,10 +26,37 @@ const DEFAULT_SETTINGS: LoadAPIRSettings = { Name: '', } +// Checks if the frontmatter is present in the request property +// If it is, it will replace the variable (this.VAR) with the frontmatter value +export function checkFrontmatter(req_prop: string) { + const match = req_prop.match(varname_regx); + + if (match) { + + for (let i = 0; i < match.length; i++) { + const var_name = match[i].replace(no_varname_regx, ""); + + const activeView = this.app.workspace.getActiveViewOfType(MarkdownView); + const markdownContent = activeView.editor.getValue(); + + try { + const frontmatterData = parseFrontmatter(readFrontmatter(markdownContent)); + req_prop = req_prop.replace(match[i], frontmatterData[var_name] || ""); + } catch (e) { + console.error(e.message); + new Notice("Error: " + e.message); + return; + } + } + } + return req_prop; +} + export default class MainAPIR extends Plugin { settings: LoadAPIRSettings; async onload() { + console.log('loading APIR'); await this.loadSettings(); this.addCommand({ @@ -116,33 +144,31 @@ export default class MainAPIR extends Plugin { addBtnCopy(el, el.innerText); } else { - const first_pattern = /{([^{}]*)}/g; - if (show.match(first_pattern)) { + if (show.match(in_braces_regx)) { if (show.includes(",")) { el.innerHTML = "Error: comma is not allowed when using {}"; return; } - const pattern = /{(\d+)\.\.(\d+)}/; let temp_show = ""; - if (show.match(pattern)) { - const range = show.match(/\d+/g).map(Number); + if (show.match(num_braces_regx)) { + const range = show.match(nums_rex).map(Number); if (range[0] > range[1]) { el.innerHTML = "Error: range is not valid"; return; } for (let i = range[0]; i <= range[1]; i++) { - temp_show += show.replace(show.match(pattern)[0], i) + ", "; - } - show = temp_show; - } else if (show.match(/(\d+-)+\d+/)) { - const numbers = show.match(/\d+/g).map(Number); - show = show.replace(/{.*?}/g, "-"); - for (let i = 0; i < numbers.length; i++) { - temp_show += show.replace("-", numbers[i]) + ", "; + temp_show += show.replace(show.match(num_braces_regx)[0], i) + ", "; } show = temp_show; + } else if (show.match(num_hyphen_regx)) { + const numbers = show.match(nums_rex).map(Number); + show = show.replace(in_braces_regx, "-"); + for (let i = 0; i < numbers.length; i++) { + temp_show += show.replace("-", numbers[i]) + ", "; + } + show = temp_show; } else { for (let i = 0; i < responseData.json.length; i++) { temp_show += show.replace("{..}", i) + ", "; diff --git a/src/regx.ts b/src/regx.ts new file mode 100644 index 0000000..09124d9 --- /dev/null +++ b/src/regx.ts @@ -0,0 +1,17 @@ +// Matches a specific pattern inside curly braces: {number1..number2} +export const num_braces_regx = /{(\d+)\.\.(\d+)}/; + +// Matches a pattern of digits separated by hyphens, e.g., 1-2-3-4 +export const num_hyphen_regx = /(\d+-)+\d+/; + +// Matches any sequence of digits, globally. +export const nums_rex = /\d+/g; + +// Matches anything inside curly braces, non-greedy, globally. +export const in_braces_regx = /{.*?}/g; + +// Matches any text inside double curly braces, preceded by "{{this." and followed by "}}" +export const varname_regx = /{{this\.([^{}]*)}}/g; + +// Matches "{{this." or "}}" globally +export const no_varname_regx = /{{this\.|}}/g;