initial commit
This commit is contained in:
commit
6cb2d83195
8
.gitignore
vendored
Normal file
8
.gitignore
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
# npm
|
||||
node_modules
|
||||
|
||||
# compiled files
|
||||
main.js
|
||||
|
||||
# obsidian
|
||||
data.json
|
40
README.md
Normal file
40
README.md
Normal file
@ -0,0 +1,40 @@
|
||||
# Obsidian Asymptote
|
||||
An Obsidian plugin for rendering [Asymptote](https://asymptote.sourceforge.io/) drawings within notes.
|
||||
|
||||
## Demo
|
||||
````md
|
||||
# Asymptote Demo
|
||||
This note shows how you can use Asymptote to draw images directly in notes.
|
||||
|
||||
See for example this picture:
|
||||
|
||||
```asy
|
||||
unitsize(1cm);
|
||||
|
||||
path radius = (0, 0) -- (1 / sqrt(2), 1 / sqrt(2));
|
||||
currentpen = currentpen + white;
|
||||
// Shading
|
||||
fill(unitcircle, p = colorless(currentpen) + opacity(0.2) + dashed + blue);
|
||||
// Boundary
|
||||
draw(unitcircle, p = colorless(currentpen) + dashed + red);
|
||||
// Radius
|
||||
draw(radius, green, L = scale(0.5) * Label("$r$"));
|
||||
// Centre
|
||||
dot((0, 0), L = scale(0.5) * Label("$x_0$"));
|
||||
```
|
||||
````
|
||||
|
||||
## Building From Source
|
||||
```
|
||||
> npm i
|
||||
> npm run build
|
||||
```
|
||||
|
||||
## Dependencies
|
||||
Due to Asymptote not having any real support for use as a pure library, this plugin works by calling an Asymptote installation on a file created in the file system's temporary directory.
|
||||
|
||||
As such, this plugin is not standalone, and [Asymptote](https://asymptote.sourceforge.io/binaries.html) must be installed.
|
||||
|
||||
## Styling
|
||||
Asymptote SVGs have their `width` and `height` properties stripped so that they can be independently styled using CSS. See `styles.css` for an example. In short, `asy-rendered` is the class of the `div` that directly wraps the child `svg` with the drawing itself.
|
||||
|
48
esbuild.config.mjs
Normal file
48
esbuild.config.mjs
Normal file
@ -0,0 +1,48 @@
|
||||
import esbuild from "esbuild";
|
||||
import process from "process";
|
||||
import builtins from "builtin-modules";
|
||||
|
||||
const banner =
|
||||
`/*
|
||||
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
|
||||
if you want to view the source, please visit the github repository of this plugin
|
||||
*/
|
||||
`;
|
||||
|
||||
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",
|
||||
});
|
||||
|
||||
if (prod) {
|
||||
await context.rebuild();
|
||||
process.exit(0);
|
||||
} else {
|
||||
await context.watch();
|
||||
}
|
200
main.ts
Normal file
200
main.ts
Normal file
@ -0,0 +1,200 @@
|
||||
import {
|
||||
App,
|
||||
Plugin,
|
||||
Vault,
|
||||
Setting,
|
||||
TAbstractFile,
|
||||
TFile,
|
||||
TFolder,
|
||||
TextComponent,
|
||||
SliderComponent,
|
||||
ButtonComponent,
|
||||
Notice,
|
||||
MarkdownPostProcessorContext,
|
||||
PluginSettingTab
|
||||
} from 'obsidian';
|
||||
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import {exec, ExecException} from 'child_process';
|
||||
import {tmpdir} from "os";
|
||||
import ErrnoException = NodeJS.ErrnoException;
|
||||
|
||||
// Temporary directory for asymptote input output
|
||||
const TEMP_DIR = path.join(tmpdir(), 'obsidian-asy');
|
||||
|
||||
interface Settings {
|
||||
library: string;
|
||||
timeout: number;
|
||||
}
|
||||
|
||||
const DEFAULT_SETTINGS: Settings = {
|
||||
library: '',
|
||||
timeout: 5000,
|
||||
}
|
||||
|
||||
export default class ObsidianAsymptote extends Plugin {
|
||||
|
||||
settings: Settings;
|
||||
|
||||
async onload() {
|
||||
this.settings = DEFAULT_SETTINGS;
|
||||
|
||||
this.registerMarkdownCodeBlockProcessor('asy', (source : string, el: HTMLElement, ctx: MarkdownPostProcessorContext) => {
|
||||
this.renderAsyToContainer(source, el);
|
||||
});
|
||||
|
||||
this.registerMarkdownCodeBlockProcessor('asymptote', (source : string, el: HTMLElement, ctx: MarkdownPostProcessorContext) => {
|
||||
this.renderAsyToContainer(source, el);
|
||||
});
|
||||
|
||||
this.addSettingTab(new ObsidianAsymptoteSettings(this.app, this));
|
||||
}
|
||||
|
||||
renderAsyToContainer(source: string, container: HTMLElement) {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
this.renderAsyToSVG(source).then((data: string) => {
|
||||
container.innerHTML = data;
|
||||
const svg = container.getElementsByTagName('svg');
|
||||
// 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');
|
||||
resolve();
|
||||
}).catch(err => {
|
||||
let html = document.createElement('pre');
|
||||
if (err.signal === 'SIGTERM') {
|
||||
html.innerText = 'Child process got terminated. Perhaps try adjusting the timeout?';
|
||||
} else {
|
||||
html.innerText = err.toString();
|
||||
}
|
||||
|
||||
container.appendChild(html);
|
||||
reject(err);
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
renderAsyToSVG(source: string) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const dirPath = TEMP_DIR;
|
||||
|
||||
// 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') {
|
||||
|
||||
// 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 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);
|
||||
|
||||
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() {
|
||||
const folder = this.app.vault.getAbstractFileByPath(this.settings.library);
|
||||
if (folder !== null && folder instanceof TFolder) {
|
||||
Vault.recurseChildren(folder, async (child : TAbstractFile) => {
|
||||
if (child instanceof TFile) {
|
||||
const file = child as TFile;
|
||||
if (file.extension === 'asy') {
|
||||
const libFile = await this.app.vault.read(file);
|
||||
fs.writeFileSync(path.join(TEMP_DIR, file.name), libFile);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
onunload() {
|
||||
}
|
||||
|
||||
async loadSettings() {
|
||||
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
|
||||
}
|
||||
|
||||
async saveSettings() {
|
||||
await this.saveData(this.settings);
|
||||
}
|
||||
}
|
||||
|
||||
class ObsidianAsymptoteSettings extends PluginSettingTab {
|
||||
plugin: ObsidianAsymptote;
|
||||
|
||||
constructor(app: App, plugin: ObsidianAsymptote) {
|
||||
super(app, plugin);
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
display(): void {
|
||||
const {containerEl} = this;
|
||||
|
||||
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();
|
||||
})
|
||||
});
|
||||
|
||||
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)')
|
||||
.addSlider((slider : SliderComponent) => {
|
||||
slider
|
||||
.setLimits(0, 30000, 100)
|
||||
.setValue(this.plugin.settings.timeout)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.timeout = value;
|
||||
await this.plugin.saveSettings();
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
10
manifest.json
Normal file
10
manifest.json
Normal file
@ -0,0 +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",
|
||||
"isDesktopOnly": true
|
||||
}
|
2203
package-lock.json
generated
Normal file
2203
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
26
package.json
Normal file
26
package.json
Normal file
@ -0,0 +1,26 @@
|
||||
{
|
||||
"name": "obsidian-asymptote",
|
||||
"version": "0.0.0",
|
||||
"description": "A plugin for rendering Asymptote drawings within notes.",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
"dev": "node esbuild.config.mjs",
|
||||
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production",
|
||||
"version": "node version-bump.mjs && git add manifest.json versions.json"
|
||||
},
|
||||
"keywords": ["Obsidian"],
|
||||
"author": "Aaron Manning",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@types/node": "^16.11.6",
|
||||
"@typescript-eslint/eslint-plugin": "5.29.0",
|
||||
"@typescript-eslint/parser": "5.29.0",
|
||||
"obsidian": "latest",
|
||||
"esbuild": "0.17.3",
|
||||
"tslib": "2.4.0",
|
||||
"builtin-modules": "3.3.0",
|
||||
"typescript": "4.7.4"
|
||||
}
|
||||
}
|
||||
|
||||
|
4
styles.css
Normal file
4
styles.css
Normal file
@ -0,0 +1,4 @@
|
||||
.asy-rendered {
|
||||
width: 50%;
|
||||
margin: auto;
|
||||
}
|
24
tsconfig.json
Normal file
24
tsconfig.json
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"baseUrl": ".",
|
||||
"inlineSourceMap": true,
|
||||
"inlineSources": true,
|
||||
"module": "ESNext",
|
||||
"target": "ES6",
|
||||
"allowJs": true,
|
||||
"noImplicitAny": true,
|
||||
"moduleResolution": "node",
|
||||
"importHelpers": true,
|
||||
"isolatedModules": true,
|
||||
"strictNullChecks": true,
|
||||
"lib": [
|
||||
"DOM",
|
||||
"ES5",
|
||||
"ES6",
|
||||
"ES7"
|
||||
]
|
||||
},
|
||||
"include": [
|
||||
"**/*.ts"
|
||||
]
|
||||
}
|
14
version-bump.mjs
Normal file
14
version-bump.mjs
Normal file
@ -0,0 +1,14 @@
|
||||
import { readFileSync, writeFileSync } from "fs";
|
||||
|
||||
const targetVersion = process.env.npm_package_version;
|
||||
|
||||
// read minAppVersion from manifest.json and bump version to target version
|
||||
let manifest = JSON.parse(readFileSync("manifest.json", "utf8"));
|
||||
const { minAppVersion } = manifest;
|
||||
manifest.version = targetVersion;
|
||||
writeFileSync("manifest.json", JSON.stringify(manifest, null, "\t"));
|
||||
|
||||
// update versions.json with target version and minAppVersion from manifest.json
|
||||
let versions = JSON.parse(readFileSync("versions.json", "utf8"));
|
||||
versions[targetVersion] = minAppVersion;
|
||||
writeFileSync("versions.json", JSON.stringify(versions, null, "\t"));
|
3
versions.json
Normal file
3
versions.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"0.0.0": "0.15.0"
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user