Skip to content

Commit

Permalink
automatically include permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Exr0n committed Feb 15, 2022
1 parent 02d1e07 commit 62ec752
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
27 changes: 20 additions & 7 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { nanoid } from "nanoid";
import { Plugin, Notice, PluginSettingTab, App, Setting } from "obsidian";
import DOMPurify from "dompurify";
import SockJS from "sockjs-client";
import URLSafeBase64 from 'urlsafe-base64';
import zlib from 'zlib';

function getFileUrl(server: string, sess: string, file: string): string {
return `${server}kernel/${sess}/files/${file}`;
Expand Down Expand Up @@ -65,7 +67,7 @@ export default class ObsidianSage extends Plugin {
const code_disp = wrapper.createEl("pre");
code_disp.addClass('sagecell-display-code')
code_disp.innerText = src;
this.outputWriters[req_id] = new OutputWriter(wrapper, this.settings.displayByDefault);
this.outputWriters[req_id] = new OutputWriter(wrapper, this.settings.displayByDefault, src, this.settings.serverURL);
this.ws.send(`${this.session_id}/channels,${payload}`);
});
})
Expand Down Expand Up @@ -181,20 +183,31 @@ class OutputWriter {
target: HTMLElement
lastType: string
open: boolean
original_code: string // constant after init
serverURL: string // constant after init

constructor(target: HTMLElement, openByDefault: boolean) {
constructor(target: HTMLElement, openByDefault: boolean, original_code: string, serverURL: string) {
this.target = target;
this.lastType = "";
this.open = openByDefault;
this.original_code = original_code;
this.serverURL = serverURL;
}

initOutput() {
if (this.lastType !== "") return;
const output = this.target.createEl('details');
if (this.open) output.setAttribute("open", null);
const summary_text = output.createEl('summary');
summary_text.innerText = 'Execution Output';
const actual_output = output.createEl('div');
const wrapper = this.target.createEl('div');
const details = wrapper.createEl('details');
details.style.display = 'inline block';
if (this.open) details.setAttribute("open", null);
const summary_text = details.createEl('summary');
summary_text.innerText = 'Execution Output or ';
const always_visible_text = summary_text.createEl('span');
zlib.deflate(this.original_code, (err, buf) => {
if (err) new Prompt(`Failed to create SageMathCell permalink: ${err}`);
always_visible_text.innerHTML = `<a href="${this.serverURL}?z=${URLSafeBase64.encode(buf)}">view remote permalink</a>`;
})
const actual_output = details.createEl('div');
actual_output.addClass('sagecell-display-output');
this.outputEl = actual_output;
}
Expand Down
13 changes: 12 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@types/sockjs-client": "^1.5.1",
"dompurify": "^2.3.5",
"nanoid": "^3.2.0",
"sockjs-client": "^1.5.2"
"sockjs-client": "^1.5.2",
"urlsafe-base64": "^1.0.0"
}
}

0 comments on commit 62ec752

Please sign in to comment.