Skip to content

Commit

Permalink
update: use frontmatter variables on codeblock (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rooyca committed Apr 23, 2024
1 parent f70b0d2 commit d9f290c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 7 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand All @@ -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
Expand Down
20 changes: 20 additions & 0 deletions frontmatterUtils.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 26 additions & 2 deletions main.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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"):
Expand All @@ -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: "):
Expand Down
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.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",
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.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": {
Expand Down

0 comments on commit d9f290c

Please sign in to comment.