Skip to content

Commit

Permalink
update(ref): move regex to a separate file(3)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rooyca committed May 20, 2024
1 parent 5a3f3bd commit 6f25b59
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 42 deletions.
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.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",
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.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": {
Expand Down
27 changes: 0 additions & 27 deletions src/functions.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
52 changes: 39 additions & 13 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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({
Expand Down Expand Up @@ -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) + ", ";
Expand Down
17 changes: 17 additions & 0 deletions src/regx.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 6f25b59

Please sign in to comment.