Skip to content

Commit

Permalink
+ adding support for local rendering(references #6)
Browse files Browse the repository at this point in the history
  • Loading branch information
joethei committed Dec 12, 2021
1 parent f5c29b9 commit e9089d8
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 19 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ You can also host your own server
[JEE](https://plantuml.com/de/server) /
[PicoWeb](https://plantuml.com/de/picoweb)) and specify its address in the settings.

You can also render your diagrams locally by installing the desktop only [Local PlantUML Provider](https://github.com/joethei/obsidian-local-plantuml) plugin.

## Usage
Create a fenced codeblock using `plantuml` as the language.
Specify your plantuml code inside.
Expand Down
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"id": "obsidian-plantuml",
"name": "PlantUML",
"version": "1.2.7",
"minAppVersion": "0.11.5",
"version": "1.3.0",
"minAppVersion": "0.12.0",
"description": "Render PlantUML Diagrams",
"author": "Johannes Theiner",
"authorUrl": "https://github.com/joethei/",
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-plantuml",
"version": "1.2.7",
"version": "1.3.0",
"description": "PlantUML rendering for Obsidian.md",
"main": "main.js",
"scripts": {
Expand All @@ -9,7 +9,7 @@
"lint": "eslint . --ext .ts"
},
"keywords": [],
"author": "",
"author": "Johannes Theiner",
"license": "MIT",
"devDependencies": {
"@types/node": "^14.14.2",
Expand All @@ -20,6 +20,7 @@
"builtin-modules": "^3.2.0",
"esbuild": "^0.14.2",
"eslint": "^7.22.0",
"node": "^17.2.0",
"obsidian": "^0.12.7",
"plantuml-encoder": "^1.4.0",
"tslib": "^2.3.1",
Expand Down
68 changes: 54 additions & 14 deletions src/processors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MarkdownPostProcessorContext, request} from "obsidian";
import {MarkdownPostProcessorContext, request} from "obsidian";
import {DEFAULT_SETTINGS} from "./settings";
import * as plantuml from "plantuml-encoder";
import PlantumlPlugin from "./main";
Expand All @@ -10,6 +10,15 @@ export class Processors {
this.plugin = plugin;
}

private async localProcessor(source: string, type: string) : Promise<string> {
//@ts-ignore
if(this.plugin.app.plugins.plugins["local-plantuml"]) {
//@ts-ignore
return await this.plugin.app.plugins.plugins["local-plantuml"].generateImage(source, this.plugin.settings.localJar, type);
}
return "";
}

/**
* replace all non-breaking spaces with actual spaces
* @param text
Expand Down Expand Up @@ -38,8 +47,15 @@ export class Processors {

const imageUrlBase = url + "/svg/";
source = this.replaceNonBreakingSpaces(source);
source = this.plugin.settings.header + "\r\n" + source;

const local = await this.localProcessor(source, "svg");
if(local !== "") {
el.insertAdjacentHTML('beforeend', local);
return;
}

const encodedDiagram = plantuml.encode(this.plugin.settings.header + "\r\n" + source);
const encodedDiagram = plantuml.encode(source);

request({url: imageUrlBase + encodedDiagram, method: 'GET'}).then((value: string) => {
el.insertAdjacentHTML('beforeend', value);
Expand All @@ -58,7 +74,29 @@ export class Processors {

const imageUrlBase = url + "/png/";
source = this.replaceNonBreakingSpaces(source);
const encodedDiagram = plantuml.encode(this.plugin.settings.header + "\r\n" + source);
source = this.plugin.settings.header + "\r\n" + source;

const encodedDiagram = plantuml.encode(source);

const local = await this.localProcessor(source, "png");
if(local !== "") {
const img = document.createElement("img");
img.src = "data:image/png;base64," + local;
img.useMap = "#" + encodedDiagram;

//@ts-ignore
const map = await this.plugin.app.plugins.plugins["local-plantuml"].generateMap(source, this.plugin.settings.localJar);
console.log(map);
if(map.contains("map")) {
console.log("map");
el.innerHTML = map;
el.children[0].setAttr("name", encodedDiagram);
}

el.appendChild(img);

return;
}

const img = document.createElement("img");
img.src = imageUrlBase + encodedDiagram;
Expand All @@ -68,7 +106,7 @@ export class Processors {
const mapUrlBase = url + "/map/";
request({url: mapUrlBase + encodedDiagram, method: "GET"}).then((value: string) => {
//only add the map content if actual text is returned(e.g. PicoWeb does not support this)
if (value.contains("<map>")) {
if (value.contains("map")) {
el.innerHTML = value;
el.children[0].setAttr("name", encodedDiagram);
}
Expand All @@ -88,8 +126,19 @@ export class Processors {
}
const asciiUrlBase = url + "/txt/";
source = this.replaceNonBreakingSpaces(source);
source = this.plugin.settings.header + "\r\n" + source;

const local = await this.localProcessor(source, "txt");
if(local !== "") {
const pre = document.createElement("pre");
const code = document.createElement("code");
pre.appendChild(code);
code.setText(local);
el.appendChild(pre);
return;
}

const encodedDiagram = plantuml.encode(this.plugin.settings.header + "\r\n" + source);
const encodedDiagram = plantuml.encode(source);

const result = await request({url: asciiUrlBase + encodedDiagram});

Expand All @@ -107,13 +156,4 @@ export class Processors {
code.setText(result);
el.appendChild(pre);
};

//taken from: https://stackoverflow.com/a/1144788/5589264
private escapeRegExp(string: string) : string {
return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}

private replaceAll(str: string, find: string, replace: string) : string {
return str.replace(new RegExp(this.escapeRegExp(find), 'g'), replace);
}
}
18 changes: 18 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ export interface PlantUMLSettings {
server_url: string,
header: string;
debounce: number;
localJar: string;
}

export const DEFAULT_SETTINGS: PlantUMLSettings = {
server_url: 'https://www.plantuml.com/plantuml',
header: '',
debounce: 3,
localJar: '',
}

export class PlantUMLSettingsTab extends PluginSettingTab {
Expand All @@ -36,6 +38,22 @@ export class PlantUMLSettingsTab extends PluginSettingTab {
}
)
);

//@ts-ignore
if(this.plugin.app.plugins.plugins["local-plantuml"]) {
new Setting(containerEl)
.setName("Local JAR")
.setDesc("Path to local PlantUML Jar")
.addText(text => text.setPlaceholder(DEFAULT_SETTINGS.localJar)
.setValue(this.plugin.settings.localJar)
.onChange(async (value) => {
this.plugin.settings.localJar = value;
await this.plugin.saveSettings();
}
)
);
}

new Setting(containerEl).setName("Header")
.setDesc("Included at the head in every diagram. Useful for specifying a common theme (.puml file)")
.addTextArea(text => {
Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"1.2.4": "0.11.5",
"1.2.5": "0.11.5",
"1.2.6": "0.11.5",
"1.2.7": "0.11.5"
"1.2.7": "0.11.5",
"1.3.0": "0.12.0"
}

0 comments on commit e9089d8

Please sign in to comment.