From d9f290cda665d82d133e148fee83c57dc83982f8 Mon Sep 17 00:00:00 2001 From: rooyca Date: Tue, 23 Apr 2024 14:23:47 -0500 Subject: [PATCH] update: use frontmatter variables on codeblock (#9) --- README.md | 12 +++++++++--- frontmatterUtils.ts | 20 ++++++++++++++++++++ main.ts | 28 ++++++++++++++++++++++++++-- manifest.json | 2 +- package.json | 2 +- 5 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 frontmatterUtils.ts diff --git a/README.md b/README.md index 954809b..0911e27 100644 --- a/README.md +++ b/README.md @@ -33,10 +33,10 @@ To use the plugin, create a code block with the language set to `req`. Inside th | Key| Description| Default| | ---| -----------|---------| -| url | The URL to send the request to| | +| url | The URL to send the request to (You can use variables defined in the frontmatter)| | | method | Request method (GET, POST, PUT, DELETE)| GET | -| body | Data to send with the request. Data should by in JSON format| | -| headers | Header(s) for the request. Data should by in JSON format| | +| body | Data to send with the request. Data should by in JSON format (You can use variables defined in the frontmatter)| | +| headers | Header(s) for the request. Data should by in JSON format (You can use variables defined in the frontmatter)| | | show | Response data to display. You can use a right arrow `->` to access nested objects| ALL | | format | Format in which the response should be displayed| {} | | response-type | The type of response we are getting (json, txt or md)| json | @@ -56,6 +56,12 @@ You can show the entire response by only specifying the url: url: https://jsonplaceholder.typicode.com/todos/1 ``` +You can use the frontmatter variables in the URL, for instance, if you have a variable `numb` defined in the frontmatter, you can use it like this: + +```req +url: https://jsonplaceholder.typicode.com/todos/{{this.numb}} +``` + You can specify the response type you are getting: ```req diff --git a/frontmatterUtils.ts b/frontmatterUtils.ts new file mode 100644 index 0000000..be14e91 --- /dev/null +++ b/frontmatterUtils.ts @@ -0,0 +1,20 @@ +import { parseYaml } from "obsidian"; + +export const FRONTMATTER_REGEX = /^\n*---[^\n]*\n+(?.+?)\n+---.*/s; + +export type Frontmatter = string | null | undefined; + +export function readFrontmatter(md: string) { + const result = md.match(FRONTMATTER_REGEX); + + return result?.groups?.fm; +} + +// throws: MetadataError +export function parseFrontmatter(input: Frontmatter) { + if (input === undefined || input === null) { + throw new Error("No hay frontmatter definido."); + } + + return parseYaml(input); +} \ No newline at end of file diff --git a/main.ts b/main.ts index a6c32b3..b53f48f 100644 --- a/main.ts +++ b/main.ts @@ -1,5 +1,26 @@ import { App, Editor, MarkdownView, Modal, Plugin, PluginSettingTab, Setting } from 'obsidian'; +import { readFrontmatter, parseFrontmatter } from './frontmatterUtils'; +export function checkFrontmatter(req_prop: string){ + const regex = /{{this\.([^{}]*)}}/g; + const match = req_prop.match(regex); + + if (match) { + const var_name = match[0].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(regex, frontmatterData[var_name] || ""); + return req_prop; + } catch (e) { + console.error(e.message); + return; + } + } + return req_prop; +} interface LoadAPIRSettings { URL: string; @@ -119,7 +140,8 @@ export default class MainAPIR extends Plugin { break; case lowercaseLine.includes("url: "): - URL = line.replace(/url: /i, ""); + URL = line.replace(/url: /i, ""); + URL = checkFrontmatter(URL); break; case lowercaseLine.includes("response-type"): @@ -136,11 +158,13 @@ export default class MainAPIR extends Plugin { break; case lowercaseLine.includes("headers: "): - headers = JSON.parse(line.replace(/headers: /i, "")); + headers = line.replace(/headers: /i, ""); + headers = JSON.parse(checkFrontmatter(headers)); break; case lowercaseLine.includes("body: "): body = line.replace(/body: /i, ""); + body = checkFrontmatter(body); break; case lowercaseLine.includes("format: "): diff --git a/manifest.json b/manifest.json index de33873..1c33b02 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "api-request", "name": "APIRequest", - "version": "1.1.2", + "version": "1.1.3", "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 541b870..933c8d3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "api-request", - "version": "1.1.2", + "version": "1.1.3", "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": {