Skip to content

Commit

Permalink
fix(#30): error when parsing response with commas
Browse files Browse the repository at this point in the history
  • Loading branch information
Rooyca committed Jul 8, 2024
1 parent a6a7c53 commit 1b6a637
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 12 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ The plugin can be installed from within Obsidian.
- [x] Save response to a file
- [x] Remove one by one from localStorage
- [x] Translate documentation
- [x] Add `render` flag to code-blocks
- [ ] Inline query from response
- [ ] Predefined requests
- [ ] GUI for code-blocks
- [ ] Add `render` flag to code-blocks


## ❤️ Sponsors

Expand Down
4 changes: 2 additions & 2 deletions docs/docs/es/codeblocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ res-type: json

## maketable

Converts the response into a table. It's useful when the response is an array of objects. This flags expects a list of titles separated by commas.
Convierte la respuesta en una tabla. Es útil cuando la respuesta es un array de objetos. Esta opción espera una lista de títulos separados por comas.

~~~markdown
```req
Expand All @@ -302,4 +302,4 @@ maketable: name, artist, stream
```
~~~

!!! note "In the example above, the response will be converted into a table with the titles `name`, `artist`, and `stream`."
!!! note "En el ejemplo anterior, la respuesta se convertirá en una tabla con los títulos `name`, `artist` y `stream`."
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.3.9",
"version": "1.4.0",
"minAppVersion": "0.15.0",
"description": "Fetch data from APIs or other sources. Responses in JSON, MD or HTML directly in 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.3.9",
"version": "1.4.0",
"description": "Fetch data from APIs or other sources. Responses in JSON, MD or HTML directly in your notes.",
"main": "main.js",
"scripts": {
Expand Down
16 changes: 10 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// CLEAN UP THIS MESS
// ---------------------------------------------

import { App, Editor, MarkdownView, Modal, Plugin, Notice, requestUrl, setIcon, debounce } from 'obsidian';
import { App, Editor, MarkdownView, Modal, Plugin, Notice, requestUrl, debounce } from 'obsidian';
import { readFrontmatter, parseFrontmatter } from 'src/functions/frontmatterUtils';
import { MarkdownParser } from 'src/functions/mdparse';
import { saveToID, addBtnCopy, replaceOrder, nestedValue, toDocument } from 'src/functions/general';
Expand Down Expand Up @@ -86,7 +86,7 @@ export default class MainAPIR extends Plugin {
}

// count the number of code-blocks
const markdownContent = this.app.workspace.getActiveViewOfType(MarkdownView)!.getViewData();
const markdownContent = this.app.workspace.getActiveViewOfType(MarkdownView)?.getViewData();
const codeBlocks = markdownContent.match(/```req/g)?.length || 0;
if (codeBlocks > 0) {
const item = this.addStatusBarItem();
Expand All @@ -108,7 +108,7 @@ export default class MainAPIR extends Plugin {
});

try {
this.registerMarkdownCodeBlockProcessor("req", async (source, el, ctx) => {
this.registerMarkdownCodeBlockProcessor("req", async (source, el) => {
const sourceLines = source.split("\n");
let [URL, show, saveTo, reqID, resType, maketable] = [String(), String(), String(), String(), String(), String()];
let [notifyIf, properties] = [[String()], [String()]];
Expand Down Expand Up @@ -314,6 +314,8 @@ export default class MainAPIR extends Plugin {
if (typeof current === "object") {
current = JSON.stringify(current, null, 2);
}
console.log(typeof current)
if (typeof current === 'string') current = encodeURIComponent(current);
result += current + ", ";
return;
}
Expand Down Expand Up @@ -374,7 +376,7 @@ export default class MainAPIR extends Plugin {
}
} else {
for (let i: number = range[0]; i <= range[1]; i++) {
temp_show += show.replace(numberBracesRegex![0], i.toString()) + ", ";
temp_show += show.replace(numberBracesRegex[0], i.toString()) + ", ";
}
show = temp_show;
}
Expand Down Expand Up @@ -467,12 +469,14 @@ export default class MainAPIR extends Plugin {
trBody = tbody.createEl("tr"); // Create a new row after every set of columns
}
const td = trBody.createEl("td");
td.createEl("strong", { text: value.trim() });
td.createEl("strong", { text: decodeURIComponent(value) });
});

return;
}

replacedText = decodeURIComponent(replacedText);

!render ? el.createEl("pre", { text: replacedText }) : el.innerHTML = parser.parse(sanitizer.SanitizeHtml(replacedText));

if (reqID) saveToID(reqID, replacedText);
Expand Down Expand Up @@ -511,7 +515,7 @@ export default class MainAPIR extends Plugin {
this.addCommand({
id: 'response-in-document-' + this.settings.URLs[i]["Name"],
name: 'Response for api: ' + this.settings.URLs[i]["Name"],
editorCallback: (editor: Editor, view: MarkdownView) => {
editorCallback: (editor: Editor) => {
const rea = this.settings.URLs[i];
toDocument(rea, this.settings.URLs[i]["DataResponse"], editor);
}
Expand Down

0 comments on commit 1b6a637

Please sign in to comment.