Skip to content

Commit

Permalink
update: lightweight MD parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Rooyca committed May 8, 2024
1 parent 09bd2f1 commit 857344e
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 7 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,22 @@ You could also used inline:
But this is a little buggy and don't work all the time. (Use this for short and unformated responses)


#### How to remove responses from localStorage

In order to remove a response from the `localStorage` you can use the following code:

```dataviewjs
localStorage.removeItem("req-idPersona")
```
Or you can use the following code to remove all responses:

```dataviewjs
localStorage.clear()
```

**There is also a button in the settings to remove all responses.**


### With Configuration

To use the plugin, press `Ctrl+P` and search for "APIR". The plugin will present you with two options:
Expand Down
12 changes: 7 additions & 5 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { App, Editor, MarkdownView, Modal, Plugin, PluginSettingTab, Setting, Notice } from 'obsidian';
import { readFrontmatter, parseFrontmatter } from './frontmatterUtils';
import { marked } from './marked.min.js'
import './styles.css'
import { MarkdownParser } from './mdparse.js';
import './styles.css';

const parser = new MarkdownParser();

export function checkFrontmatter(req_prop: string){
const regex = /{{this\.([^{}]*)}}/g;
Expand Down Expand Up @@ -187,7 +189,7 @@ export default class MainAPIR extends Plugin {
if (sourceLines.includes("disabled")) {
const idExists = localStorage.getItem(reqID);
if (idExists) {
el.innerHTML = marked(idExists);
el.innerHTML = parser.parse(idExists);
return;
} else {
sourceLines.splice(sourceLines.indexOf("disabled"), 1);
Expand All @@ -210,7 +212,7 @@ export default class MainAPIR extends Plugin {
const responseData = await requestUrl({ url: URL, method, headers, body });
if (responseType !== "json") {
try {
el.innerHTML += marked(responseData.text);
el.innerHTML += parser.parse(responseData.text);
} catch (e) {
new Notice("Error: " + e.message);
el.innerHTML += responseData.text;
Expand Down Expand Up @@ -241,7 +243,7 @@ export default class MainAPIR extends Plugin {
return value;
}) : [show.trim().includes("->") ? nestedValue(responseData, show.trim()) : JSON.stringify(responseData.json[show.trim()])];
const replacedText = replaceOrder(format, values);
el.innerHTML += marked(replacedText);
el.innerHTML += parser.parse(replacedText);

saveToID(reqID, replacedText);
addBtnCopy(el, replacedText);
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.2.0",
"version": "1.2.1",
"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
75 changes: 75 additions & 0 deletions mdparse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
export class MarkdownParser {
parse(text) {
// Headers
text = text.replace(/^(#{1,6})\s*(.*)$/gm, (match, p1, p2) => `<h${p1.length}>${p2}</h${p1.length}>`);

// Todo Items
text = text.replace(/^- \[ \](.*)$/gm, '<li><input type="checkbox">$1</li>');
text = text.replace(/^- \[x\](.*)$/gm, '<li><input type="checkbox" checked>$1</li>');

// Unordered Lists
text = text.replace(/^\s*-\s*(.*)$/gm, '<li>$1</li>');
text = text.replace(/^\s*<li>(.*?)<\/li>\s*$/gm, '<ul>$1</ul>');

// Ordered Lists
text = text.replace(/^\s*\d+\.\s*(.*)$/gm, '<li>$1</li>');
text = text.replace(/^\s*<li>(.*?)<\/li>\s*$/gm, '<ol>$1</ol>');

// Bold
text = text.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');

// Italic
text = text.replace(/\*(.*?)\*/g, '<em>$1</em>');

// Images
text = text.replace(/\!\[([^\]]+)\]\(([^)]+)\)/g, '<img src="$2" alt="$1">');

// Links
text = text.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>');

// Blockquotes
text = text.replace(/^\s*>\s*(.*)$/gm, '<blockquote>$1</blockquote>');

// Code blocks
text = text.replace(/`([^`]+)`/g, '<code>$1</code>');

// Markdown Tables
text = this.mdTables(text);

return text;
}

mdTables(text) {
let table = '';
let regCheckPipe = /(\|)/gi;

text = text.trim();

if (text.match(regCheckPipe)) {
let rows = text.split('\n');
let header = rows.shift();
let headerCells = header.split('|').map(cell => cell.trim()).filter(cell => cell);
let tableStart = '<table>\n<thead>\n';
let tableEnd = '</thead>\n';
let bodyStart = '<tbody>\n';
let bodyEnd = '</tbody>\n';
let rowStart = '<tr>\n';
let rowEnd = '</tr>\n';
let cellStart = '<td>';
let cellEnd = '</td>\n';
let headerRow = rowStart + headerCells.map(cell => `<th>${cell}</th>`).join('\n') + rowEnd;
let bodyRows = rows.map(row => {
let cells = row.split('|').map(cell => cell.trim()).filter(cell => cell);
return rowStart + cells.map(cell => `<td>${cell}</td>`).join('\n') + rowEnd;
}).join('\n');

table += tableStart + headerRow + tableEnd + bodyStart + bodyRows + bodyEnd;
table = table.replace(/<td>[-]+<\/td>/g, '');

return table;
} else {
return text;
}

}
}
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.0",
"version": "1.2.1",
"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
6 changes: 6 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@
cursor: var(--cursor);
}

ul {
list-style-type: none;
padding: 0 !important;
margin: 0 !important;
}

0 comments on commit 857344e

Please sign in to comment.