update: use frontmatter variables on codeblock (#9)

This commit is contained in:
rooyca 2024-04-23 14:23:47 -05:00
parent f70b0d23f3
commit d9f290cda6
5 changed files with 57 additions and 7 deletions

View file

@ -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

20
frontmatterUtils.ts Normal file
View file

@ -0,0 +1,20 @@
import { parseYaml } from "obsidian";
export const FRONTMATTER_REGEX = /^\n*---[^\n]*\n+(?<fm>.+?)\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);
}

28
main.ts
View file

@ -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: "):

View file

@ -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",

View file

@ -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": {