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 prod = (process.argv[2] === "production");
const context = await esbuild.context({ const context = await esbuild.context({
banner: { banner: {
js: banner, js: banner,
}, },
entryPoints: ["main.ts"], entryPoints: ["main.ts"],
bundle: true, bundle: true,
external: [ external: [
"obsidian", "obsidian",
"electron", "electron",
"@codemirror/autocomplete", "@codemirror/autocomplete",
"@codemirror/collab", "@codemirror/collab",
"@codemirror/commands", "@codemirror/commands",
"@codemirror/language", "@codemirror/language",
"@codemirror/lint", "@codemirror/lint",
"@codemirror/search", "@codemirror/search",
"@codemirror/state", "@codemirror/state",
"@codemirror/view", "@codemirror/view",
"@lezer/common", "@lezer/common",
"@lezer/highlight", "@lezer/highlight",
"@lezer/lr", "@lezer/lr",
...builtins], ...builtins],
format: "cjs", format: "cjs",
target: "es2018", target: "es2018",
logLevel: "info", logLevel: "info",
sourcemap: prod ? false : "inline", sourcemap: prod ? false : "inline",
treeShaking: true, treeShaking: true,
outfile: "main.js", outfile: "main.js",
}); });
if (prod) { if (prod) {
await context.rebuild(); await context.rebuild();
process.exit(0); process.exit(0);
} else { } else {
await context.watch(); await context.watch();
} }

159
main.ts
View File

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

View File

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