Text2Dia

name

Text2Dia

description

Convert text / code to diagrams and diagrams back to text. Supports Math (tex), Mermaid, PlantUML etc.

icon

sync_alt

instructions

This plugin allows users to easily convert text to diagrams and diagrams back to text. It supports various diagram types including Math (tex), Mermaid, PlantUML, Graphviz, etc.

To convert text to diagrams, select the "Convert to Dia" menu after selecting some text:



To convert diagram back to text, use the "Convert to Text" menu:



The plugin also has tight integration for math. You can also create diagram using an {expression}.


linkText2Dia (v1.0.1)


linkCode

/***
* Source Code: https://github.com/debanjandhar12/my-amplenote-plugins-v2
* Author: debajandhar12
* Target Folder: src-text2dia
***/
(()=>{var e={replaceText:{"Convert to Dia":async function(e,t){t=t.trim();var{type:a,text:r}=await this._parseUserIntentFromText(e,t);if("cancel"!=a)return await this._toDiagram(e,a,t,r),null}},insertText:{"Create Diagram":async function(e){let{type:t,text:a}=await this._parseUserIntentFromText(e,null);if("cancel"!=t)return a=a.trim(),await this._toDiagram(e,t,a,a),null}},imageOption:{"Convert to Text":async function(e,t){return await this._toText(e,t),null}},async _parseUserIntentFromText(e,t){if("string"==typeof t&&t.match(/^\$\$(.*?)\$\$/s))return{type:"tex-display",text:t.replace(/^\$\$(.*?)\$\$/s,"$1")};if("string"==typeof t&&t.match(/^\$(.*?)\$/s))return{type:"tex",text:t.replace(/^\$(.*?)\$/s,"$1")};var a=[{label:"Diagram Type",type:"select",options:[{label:"Tex (Inline)",value:"tex"},{label:"Tex (Display)",value:"tex-display"},{label:"Mermaid",value:"mermaid"},{label:"PlantUML",value:"plantuml"},{label:"Graphviz",value:"graphviz"},{label:"Ditaa",value:"ditaa"},{label:"DBML",value:"dbml"}],value:"ditaa"}];let r;if(null==t){var n=await e.prompt("Options for creating image:",{inputs:[{label:"Text / Code",placeholder:"+--+\n|A |\n+--+",type:"text"},...a]});if(!n)return{type:"cancel"};if(!(t=n[0])||""==t)return{type:"cancel"};r=n[1]}else if(!(r=await e.prompt("Options for creating image:",{inputs:a})))return{type:"cancel"};return{type:r,text:t}},async _toDiagram(t,a,r,n){try{console.log(r);let e;if(null!=a){if("tex"===a||"tex-display"===a?e=await fetch("https://kroki.io/tikz/svg",{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:`\\documentclass{article}
\\usepackage{amsmath}
\\usepackage[dvipsnames]{xcolor}
\\usepackage[active,tightpage]{preview}
\\PreviewEnvironment{equation*}
\\setlength\\PreviewBorder{0.125pt}
 
\\begin{document}
\\${"tex-display"===a?"huge":"large"}
\\begin{equation*}
{${"tex-display"===a?"\\displaystyle":""} \\color{Emerald} ${n}}
\\end{equation*}
\\end{document}`}):"mermaid"!==a&&"plantuml"!==a&&"graphviz"!==a&&"ditaa"!==a&&"dbml"!==a||(e=await fetch(`https://kroki.io/${a}/svg`,{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:n})),!e)throw new Error("No response from kroki.io");var i,o,l,s,c,p;if(e.ok)return o=await e.blob(),l=await this._dataURLFromBlob(o),s=await this._svgToPng(l),c={uuid:t.context.noteUUID},p=await t.attachNoteMedia(c,s)+"?text="+window.encodeURIComponent(window.btoa(r)),t.context.replaceSelection(`![${r||""}](${p})<a> </a>`),null;throw i=await e.text(),new Error(i)}}catch(e){t.alert("Failed _toDiagram - "+e)}},async _toText(t,e){try{var a=new URL(e.src);if(null==a.searchParams.get("text"))throw new Error("No text information found in image. It is possible that the image was not created by this plugin.");var r=window.atob(window.decodeURIComponent(a.searchParams.get("text")));console.log(r),t.context.replaceSelection(r)}catch(e){t.alert("Failed _toText - "+e)}},async _svgToPng(r){return new Promise(async function(t,e){let a=new Image;a.src=r,a.onload=function(){var e=document.createElement("canvas");e.width=a.width,e.height=a.height;e.getContext("2d").drawImage(a,0,0);e=e.toDataURL("image/png");t(e)}})},async _dataURLFromBlob(e){return new Promise((t,a)=>{let r=new FileReader;r.onload=e=>{t(e.target.result)},r.onerror=function(e){r.abort(),a(e.target.error)},r.readAsDataURL(e)})}};return e})()


linkChangelog

13/07/2024 - Temporarily fixed issue with replaceSelection



Note: The images are generated by the free and open source service kroki.io.