/* THIS IS A GENERATED/BUNDLED FILE BY ESBUILD if you want to view the source, please visit the github repository of this plugin */ var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // main.ts var main_exports = {}; __export(main_exports, { default: () => ObsidianAsymptote }); module.exports = __toCommonJS(main_exports); var import_obsidian = require("obsidian"); var fs = __toESM(require("fs")); var path = __toESM(require("path")); var import_child_process = require("child_process"); var import_os = require("os"); var TEMP_DIR = path.join((0, import_os.tmpdir)(), "obsidian-asy"); var DEFAULT_SETTINGS = { library: "", timeout: 5e3 }; var ObsidianAsymptote = class extends import_obsidian.Plugin { async onload() { this.settings = DEFAULT_SETTINGS; this.registerMarkdownCodeBlockProcessor("asy", (source, el, ctx) => { this.renderAsyToContainer(source, el); }); this.registerMarkdownCodeBlockProcessor("asymptote", (source, el, ctx) => { this.renderAsyToContainer(source, el); }); this.addSettingTab(new ObsidianAsymptoteSettings(this.app, this)); } renderAsyToContainer(source, container) { return new Promise((resolve, reject) => { this.renderAsyToSVG(source).then((data) => { container.innerHTML = data; const svg = container.getElementsByTagName("svg"); 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) { return new Promise((resolve, reject) => { const dirPath = TEMP_DIR; this.copyLibraryFiles(); fs.mkdir(dirPath, (err) => { if (err === null || (err == null ? void 0 : err.code) === "EEXIST") { const fileId = Math.floor(Math.random() * 1e7); const inFileName = `input-${fileId}.asy`; const outFileName = `output-${fileId}.svg`; const inputPath = path.join(dirPath, inFileName); fs.writeFile(inputPath, source, (err2) => { if (err2) reject(err2); const command = 'asy "{input}" -o "{output}" -f svg'.replace("{input}", inFileName).replace("{output}", outFileName); (0, import_child_process.exec)(command, { cwd: dirPath, timeout: this.settings.timeout }, (err3) => { if (err3) reject(err3); fs.readFile(path.join(dirPath, outFileName), function(err4, data) { if (err4) reject(err4); resolve(data.toString()); }); }); }); } }); }); } copyLibraryFiles() { const folder = this.app.vault.getAbstractFileByPath(this.settings.library); if (folder !== null && folder instanceof import_obsidian.TFolder) { import_obsidian.Vault.recurseChildren(folder, async (child) => { if (child instanceof import_obsidian.TFile) { const file = child; 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); } }; var ObsidianAsymptoteSettings = class extends import_obsidian.PluginSettingTab { constructor(app, plugin) { super(app, plugin); this.plugin = plugin; } display() { const { containerEl } = this; containerEl.empty(); new import_obsidian.Setting(containerEl).setName("Library").setDesc("Folder of Asymptote library files (relative to vault root)").addText((text) => { text.setPlaceholder("Enter Path").setValue(this.plugin.settings.library).onChange(async (value) => { this.plugin.settings.library = value; await this.plugin.saveSettings(); }); }); new import_obsidian.Setting(containerEl).setName("Clean Temporary Directory").addButton((button) => { button.setButtonText("Clear").onClick((_) => { fs.rm(TEMP_DIR, { recursive: true, force: true }, (err) => { if (err === null) { new import_obsidian.Notice("Directory Cleared"); } else { new import_obsidian.Notice("Error Clearing Directory"); } }); }); }); new import_obsidian.Setting(containerEl).setName("Timeout (ms)").addSlider((slider) => { slider.setLimits(0, 3e4, 100).setValue(this.plugin.settings.timeout).onChange(async (value) => { this.plugin.settings.timeout = value; await this.plugin.saveSettings(); }); }); } };