Compare commits

..

No commits in common. "master" and "0.0.0" have entirely different histories.

4 changed files with 116 additions and 126 deletions

10
LICENSE
View File

@ -1,10 +0,0 @@
MIT License
Copyright (c) 2023 Aaron Manning
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -12,38 +12,37 @@ if you want to view the source, please visit the github repository of this plugi
const prod = (process.argv[2] === "production");
const context = await esbuild.context({
banner: {
js: banner,
},
entryPoints: ["main.ts"],
bundle: true,
external: [
"obsidian",
"electron",
"@codemirror/autocomplete",
"@codemirror/collab",
"@codemirror/commands",
"@codemirror/language",
"@codemirror/lint",
"@codemirror/search",
"@codemirror/state",
"@codemirror/view",
"@lezer/common",
"@lezer/highlight",
"@lezer/lr",
...builtins],
format: "cjs",
target: "es2018",
logLevel: "info",
sourcemap: prod ? false : "inline",
treeShaking: true,
outfile: "main.js",
banner: {
js: banner,
},
entryPoints: ["main.ts"],
bundle: true,
external: [
"obsidian",
"electron",
"@codemirror/autocomplete",
"@codemirror/collab",
"@codemirror/commands",
"@codemirror/language",
"@codemirror/lint",
"@codemirror/search",
"@codemirror/state",
"@codemirror/view",
"@lezer/common",
"@lezer/highlight",
"@lezer/lr",
...builtins],
format: "cjs",
target: "es2018",
logLevel: "info",
sourcemap: prod ? false : "inline",
treeShaking: true,
outfile: "main.js",
});
if (prod) {
await context.rebuild();
process.exit(0);
await context.rebuild();
process.exit(0);
} else {
await context.watch();
await context.watch();
}

159
main.ts
View File

@ -1,17 +1,17 @@
import {
App,
Plugin,
Vault,
Setting,
TAbstractFile,
TFile,
TFolder,
TextComponent,
App,
Plugin,
Vault,
Setting,
TAbstractFile,
TFile,
TFolder,
TextComponent,
SliderComponent,
ButtonComponent,
Notice,
MarkdownPostProcessorContext,
PluginSettingTab
ButtonComponent,
Notice,
MarkdownPostProcessorContext,
PluginSettingTab
} from 'obsidian';
import * as fs from 'fs';
@ -24,21 +24,21 @@ import ErrnoException = NodeJS.ErrnoException;
const TEMP_DIR = path.join(tmpdir(), 'obsidian-asy');
interface Settings {
library: string;
library: string;
timeout: number;
}
const DEFAULT_SETTINGS: Settings = {
library: '',
library: '',
timeout: 5000,
}
export default class ObsidianAsymptote extends Plugin {
settings: Settings;
settings: Settings;
async onload() {
this.settings = DEFAULT_SETTINGS;
this.settings = DEFAULT_SETTINGS;
this.registerMarkdownCodeBlockProcessor('asy', (source : string, el: HTMLElement, ctx: MarkdownPostProcessorContext) => {
this.renderAsyToContainer(source, el);
@ -48,7 +48,7 @@ export default class ObsidianAsymptote extends Plugin {
this.renderAsyToContainer(source, el);
});
this.addSettingTab(new ObsidianAsymptoteSettings(this.app, this));
this.addSettingTab(new ObsidianAsymptoteSettings(this.app, this));
}
renderAsyToContainer(source: string, container: HTMLElement) {
@ -59,7 +59,7 @@ export default class ObsidianAsymptote extends Plugin {
// Remove height and width so that it can be determined by CSS
svg[0].removeAttribute('width');
svg[0].removeAttribute('height');
container.classList.add('asy-rendered');
container.classList.add('asy-rendered');
resolve();
}).catch(err => {
let html = document.createElement('pre');
@ -79,42 +79,42 @@ export default class ObsidianAsymptote extends Plugin {
return new Promise((resolve, reject) => {
const dirPath = TEMP_DIR;
// Copy over library files
this.copyLibraryFiles();
// Copy over library files
this.copyLibraryFiles();
fs.mkdir(dirPath, (err: ErrnoException | null) => {
// File already exists error can be ignored
if (err === null || err?.code === 'EEXIST') {
fs.mkdir(dirPath, (err: ErrnoException | null) => {
// File already exists error can be ignored
if (err === null || err?.code === 'EEXIST') {
// Generate random number to prevent name collisions
const fileId = Math.floor(Math.random() * 10000000);
// Generate random number to prevent name collisions
const fileId = Math.floor(Math.random() * 10000000);
const inFileName = `input-${fileId}.asy`;
const outFileName = `output-${fileId}.svg`;
const inFileName = `input-${fileId}.asy`;
const outFileName = `output-${fileId}.svg`;
const inputPath = path.join(dirPath, inFileName);
const inputPath = path.join(dirPath, inFileName);
fs.writeFile(inputPath, source, (err: ErrnoException | null) => {
if (err) reject(err);
const command = 'asy "{input}" -o "{output}" -f svg'
.replace('{input}', inFileName)
.replace('{output}', outFileName);
fs.writeFile(inputPath, source, (err: ErrnoException | null) => {
if (err) reject(err);
const command = 'asy "{input}" -o "{output}" -f svg'
.replace('{input}', inFileName)
.replace('{output}', outFileName);
exec(command, {cwd: dirPath, timeout: this.settings.timeout}, (err: ExecException | null) => {
if (err) reject(err);
fs.readFile(path.join(dirPath, outFileName), function (err: ErrnoException | null, data: Buffer) {
if (err) reject(err)
resolve(data.toString());
});
});
});
}
})
exec(command, {cwd: dirPath, timeout: this.settings.timeout}, (err: ExecException | null) => {
if (err) reject(err);
fs.readFile(path.join(dirPath, outFileName), function (err: ErrnoException | null, data: Buffer) {
if (err) reject(err)
resolve(data.toString());
});
});
});
}
})
});
}
copyLibraryFiles() {
copyLibraryFiles() {
const folder = this.app.vault.getAbstractFileByPath(this.settings.library);
if (folder !== null && folder instanceof TFolder) {
Vault.recurseChildren(folder, async (child : TAbstractFile) => {
@ -132,13 +132,13 @@ export default class ObsidianAsymptote extends Plugin {
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class ObsidianAsymptoteSettings extends PluginSettingTab {
@ -155,33 +155,33 @@ class ObsidianAsymptoteSettings extends PluginSettingTab {
containerEl.empty();
new Setting(containerEl)
.setName('Library')
.setDesc('Folder of Asymptote library files (relative to vault root)')
.addText((text: TextComponent) => {
text
.setPlaceholder('Enter Path')
.setValue(this.plugin.settings.library)
.onChange(async (value) => {
this.plugin.settings.library = value;
await this.plugin.saveSettings();
})
});
.setName('Library')
.setDesc('Folder of Asymptote library files (relative to vault root)')
.addText((text: TextComponent) => {
text
.setPlaceholder('Enter Path')
.setValue(this.plugin.settings.library)
.onChange(async (value) => {
this.plugin.settings.library = value;
await this.plugin.saveSettings();
})
});
new Setting(containerEl)
.setName('Clean Temporary Directory')
.addButton((button: ButtonComponent) => {
button
.setButtonText('Clear')
.onClick((_: MouseEvent) => {
fs.rm(TEMP_DIR, { recursive: true, force: true }, (err: ErrnoException | null) => {
if (err === null) {
new Notice('Directory Cleared');
} else {
new Notice('Error Clearing Directory')
}
});
});
});
new Setting(containerEl)
.setName('Clean Temporary Directory')
.addButton((button: ButtonComponent) => {
button
.setButtonText('Clear')
.onClick((_: MouseEvent) => {
fs.rm(TEMP_DIR, { recursive: true, force: true }, (err: ErrnoException | null) => {
if (err === null) {
new Notice('Directory Cleared');
} else {
new Notice('Error Clearing Directory')
}
});
});
});
new Setting(containerEl)
.setName('Timeout (ms)')
@ -190,10 +190,11 @@ class ObsidianAsymptoteSettings extends PluginSettingTab {
.setLimits(0, 30000, 100)
.setValue(this.plugin.settings.timeout)
.onChange(async (value) => {
this.plugin.settings.timeout = value;
await this.plugin.saveSettings();
this.plugin.settings.timeout = value;
await this.plugin.saveSettings();
});
});
}
}

View File

@ -1,10 +1,10 @@
{
"id": "obsidian-asymptote",
"name": "Obsidian Asymptote",
"version": "0.0.0",
"minAppVersion": "0.15.0",
"description": "A plugin for rendering Asymptote drawings within notes.",
"author": "Aaron Manning",
"authorUrl": "https://aaronmanning.net",
"id": "obsidian-asymptote",
"name": "Obsidian Asymptote",
"version": "0.0.0",
"minAppVersion": "0.15.0",
"description": "A plugin for rendering Asymptote drawings within notes.",
"author": "Aaron Manning",
"authorUrl": "https://aaronmanning.net",
"isDesktopOnly": true
}