├── .editorconfig ├── .eslintignore ├── .eslintrc ├── .gitignore ├── .npmrc ├── LICENSE ├── README.md ├── docs └── demo.png ├── esbuild.config.mjs ├── manifest.json ├── package-lock.json ├── package.json ├── src ├── Outputter.ts ├── Squiggle.tsx ├── main.ts └── prism.ts ├── styles.css ├── tsconfig.json ├── version-bump.mjs └── versions.json /.editorconfig: -------------------------------------------------------------------------------- 1 | # top-most EditorConfig file 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | insert_final_newline = true 7 | indent_style = tab 8 | indent_size = 4 9 | tab_width = 4 10 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | npm node_modules 2 | build -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "root": true, 3 | "parser": "@typescript-eslint/parser", 4 | "env": { "node": true }, 5 | "plugins": [ 6 | "@typescript-eslint" 7 | ], 8 | "extends": [ 9 | "eslint:recommended", 10 | "plugin:@typescript-eslint/eslint-recommended", 11 | "plugin:@typescript-eslint/recommended" 12 | ], 13 | "parserOptions": { 14 | "sourceType": "module" 15 | }, 16 | "rules": { 17 | "no-unused-vars": "off", 18 | "@typescript-eslint/no-unused-vars": ["error", { "args": "none" }], 19 | "@typescript-eslint/ban-ts-comment": "off", 20 | "no-prototype-builtins": "off", 21 | "@typescript-eslint/no-empty-function": "off" 22 | } 23 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # vscode 2 | .vscode 3 | 4 | # Intellij 5 | *.iml 6 | .idea 7 | 8 | # npm 9 | node_modules 10 | 11 | # Don't include the compiled main.js file in the repo. 12 | # They should be uploaded to GitHub releases instead. 13 | main.js 14 | 15 | # Exclude sourcemaps 16 | *.map 17 | 18 | # obsidian 19 | data.json 20 | 21 | # Exclude macOS Finder (System Explorer) View States 22 | .DS_Store 23 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | tag-version-prefix="" -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Jesse Hoogland 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Obsidian Squiggle Plugin 2 | 3 | Note: This was forked from the [obsidian-execute-code plugin](https://github.com/twibiral/obsidian-execute-code/blob/master/execute_code_example.gif?raw=true). 4 | 5 | This plugin allows you to execute squiggle code snippets in code blocks in your notes. The plugin adds a 'run' button for code blocks in supported languages. Clicking them results in the code of the block being executed. After the execution the result of the execution is showed. 6 | 7 | It also adds syntax highlighting with prism. 8 | 9 | The result is shown only after the execution is finished. It is not possible to enter text on the command line into the executed programm now. 10 | 11 | ![Demo](docs/demo.png) 12 | 13 | 14 | ## Running in Preview 15 | 16 | Adding `run-` before the language name in the code blocks (as in the example below) renders the code block in the 17 | preview already. 18 | This allows you to execute the code in the preview. 19 | 20 | ## Installation 21 | 22 | In your vault go to Settings > Community plugins > Browse and search for "Squiggle". Select the plugin, install it 23 | and activate it. 24 | 25 | or 26 | 27 | Follow [this link](https://obsidian.md/plugins?search=squiggle#) and click "Open in Obsidian". 28 | 29 | ## Warning 30 | Do not execute code from sources you don't know or code you don't understand. Executing code can cause irrepairable damage. 31 | 32 | ## Known Problems 33 | - Missing when `run` button after switching the theme: Try to close and reopen your notes and wait for a few minutes. It seems like obsidian doesn't call the postprocessors after the theme switch. 34 | 35 | ## Future Work 36 | - Merge back into the `execute-code-plugin`? This probably isn't worth it as Squiggle is run inside of JS & rendered with the help of React. This is very different to languages that actually run on your computer. 37 | 38 | ## Contribution 39 | All contributions are welcome. Just create a merge request or email me: jesse(at)jessehoogland.com 40 | 41 | -------------------------------------------------------------------------------- /docs/demo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jqhoogland/obsidian-squiggle/8eec74a26858fa41d11c3a892de54b9ce89ef075/docs/demo.png -------------------------------------------------------------------------------- /esbuild.config.mjs: -------------------------------------------------------------------------------- 1 | import esbuild from "esbuild"; 2 | import process from "process"; 3 | import builtins from 'builtin-modules' 4 | 5 | const banner = 6 | `/* 7 | THIS IS A GENERATED/BUNDLED FILE BY ESBUILD 8 | if you want to view the source, please visit the github repository of this plugin 9 | */ 10 | `; 11 | 12 | const prod = (process.argv[2] === 'production'); 13 | 14 | esbuild.build({ 15 | banner: { 16 | js: banner, 17 | }, 18 | entryPoints: ['./src/main.ts'], 19 | bundle: true, 20 | external: [ 21 | 'obsidian', 22 | 'electron', 23 | '@codemirror/autocomplete', 24 | '@codemirror/closebrackets', 25 | '@codemirror/collab', 26 | '@codemirror/commands', 27 | '@codemirror/comment', 28 | '@codemirror/fold', 29 | '@codemirror/gutter', 30 | '@codemirror/highlight', 31 | '@codemirror/history', 32 | '@codemirror/language', 33 | '@codemirror/lint', 34 | '@codemirror/matchbrackets', 35 | '@codemirror/panel', 36 | '@codemirror/rangeset', 37 | '@codemirror/rectangular-selection', 38 | '@codemirror/search', 39 | '@codemirror/state', 40 | '@codemirror/stream-parser', 41 | '@codemirror/text', 42 | '@codemirror/tooltip', 43 | '@codemirror/view', 44 | ...builtins], 45 | format: 'cjs', 46 | watch: !prod, 47 | target: 'es2016', 48 | logLevel: "info", 49 | sourcemap: prod ? false : 'inline', 50 | treeShaking: true, 51 | outfile: 'main.js', 52 | }).catch(() => process.exit(1)); 53 | -------------------------------------------------------------------------------- /manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": "squiggle", 3 | "name": "Squiggle", 4 | "version": "0.1.2", 5 | "minAppVersion": "0.12.0", 6 | "description": "Enables running squiggle code snippets within a note.", 7 | "isDesktopOnly": true, 8 | "author": "Jesse Hoogland" 9 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "obsidian-squiggle", 3 | "version": "0.1.2", 4 | "description": "This is a plugin that lets you run squiggle from code blocks in Obsidian", 5 | "main": "main.js", 6 | "scripts": { 7 | "dev": "node esbuild.config.mjs", 8 | "build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production", 9 | "version": "node version-bump.mjs && git add manifest.json versions.json" 10 | }, 11 | "keywords": [], 12 | "author": "Jesse Hoogland", 13 | "license": "MIT", 14 | "devDependencies": { 15 | "@types/node": "^16.11.6", 16 | "@types/prismjs": "^1.26.0", 17 | "@types/react-dom": "^18.0.6", 18 | "@typescript-eslint/eslint-plugin": "^5.2.0", 19 | "@typescript-eslint/parser": "^5.2.0", 20 | "builtin-modules": "^3.2.0", 21 | "esbuild": "0.13.12", 22 | "obsidian": "latest", 23 | "tslib": "2.3.1", 24 | "typescript": "^4.4.4" 25 | }, 26 | "dependencies": { 27 | "@quri/squiggle-components": "^0.3.1", 28 | "@quri/squiggle-lang": "^0.3.0", 29 | "g": "^2.0.1", 30 | "JSCPP": "^2.0.9", 31 | "moment": "^2.29.3", 32 | "original-fs": "^1.1.0", 33 | "prism": "^4.1.2", 34 | "react": "^18.2.0", 35 | "react-dom": "^18.2.0", 36 | "tau-prolog": "^0.3.4" 37 | } 38 | } -------------------------------------------------------------------------------- /src/Outputter.ts: -------------------------------------------------------------------------------- 1 | 2 | export class Outputter { 3 | codeBlockElement: HTMLElement; 4 | outputElement: HTMLElement; 5 | clearButton: HTMLButtonElement; 6 | stdoutElem: HTMLSpanElement; 7 | stderrElem: HTMLSpanElement; 8 | stdoutText: string; 9 | stderrText: string; 10 | 11 | constructor(codeBlock: HTMLElement) { 12 | this.codeBlockElement = codeBlock; 13 | this.stdoutText = ""; 14 | this.stderrText = ""; 15 | } 16 | 17 | clear() { 18 | if (this.outputElement) { 19 | this.stdoutElem.setText(""); 20 | this.stderrElem.setText(""); 21 | } 22 | this.stdoutText = ""; 23 | this.stderrText = ""; 24 | } 25 | 26 | delete() { 27 | if (this.outputElement) 28 | this.outputElement.style.display = "none"; 29 | 30 | if (this.clearButton) 31 | this.clearButton.style.display = "none"; 32 | 33 | this.clear() 34 | } 35 | 36 | write(text: string) { 37 | if (!this.outputElement) { 38 | this.addOutputElement(); 39 | } 40 | 41 | if (!this.clearButton) { 42 | this.addClearButton(); 43 | } 44 | 45 | this.stdoutText += text; 46 | if (!this.stderrText && !this.stdoutText) return; 47 | 48 | // make visible again: 49 | this.outputElement.style.display = "block"; 50 | this.clearButton.style.display = "block"; 51 | } 52 | 53 | writeErr(text: string) { 54 | if (!this.outputElement) { 55 | this.addOutputElement(); 56 | } 57 | 58 | if (!this.clearButton) { 59 | this.addClearButton(); 60 | } 61 | 62 | this.stderrText += text; 63 | if (!this.stderrText && !this.stdoutText) return; 64 | 65 | this.stderrElem.setText(this.stderrText); 66 | 67 | // make visible again: 68 | this.outputElement.style.display = "block"; 69 | this.clearButton.style.display = "block"; 70 | } 71 | 72 | private getParentElement() { 73 | return this.codeBlockElement.parentElement as HTMLDivElement; 74 | } 75 | 76 | private addClearButton() { 77 | const parentEl = this.getParentElement(); 78 | 79 | this.clearButton = document.createElement("button"); 80 | this.clearButton.className = "clear-button"; 81 | this.clearButton.setText("Clear"); 82 | this.clearButton.addEventListener("click", () => this.delete()); 83 | 84 | parentEl.appendChild(this.clearButton); 85 | } 86 | 87 | addOutputElement() { 88 | const parentEl = this.getParentElement(); 89 | 90 | const hr = document.createElement("hr"); 91 | 92 | this.outputElement = document.createElement("code"); 93 | this.outputElement.classList.add("language-output"); 94 | 95 | this.stdoutElem = document.createElement("span"); 96 | this.stdoutElem.addClass("stdout"); 97 | 98 | this.stderrElem = document.createElement("span"); 99 | this.stderrElem.addClass("stderr"); 100 | 101 | this.outputElement.appendChild(hr); 102 | this.outputElement.appendChild(this.stdoutElem); 103 | this.outputElement.appendChild(this.stderrElem); 104 | parentEl.appendChild(this.outputElement); 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/Squiggle.tsx: -------------------------------------------------------------------------------- 1 | import * as React from "react"; 2 | import * as ReactDOM from "react-dom/client"; 3 | import { SquiggleChart } from "@quri/squiggle-components"; 4 | 5 | export const renderSquiggle = (code: string, element: HTMLElement) => ReactDOM.createRoot(element).render() -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { loadPrism, MarkdownRenderer, Plugin } from 'obsidian'; 2 | 3 | import * as Prism from 'prismjs'; 4 | import { Outputter } from "./Outputter"; 5 | import { squigglePrism } from './prism'; 6 | import { renderSquiggle } from "./squiggle"; 7 | 8 | const supportedLanguages = ["squiggle"]; 9 | 10 | const buttonText = "Run"; 11 | 12 | const runButtonClass = "run-code-button"; 13 | const runButtonDisabledClass = "run-button-disabled"; 14 | const hasButtonClass = "has-run-code-button"; 15 | 16 | 17 | export default class SquigglePlugin extends Plugin { 18 | async onload() { 19 | await loadPrism().then((prism: typeof Prism) => prism.languages.squiggle = squigglePrism(prism)) 20 | 21 | this.addRunButtons(document.body); 22 | this.registerMarkdownPostProcessor((element, _context) => { 23 | this.addRunButtons(element); 24 | 25 | }); 26 | 27 | // live preview renderers 28 | supportedLanguages.forEach(l => { 29 | console.log(`registering renderer for ${l}`) 30 | this.registerMarkdownCodeBlockProcessor(`run-${l}`, async (src, el, _ctx) => { 31 | await MarkdownRenderer.renderMarkdown('```' + l + '\n' + src + '\n```', el, '', null) 32 | }) 33 | }) 34 | } 35 | 36 | onunload() { 37 | document 38 | .querySelectorAll("pre > code") 39 | .forEach((codeBlock: HTMLElement) => { 40 | const pre = codeBlock.parentElement as HTMLPreElement; 41 | const parent = pre.parentElement as HTMLDivElement; 42 | 43 | if (parent.hasClass(hasButtonClass)) { 44 | parent.removeClass(hasButtonClass); 45 | } 46 | }); 47 | 48 | document 49 | .querySelectorAll("." + runButtonClass) 50 | .forEach((button: HTMLButtonElement) => button.remove()); 51 | 52 | document 53 | .querySelectorAll("." + runButtonDisabledClass) 54 | .forEach((button: HTMLButtonElement) => button.remove()); 55 | 56 | document 57 | .querySelectorAll(".clear-button") 58 | .forEach((button: HTMLButtonElement) => button.remove()); 59 | 60 | document 61 | .querySelectorAll(".language-output") 62 | .forEach((out: HTMLElement) => out.remove()); 63 | 64 | console.log("Unloaded plugin: Execute Code"); 65 | } 66 | 67 | private addRunButtons(element: HTMLElement) { 68 | element.querySelectorAll("code") 69 | .forEach((codeBlock: HTMLElement) => { 70 | console.log({codeBlock}) 71 | const pre = codeBlock.parentElement as HTMLPreElement; 72 | const parent = pre.parentElement as HTMLDivElement; 73 | const language = codeBlock.className.toLowerCase(); 74 | 75 | const srcCode = codeBlock.getText(); // get source code and perform magic to insert title etc 76 | 77 | if (supportedLanguages.some((lang) => language.contains(`language-${lang}`)) 78 | && !parent.classList.contains(hasButtonClass)) { // unsupported language 79 | 80 | parent.classList.add(hasButtonClass); 81 | const button = this.createRunButton(); 82 | pre.appendChild(button); 83 | 84 | const out = new Outputter(codeBlock); 85 | 86 | // Add button: 87 | if (language.contains("language-squiggle")) { 88 | button.addEventListener("click", async () => { 89 | this.runCode(srcCode, out, button); 90 | }); 91 | } 92 | } 93 | }) 94 | } 95 | 96 | 97 | private createRunButton() { 98 | console.log("Add run button"); 99 | const button = document.createElement("button"); 100 | button.classList.add(runButtonClass); 101 | button.setText(buttonText); 102 | return button; 103 | } 104 | 105 | private runCode(codeBlockContent: string, outputter: Outputter, button: HTMLButtonElement) { 106 | outputter.clear(); 107 | outputter.addOutputElement(); 108 | renderSquiggle(codeBlockContent, outputter.outputElement); 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /src/prism.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Prism declarations for .squiggle based on the squiggle language 3 | * [textmate grammar](https://github.com/quantified-uncertainty/squiggle/blob/develop/packages/vscode-ext/syntaxes/squiggle.tmLanguage.yaml). 4 | * (Couldn't find any codegen solutions.) 5 | */ 6 | 7 | import * as Prism from "prismjs"; 8 | 9 | 10 | 11 | /** 12 | * Left to implement (maybe): 13 | * * let 14 | * * defun 15 | * * array-parmeters 16 | * * function-call 17 | * * block 18 | **/ 19 | export const squigglePrism = (prism: typeof Prism) => ({ 20 | comment: [ 21 | { 22 | pattern: /\/\/.*/, 23 | greedy: true 24 | }, 25 | { 26 | pattern: /#.*/, 27 | greedy: true 28 | }, 29 | { 30 | pattern: /\/\*[\s\S]*\*\//, 31 | } 32 | ], 33 | keyword: [ 34 | { 35 | pattern: /\bif|then|else|to\b/, 36 | lookbehind: true 37 | }, 38 | ], 39 | number: [ 40 | { 41 | // Not perfect: doesn't match the `.` in `(.2` 42 | pattern: /\b(\d+\.\d*|\.?\d+)([eE]-?\d+)?([_a-zA-Z]+[_a-zA-Z0-9]*)?/, 43 | }, 44 | { 45 | pattern: /\b\d+(?:[_a-zA-Z]+[_a-zA-Z0-9]*)?/ 46 | } 47 | ], 48 | // let: { 49 | // pattern: /\b\s*(\w+)\s*=/, 50 | // }, 51 | // Allow for all non-ASCII characters (See http://stackoverflow.com/a/2008444) 52 | function: /^\s*(\w+)\s*(\().+(\))\s*=/, 53 | // operator: /--|\+\+|\*\*=?|=>|&&=?|\|\|=?|[!=]==|<<=?|>>>?=?|[-+*/%&|^!=<>]=?|\.{3}|\?\?=?|\?\.?|[~:]|.*|.>|\|>/, 54 | // punctuation: /[{}[\],]/, 55 | }); 56 | 57 | -------------------------------------------------------------------------------- /styles.css: -------------------------------------------------------------------------------- 1 | button.run-code-button { 2 | display: none; 3 | color: var(--text-muted); 4 | position: absolute; 5 | bottom: 0; 6 | right: 0; 7 | margin: 5px; 8 | padding: 5px 20px 5px 20px; 9 | } 10 | 11 | button.clear-button { 12 | display: none; 13 | color: var(--text-muted); 14 | position: absolute; 15 | bottom: 0; 16 | left: 0; 17 | margin: 5px; 18 | padding: 5px 20px 5px 20px; 19 | } 20 | 21 | pre:hover .run-code-button { 22 | display: block; 23 | } 24 | 25 | pre:hover .clear-button { 26 | display: block; 27 | } 28 | 29 | .run-button-disabled { 30 | display: none; 31 | } 32 | 33 | pre:hover .run-button-disabled { 34 | display: none; 35 | } 36 | 37 | code.language-output { 38 | padding-bottom: 2.5em; 39 | } 40 | 41 | code.language-output span.stdout { 42 | color: var(--text-muted); 43 | } 44 | 45 | code.language-output span.stderr { 46 | color: red; 47 | } 48 | 49 | code.language-output hr { 50 | margin: 0 0 1em; 51 | } 52 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "baseUrl": ".", 4 | "inlineSourceMap": true, 5 | "inlineSources": true, 6 | "module": "ESNext", 7 | "target": "ES6", 8 | "allowJs": true, 9 | "noImplicitAny": true, 10 | "moduleResolution": "node", 11 | "importHelpers": true, 12 | "isolatedModules": true, 13 | "jsx": "react", 14 | "lib": [ 15 | "DOM", 16 | "ES5", 17 | "ES6", 18 | "ES7" 19 | ] 20 | }, 21 | "include": [ 22 | "**/*.ts" 23 | ], 24 | } 25 | -------------------------------------------------------------------------------- /version-bump.mjs: -------------------------------------------------------------------------------- 1 | import { readFileSync, writeFileSync } from "fs"; 2 | 3 | const targetVersion = process.env.npm_package_version; 4 | 5 | // read minAppVersion from manifest.json and bump version to target version 6 | let manifest = JSON.parse(readFileSync("manifest.json", "utf8")); 7 | const { minAppVersion } = manifest; 8 | manifest.version = targetVersion; 9 | writeFileSync("manifest.json", JSON.stringify(manifest, null, "\t")); 10 | 11 | // update versions.json with target version and minAppVersion from manifest.json 12 | let versions = JSON.parse(readFileSync("versions.json", "utf8")); 13 | versions[targetVersion] = minAppVersion; 14 | writeFileSync("versions.json", JSON.stringify(versions, null, "\t")); 15 | -------------------------------------------------------------------------------- /versions.json: -------------------------------------------------------------------------------- 1 | { 2 | "0.1,0": "0.1.0", 3 | "0.1.1": "0.1.1", 4 | "0.1.2": "0.1.2" 5 | } --------------------------------------------------------------------------------