Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelNivard authored Nov 14, 2024
1 parent 0276ee2 commit 82bd102
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 64 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"id":"ridian","name":"Ridian","version":"0.0.5","minAppVersion":"0.15.0","description":"Execute R code blocks and display outputs and plots & render documents with Quarto within Obsidian.","author":"Michel Nivard","authorUrl":"https://github.com/MichelNivard/Ridian","isDesktopOnly":true}
{"id":"ridian","name":"Ridian","version":"0.0.7","minAppVersion":"0.15.0","description":"Execute R code blocks and display outputs and plots & render documents with Quarto within Obsidian.","author":"Michel Nivard","authorUrl":"https://github.com/MichelNivard/Ridian","isDesktopOnly":true}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Ridian",
"version": "0.0.6",
"version": "0.0.7",
"description": "Execute R code blocks and display outputs and plots within Obsidian.",
"main": "main.js",
"scripts": {
Expand Down
133 changes: 72 additions & 61 deletions src/RCodeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -638,72 +638,83 @@ options(device = function(...) jpeg(filename = tempfile(), width=800, height=600
return rProcess;
}


function escapeMarkdown(text: string): string {
return text.replace(/([\\`*_{}[\]()#+\-!>|])/g, '\\$1');
}

function insertOutputWithCallout(
editor: Editor,
endLine: number,
output: string,
imagePaths: string[],
widgetPaths: string[],
uniqueId: string,
options: { [key: string]: string }
) {
const contentLines: string[] = [];

if (output && output.trim() !== '') {
const outputLines = output.trim().split('\n').map((line) => '> ' + line);
contentLines.push(...outputLines);
}

imagePaths.forEach((imagePath) => {
const vaultImagePath = `${imagePath}`;
const imageMarkdown = `![center|480](${vaultImagePath})`;
contentLines.push(`> ${imageMarkdown}`);
});

widgetPaths.forEach((widgetPath) => {
const widgetMarkdown = `<iframe src="${widgetPath}" width="100%" height="680px"></iframe>`;
contentLines.push(`> ${widgetMarkdown}`);
});

if (contentLines.length === 0) {
return;
}

let outputContent = `> [!OUTPUT]+ {#output-${uniqueId}}\n`;
outputContent += contentLines.join('\n') + '\n';
outputContent += '> \n';

let existingOutputStart = -1;
let existingOutputEnd = -1;
const totalLines = editor.lineCount();

for (let i = 0; i < totalLines; i++) {
const line = editor.getLine(i);
if (line.trim() === `> [!OUTPUT]+ {#output-${uniqueId}}`) {
existingOutputStart = i;
existingOutputEnd = i;
while (existingOutputEnd + 1 < totalLines) {
const nextLine = editor.getLine(existingOutputEnd + 1);
if (!nextLine.startsWith('> ') && nextLine.trim() !== '') {
break;
}
existingOutputEnd++;
editor: Editor,
endLine: number,
output: string,
imagePaths: string[],
widgetPaths: string[],
uniqueId: string,
options: { [key: string]: string },
isError: boolean = false // Optional parameter to style errors differently
) {
const contentLines: string[] = [];

if (output && output.trim() !== '') {
// Escape markdown special characters in the output
const escapedOutput = escapeMarkdown(output.trim());
const outputLines = escapedOutput.split('\n').map((line) => '> ' + line);
contentLines.push(...outputLines);
}

imagePaths.forEach((imagePath) => {
const vaultImagePath = `${imagePath}`;
const imageMarkdown = `![centre|480](${vaultImagePath})`;
contentLines.push(`> ${imageMarkdown}`);
});

widgetPaths.forEach((widgetPath) => {
const widgetMarkdown = `<iframe src="${widgetPath}" width="100%" height="680px"></iframe>`;
contentLines.push(`> ${widgetMarkdown}`);
});

if (contentLines.length === 0) {
return;
}

// Use a different callout type or add a class for errors if needed
let calloutType = isError ? 'ERROR' : 'OUTPUT';
let outputContent = `> [!${calloutType}]+ {#output-${uniqueId}}\n`;
outputContent += contentLines.join('\n') + '\n';
outputContent += '> \n';

let existingOutputStart = -1;
let existingOutputEnd = -1;
const totalLines = editor.lineCount();

for (let i = 0; i < totalLines; i++) {
const line = editor.getLine(i);
if (line.trim() === `> [!${calloutType}]+ {#output-${uniqueId}}`) {
existingOutputStart = i;
existingOutputEnd = i;
while (existingOutputEnd + 1 < totalLines) {
const nextLine = editor.getLine(existingOutputEnd + 1);
if (!nextLine.startsWith('> ') && nextLine.trim() !== '') {
break;
}
break;
existingOutputEnd++;
}
}

if (existingOutputStart !== -1 && existingOutputEnd !== -1) {
const from = { line: existingOutputStart, ch: 0 };
const to = { line: existingOutputEnd + 1, ch: 0 };
editor.replaceRange(outputContent + '\n', from, to);
} else {
const insertPosition = { line: endLine + 1, ch: 0 };
editor.replaceRange('\n' + outputContent + '\n', insertPosition);
break;
}
}

function removeOutputCallout(editor: Editor, uniqueId: string) {

if (existingOutputStart !== -1 && existingOutputEnd !== -1) {
const from = { line: existingOutputStart, ch: 0 };
const to = { line: existingOutputEnd + 1, ch: 0 };
editor.replaceRange(outputContent + '\n', from, to);
} else {
const insertPosition = { line: endLine + 1, ch: 0 };
editor.replaceRange('\n' + outputContent + '\n', insertPosition);
}
}


function removeOutputCallout(editor: Editor, uniqueId: string) {
let existingOutputStart = -1;
let existingOutputEnd = -1;
const totalLines = editor.lineCount();
Expand Down
2 changes: 1 addition & 1 deletion versions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"0.0.6": "0.15.0"
"0.0.7": "0.15.0"
}

0 comments on commit 82bd102

Please sign in to comment.