Skip to content

Commit

Permalink
Merge pull request #67
Browse files Browse the repository at this point in the history
Version 0.15.2
  • Loading branch information
twibiral authored Sep 21, 2022
2 parents 1210077 + 436bb5e commit b7a031e
Show file tree
Hide file tree
Showing 19 changed files with 310 additions and 130 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [0.15.2] - 2022-09-21

### Changed

- Changed project structure: Added node config and build files to root dir.
- Added documentation for every function.
- Fixed shell support.

## [0.15.1] - 2022-09-20

### Changed
Expand Down
4 changes: 2 additions & 2 deletions src/esbuild.config.mjs → esbuild.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ esbuild.build({
banner: {
js: banner,
},
entryPoints: ['main.ts'],
entryPoints: ['src/main.ts'],
bundle: true,
external: [
'obsidian',
Expand Down Expand Up @@ -48,5 +48,5 @@ esbuild.build({
logLevel: "info",
sourcemap: prod ? false : 'inline',
treeShaking: true,
outfile: 'main.js',
outfile: 'src/main.js',
}).catch(() => process.exit(1));
20 changes: 10 additions & 10 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"id": "execute-code",
"name": "Execute Code",
"version": "0.15.0",
"minAppVersion": "0.12.0",
"description": "Allows to execute code snippets within a note.",
"author": "twibiral",
"authorUrl": "https://www.github.com/twibiral",
"isDesktopOnly": true
}
{
"id": "execute-code",
"name": "Execute Code",
"version": "0.15.2",
"minAppVersion": "0.12.0",
"description": "Allows to execute code snippets within a note.",
"author": "twibiral",
"authorUrl": "https://www.github.com/twibiral",
"isDesktopOnly": true
}
8 changes: 4 additions & 4 deletions src/package-lock.json → package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/package.json → package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "execute-code",
"version": "0.15.1",
"version": "0.15.2",
"description": "This is a sample plugin for Obsidian (https://obsidian.md)",
"main": "main.js",
"main": "src/main.js",
"scripts": {
"dev": "node esbuild.config.mjs",
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production",
Expand Down
92 changes: 82 additions & 10 deletions src/Magic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,37 @@ const NOTE_TITLE_REGEX = /@title/g;
const PYTHON_PLOT_REGEX = /^(plt|matplotlib.pyplot|pyplot)\.show\(\)/gm;
const R_PLOT_REGEX = /^plot\(.*\)/gm;

/**
* Parses the source code for the @vault command and replaces it with the vault path.
*
* @param source The source code to parse.
* @param vaultPath The path of the vault.
* @returns The transformed source code.
*/
export function insertVaultPath(source: string, vaultPath: string): string {
return source.replace(VAULT_REGEX, `"app://local/${vaultPath.replace(/\\/g, "/")}"`);
}


/**
* Parses the source code for the @note command and replaces it with the note path.
*
* @param source The source code to parse.
* @param notePath The path of the vault.
* @returns The transformed source code.
*/
export function insertNotePath(source: string, notePath: string): string {
return source.replace(CURRENT_NOTE_REGEX, `"app://local/${notePath.replace(/\\/g, "/")}"`);
}


/**
* Parses the source code for the @vault command and replaces it with the vault path.
*
* @param source The source code to parse.
* @param noteTitle The path of the vault.
* @returns The transformed source code.
*/
export function insertNoteTitle(source: string, noteTitle: string): string {
let t = "";
if (noteTitle.contains("."))
Expand All @@ -38,11 +61,51 @@ export function insertNoteTitle(source: string, noteTitle: string): string {
return source.replace(NOTE_TITLE_REGEX, `"${t}"`);
}


/**
* Add the @show command to python. @show is only supported in python and javascript.
*
* @param source The source code to parse.
* @returns The transformed source code.
*/
export function addMagicToPython(source: string): string {
source = pythonParseShowImage(source);
return source;
}


/**
* Add the @show command to javascript. @show is only supported in python and javascript.
*
* @param source The source code to parse.
* @returns The transformed source code.
*/
export function addMagicToJS(source: string): string {
source = jsParseShowImage(source);
return source;
}


/**
* Parses some python code and changes it to display plots in the note instead of opening a new window.
* Only supports normal plots generated with the `plt.show(...)` function.
*
* @param source The source code to parse.
* @returns The transformed source code.
*/
export function addInlinePlotsToPython(source: string): string {
const showPlot = `import io; import sys; __obsidian_execute_code_temp_pyplot_var__=io.BytesIO(); plt.plot(); plt.savefig(__obsidian_execute_code_temp_pyplot_var__, format='svg'); plt.close(); sys.stdout.buffer.write(__obsidian_execute_code_temp_pyplot_var__.getvalue())`;
return source.replace(PYTHON_PLOT_REGEX, showPlot);
}


/**
* Parses some R code and changes it to display plots in the note instead of opening a new window.
* Only supports normal plots generated with the `plot(...)` function.
*
* @param source The source code to parse.
* @returns The transformed source code.
*/
export function addInlinePlotsToR(source: string): string {
const matches = source.matchAll(R_PLOT_REGEX);
for (const match of matches) {
Expand All @@ -56,17 +119,11 @@ export function addInlinePlotsToR(source: string): string {
return source;
}

export function addMagicToPython(source: string): string {
source = pythonParseShowImage(source);
return source;
}

export function addMagicToJS(source: string): string {
source = jsParseShowImage(source);
return source;
}


/**
* Parses the PYTHON code for the @show command and replaces it with the image.
* @param source The source code to parse.
*/
function pythonParseShowImage(source: string): string {
const matches = source.matchAll(SHOW_REGEX);
for (const match of matches) {
Expand All @@ -82,6 +139,11 @@ function pythonParseShowImage(source: string): string {
return source;
}


/**
* Parses the JAVASCRIPT code for the @show command and replaces it with the image.
* @param source The source code to parse.
*/
function jsParseShowImage(source: string): string {
const matches = source.matchAll(SHOW_REGEX);
for (const match of matches) {
Expand All @@ -98,6 +160,16 @@ function jsParseShowImage(source: string): string {
return source;
}


/**
* Builds the image string that is used to display the image in the note based on the configurations for
* height, width and alignment.
*
* @param imagePath The path to the image.
* @param width The image width.
* @param height The image height.
* @param alignment The image alignment.
*/
function buildMagicShowImage(imagePath: string, width: string = "0", height: string = "0", alignment: string = "center"): string {
if (imagePath.contains("+")) {
let splittedPath = imagePath.replace(/['"]/g, "").split("+");
Expand Down
9 changes: 9 additions & 0 deletions src/Settings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import {supportedLanguages} from "./main";


export type ExecutorSettingsLanguages = Exclude<typeof supportedLanguages[number], "javascript">;


/**
* Interface that contains all the settings for the extension.
*/
export interface ExecutorSettings {
timeout: number;
nodePath: string;
Expand Down Expand Up @@ -52,6 +57,10 @@ export interface ExecutorSettings {
kotlinInject: string;
}


/**
* The default settings for the extensions as implementation of the ExecutorSettings interface.
*/
export const DEFAULT_SETTINGS: ExecutorSettings = {
timeout: 10000,
nodePath: "node",
Expand Down
10 changes: 10 additions & 0 deletions src/SettingsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import {App, PluginSettingTab, Setting} from "obsidian";
import ExecuteCodePlugin from "./main";
import {ExecutorSettings, ExecutorSettingsLanguages} from "./Settings";


/**
* This class is responsible for creating a settings tab in the settings menu. The settings tab is showed in the
* regular obsidian settings menu.
*
* The {@link display} functions build the html page that is showed in the settings.
*/
export class SettingsTab extends PluginSettingTab {
plugin: ExecuteCodePlugin;

Expand All @@ -10,6 +17,9 @@ export class SettingsTab extends PluginSettingTab {
this.plugin = plugin;
}

/**
* Builds the html page that is showed in the settings.
*/
display() {
const {containerEl} = this;
containerEl.empty();
Expand Down
Loading

0 comments on commit b7a031e

Please sign in to comment.