[
    {
        "author": "LinOnetwo",
        "core-version": ">=5.1.22",
        "dependents": "$:/plugins/linonetwo/itonnote",
        "description": "ZX is a tool for writing better scripts, this plugin allows you to execute a tiddler with zx.",
        "list": "readme demo developer",
        "name": "ZX Script Executor",
        "plugin-type": "plugin",
        "stability": "STABILITY_1_EXPERIMENTAL",
        "text": "{\"tiddlers\":{\"$:/plugins/linonetwo/zx-script/DecorateCoreCodeblockWidget\":{\"title\":\"$:/plugins/linonetwo/zx-script/DecorateCoreCodeblockWidget\",\"tags\":\"$:/tags/Global/View/Body\",\"text\":\"\\\\widget $codeblock(code, language)\\n<%if markdown md js javascript ts typescript bash zsh shell sh +[match<language>] %>\\n  <$set name=\\\"output-id\\\" filter=\\\"[<code>sha256[]]\\\">\\n    <$button class=\\\"code-block-zx-script-execution-button\\\">\\n      {{$:/plugins/linonetwo/zx-script/zx-icon}}ZX\\n      <$action-sendmessage $message=\\\"tm-zx\\\" code=<<code>> language=<<language>> id=<<output-id>> />\\n    </$button>\\n    <$genesis $type=\\\"$codeblock\\\" $remappable=\\\"no\\\" code=<<code>> language=<<language>> />\\n    <!-- Output -->\\n    <$let output-title={{{ [<output-id>addprefix[$:/state/linonetwo/zx-script/output/]] }}} >\\n      <%if [<output-title>is[tiddler]] %>\\n\\n        <!-- Similar to $:/plugins/linonetwo/zx-script/OutputBar -->\\n        <$transclude $tiddler=<<output-title>> />\\n\\n        <small><$link to=<<output-title>> /></small>\\n      <%endif%>\\n    </$let>\\n  </$set>\\n<%else%>\\n  <$genesis $type=\\\"$codeblock\\\" $remappable=\\\"no\\\" code=<<code>> language=<<language>> />\\n<%endif%>\\n\\\\end\\n\"},\"$:/plugins/linonetwo/zx-script/OutputBar\":{\"title\":\"$:/plugins/linonetwo/zx-script/OutputBar\",\"tags\":\"$:/tags/ViewTemplate\",\"list-before\":\"$:/plugins/linonetwo/inverse-link-and-folder/infobar\",\"text\":\"<$let output-title={{{ [{!!title}addprefix[$:/state/linonetwo/zx-script/output/]] }}} >\\n  <%if [<output-title>is[tiddler]] %>\\n    <!-- Space between top level and inner level transclude can't be omit, otherwise generated line will stick together, and destroy the tw render -->\\n    <$transclude $tiddler=<<output-title>> />\\n\\n    <small><$link to=<<output-title>> /></small>\\n  <%endif%>\\n</$let>\"},\"$:/plugins/linonetwo/zx-script/ViewToolbarButton\":{\"title\":\"$:/plugins/linonetwo/zx-script/ViewToolbarButton\",\"tags\":\"$:/tags/ViewToolbar\",\"list-before\":\"$:/core/ui/Buttons/close\",\"caption\":\"{{$:/plugins/linonetwo/zx-script/zx-icon}} {{$:/plugins/linonetwo/zx-script/zx-button-caption}}\",\"short-caption\":\"zx script executor\",\"description\":\"Run tiddler as ZX Script\",\"text\":\"\\\\whitespace trim\\n<%if [all[current]!has[plugin-type]] %>\\n  <%if [all[current]field:type[application/javascript]] :else[all[current]search:text[```]] %>\\n    <$button>\\n      <$action-sendmessage $message=\\\"tm-zx\\\" title={{!!title}} field=\\\"text\\\"/>\\n      <%if [<tv-config-toolbar-icons>match[yes]] %>\\n        {{$:/plugins/linonetwo/zx-script/zx-icon}}\\n      <%endif%>\\n      <%if [<tv-config-toolbar-text>match[yes]] %>\\n        <span class=\\\"tc-btn-text\\\">\\n          {{$:/plugins/linonetwo/zx-script/zx-button-caption}}\\n        </span>\\n      <%endif%>\\n    </$button>\\n  <%endif%>\\n<%endif%>\\n\"},\"$:/plugins/linonetwo/zx-script/demo\":{\"title\":\"$:/plugins/linonetwo/zx-script/demo\",\"text\":\"!! ZX JS script Demo\\n\\n```js\\nconsole.log(`!! Hi!`);\\n```\\n\\nResult will form a tiddler.\\n\\n```js\\nconsole.log(`\\nCode block:\\n\\n\\\\`\\\\`\\\\`js\\nconsole.log(\\\\`!! Hi!\\\\`);\\n\\\\`\\\\`\\\\`\\n`);\\n```\\n\\n!! Dynamic WikiText Demo\\n\\n```js\\nconst ramdomID = Math.random().toString(36).slice(2);\\nconst text = `<fieldset>\\n\\t<legend>Quick Add</legend>\\n\\n\\t<$button>\\n\\t\\t<$action-sendmessage $message=\\\"tm-new-tiddler\\\" title=<<now \\\"YYYY-MM-DD\\\">> tags=\\\"${ramdomID}\\\" />\\n    Add New Tiddler with ID ${ramdomID}\\n\\t</$button>\\n\\n</fieldset>`\\n\\nconsole.log(text)\\n```\\n\\n!! Wiki Scripting Demo\\n\\n```js\\nconst exampleTitle = '$:/plugins/linonetwo/zx-script/style.css';\\n\\n// to write script execute in tiddlywiki context, start with this separator ↓\\n/** tw */\\n// You can use $tw.wiki.getTiddler to get tiddler data, and can use some variables from previous context\\nconst tiddlerData = $tw.wiki.getTiddler(exampleTitle);\\n// This script runs in the nodejs side, and you can't access some API from nodejs ↓ it will cause error\\n// $tw.notifier.display('$:/state/notification/asdf');\\n\\n// You can even add tiddler using the API! But this will be add to the nodejs side, it will take a while to wait it sync to the browser side.\\n// $tw.syncadaptor.wiki.addTiddler({ title: 'asdf', text: 'aaa bbb' });\\n// You can't add state tiddler this way, they won't get sync to the browser. (nodejs wiki limitation)\\n// $tw.syncadaptor.wiki.addTiddler({ title: '$:/state/notification/asdf', text: 'aaa bbb' });\\n\\n// You can use console to print some data\\nconsole.log(tiddlerData.fields.type);\\n// You can assign some data to a variable, it can be used in next context\\nconst tiddlerDataString = JSON.stringify(tiddlerData);\\n\\n// ↓ If you end the block with separator, you can write script execute in zx environment again\\n/** tw */\\nconsole.log(`\\\\n!! Hi!\\\\n`);\\nconsole.log(tiddlerDataString);\\n```\\n\\n!!! DB and execution\\n\\nStart from v0.8.0-prerelease11 of [TidGi](https://github.com/tiddly-gittly/TidGi-Desktop/releases), it has a [better-sqlite3](https://github.com/WiseLibs/better-sqlite3/blob/master/docs/api.md) instance at `$tw.utils.Sqlite`. Which is accessible for server side plugin (TidGi is a launcher for nodejs wiki, so can run server side plugins) or zx script.\\n\\n```js\\n// pure nodejs context\\nconsole.log(`## Hi!`);\\nlet aaa = 3;\\n\\n/** tw */\\n\\n// now in worker_thread with $tw wiki access\\n\\nlet filterExample = $tw.wiki.filterOperators.sum((callback) => {callback({}, String(aaa));callback({}, String(aaa))})\\nconsole.log(filterExample)\\n\\n// use Sqlite\\nconst db = $tw.utils.Sqlite\\nconst stmt = db.prepare('SELECT title, text FROM tiddlers');\\nconst result = stmt.pluck().all()\\nconsole.log(result)\\n```\"},\"$:/plugins/linonetwo/zx-script/developer\":{\"title\":\"$:/plugins/linonetwo/zx-script/developer\",\"text\":\"!! Developer guide\\n\\n!!! Output\\n\\nWe place output of zx to a temporary tiddler, whose name start with \\\"$:/state/linonetwo/zx-script/output/\\\"\\n\\nAnd we get the name of this tiddler in wikitext using `[{!!title}addprefix[$:/state/linonetwo/zx-script/output/]`, so it can be transcluded with `{{{ }}}`\\n\\n!!! Batch execution\\n\\nYou can use message to programmatically execute any code:\\n\\n```tid\\n<$action-sendmessage $message=\\\"tm-zx\\\" code=<<code>> language=<<language>> id=<<output-id>> />\\n```\\n\\nThe id should be an unique identifier, for example:\\n\\n```tid\\n<$set name=\\\"output-id\\\" filter=\\\"[<code>sha256[]]\\\">\\n```\\n\\nRead [[$:/plugins/linonetwo/zx-script/DecorateCoreCodeblockWidget]] for how I use it.\\n\"},\"$:/plugins/linonetwo/zx-script/readme\":{\"title\":\"$:/plugins/linonetwo/zx-script/readme\",\"text\":\"!! Background\\n\\n[[google/zx|https://github.com/google/zx]] is a tool for writing better scripts, with Javascript.\\n\\nThis tiddlywiki plugin enables you to run any tiddler with zx.\\n\\n!! Prerequisit\\n\\nThis plugin requires [[TidGi Desktop App|https://github.com/tiddly-gittly/TidGi-Desktop]] environment to function properly. It will send tiddler content to the `zx` inside TidGi, and prints the output.\\n\\n!! Basic Usage\\n\\n!!! Button\\n\\nA ViewToolbar button is added. You can click on it to execute the script inside your tiddler.\\n\\nAnd the zx output will be printed below your tiddler content.\\n\\nTiddler with title extension `*.js *.md *.mjs` can be execute without problem. Other tiddler witout extension (no `.xxx`) will be executed as `*.md` file, for example, tid file without extions will be executed as `*.md`, but it works without problem too, even you are writing wiki text instead of markdown.\\n\\nIf you add filetype `application/javascript` to your tiddler, it will be recognized as `*.mjs`.\\n\\n!!! Codeblock\\n\\nAn execution button is added to the codeblock widget, if language is supported.\\n\\n```js\\nconsole.log(`!! Hi!`);\\n```\\n\\nSee [[$:/plugins/linonetwo/zx-script/demo]] for demo of full tiddler execution.\\n\\n!! Wiki Scripting\\n\\n!!! Execute the code block on the $tw context\\n\\nYou can write some code that is surrounded with `/** tw */` separator. They will be recognized as js code and executed on the context that have access to the $tw api.\\n\\nSee [[$:/plugins/linonetwo/zx-script/demo]] for examples.\\n\\nGlobal variables you can use:\\n\\n* `$tw` try it in your [[developer tool|https://developer.mozilla.org/en-US/docs/Tools]] to play around!\\n* `_` [[lodash|https://lodash.com/docs/]]\\n\\nThis means you can fetch some data on the zx script, and write the results to the wiki, creating new tiddlers.\\n\\n!!! Access filter and macros\\n\\nFilter function's call example is\\n\\n```js\\n/** tw */\\nconst result = $tw.wiki.filterOperators.sum((callback) => {callback({}, '2');callback({}, '2')})\\nconsole.log(result)\\n```\\n\\n!! Developer Documentation\\n\\nSee [[$:/plugins/linonetwo/zx-script/developer]]\\n\"},\"$:/plugins/linonetwo/zx-script/style.css\":{\"title\":\"$:/plugins/linonetwo/zx-script/style.css\",\"text\":\".code-block-zx-script-execution-button{position:absolute;right:0;transform:translate(-100%,0);opacity:.3}.code-block-zx-script-execution-button:hover{opacity:.8}.code-block-zx-script-execution-button:active{opacity:1}article.zx-script-output{display:flex;flex-direction:column}\",\"tags\":\"$:/tags/Stylesheet\",\"type\":\"text/css\"},\"$:/plugins/linonetwo/zx-script/zx-button-caption\":{\"title\":\"$:/plugins/linonetwo/zx-script/zx-button-caption\",\"type\":\"text/vnd.tiddlywiki\",\"text\":\"ZX Script\"},\"$:/plugins/linonetwo/zx-script/zx-icon\":{\"title\":\"$:/plugins/linonetwo/zx-script/zx-icon\",\"tags\":\"$:/tags/Image\",\"text\":\"<svg width=\\\"22pt\\\" height=\\\"22pt\\\" class=\\\"tc-image-zx-script tc-image-button\\\" xmlns=\\\"http://www.w3.org/2000/svg\\\" xmlns:xlink=\\\"http://www.w3.org/1999/xlink\\\" version=\\\"1.1\\\" x=\\\"0px\\\" y=\\\"0px\\\" viewBox=\\\"0 0 100 90.25\\\"><path d=\\\"M93.103,83.904c0,0,1.708-1.634,3.242,0c1.535,1.634,0.322,2.203-0.223,2.426c-0.544,0.223-2.623,0.446-3.341-0.197 S92.635,84.248,93.103,83.904z\\\"></path><path d=\\\"M89.241,79.622c0,0,3.144-3.021,4.827-0.842c1.683,2.178,1.312,3.021-1.486,3.614c-2.796,0.595-3.861,1.263-5.37,0.595 c-1.511-0.669-2.005-2.13,0.495-2.724C89.241,79.622,89.241,79.622,89.241,79.622z\\\"></path><path d=\\\"M83.598,73.087c0.074-0.123,2.772-2.054,4.802-1.782c2.03,0.272,3.045,1.46,3.688,2.476s0.247,2.476-1.708,3.095 c-1.955,0.618-4.184,1.633-5.272,2.376c-1.089,0.742-2.302,0.989-3.317,0.42c-1.014-0.569-3.144-0.668-3.837-2.426 c-0.692-1.757,1.238-1.731,1.882-2.128S83.598,73.087,83.598,73.087z\\\"></path><path d=\\\"M74.191,66.429c0,0,4.554-3.218,7.476-3.664c2.921-0.445,4.652,0.99,5.891,2.625c1.237,1.633,2.438,4.059-1.603,5 c-4.041,0.94-5.328,2.029-6.17,2.524c-0.841,0.494-3.861,2.178-5.841,2.079c-1.98-0.099-4.803-1.089-5.842-1.881 c-1.04-0.792-2.872-1.733,0-3.268c2.871-1.535,4.059-2.277,4.059-2.277L74.191,66.429z\\\"></path><path d=\\\"M68.828,53.541c0,0,5.742-3.497,8.978-3.564c3.234-0.066,3.894,0.396,4.884,2.508c0.99,2.114,2.399,4.555,2.399,4.555 s0.636,2.708-2.796,3.631c-3.431,0.925-8.05,3.563-10.164,4.488c-2.113,0.925-6.206,4.818-11.024,4.753 c-4.817-0.065-8.978-1.518-10.297-3.894c-1.32-2.377,0.726-3.895,2.574-3.961c1.849-0.067,7.591-3.696,8.977-4.29 C63.746,57.173,68.828,53.541,68.828,53.541z\\\"></path><path d=\\\"M51.203,40.271l8.581-3.563c0,0,6.206-1.979,8.714-1.451c2.508,0.527,5.741,2.376,6.864,3.96s2.398,6.006,2.288,7.063 s-5.918,3.17-8.162,4.226c-2.245,1.056-13.004,6.007-14.588,7.128c-1.584,1.122-10.033,3.299-12.739,3.036 c-2.707-0.263-9.308-2.772-10.628-4.555s-3.367-3.763-0.99-4.95c2.376-1.188,6.138-2.573,8.118-3.828 C40.643,46.082,51.203,40.271,51.203,40.271z\\\"></path><path d=\\\"M28.894,11.229c0,0,14.158-0.198,16.535,0c2.376,0.198,13.367,0.495,16.139,2.277s5.941,5.644,7.128,8.614 c1.188,2.97,2.675,7.525,0,8.713c-2.673,1.188-14.06,5.743-17.425,7.624c-3.367,1.881-16.734,6.832-18.714,7.822 c-1.98,0.99-10.396,5.897-14.654,3.691c-4.258-2.205-7.525-11.91-7.921-14.384c0,0-2.475-5.743-2.673-15.644 c0,0-0.297-2.674-1.881-6.832c-1.584-4.159-3.07-6.436-3.07-6.436S0.675,0.833,4.041,2.516c3.366,1.683,2.818,2.161,6.237,3.564 c3.862,1.584,10,3.367,10.595,3.564C21.467,9.843,28.894,11.229,28.894,11.229z\\\"></path></svg>\"},\"$:/plugins/linonetwo/zx-script/zx-message.js\":{\"title\":\"$:/plugins/linonetwo/zx-script/zx-message.js\",\"type\":\"application/javascript\",\"module-type\":\"startup\",\"Modern.TiddlyDev#Origin\":\"zx-message.ts\",\"text\":\"\\\"use strict\\\";function execute(e,i,a=\\\"text/vnd.tiddlywiki\\\",n,s){if(\\\"observables\\\"in window){let t=e.replaceAll(/[$/:]/g,\\\"-\\\");if(!t.endsWith(\\\".mjs\\\")&&!t.endsWith(\\\".js\\\")&&!t.endsWith(\\\".md\\\"))switch(a){case\\\"text/vnd.tiddlywiki\\\":case\\\"text/plain\\\":case\\\"text/markdown\\\":case\\\"text/x-markdown\\\":case\\\"text/html\\\":t+=\\\".md\\\";break;case\\\"text/x-shellscript\\\":i=i.split(\\\"\\\\n\\\").map(t=>\\\"await $`\\\".concat(t.trim(),\\\"`;\\\")).join(\\\"\\\\n\\\"),t+=\\\".mjs\\\";break;case\\\"application/typescript\\\":t+=\\\".ts\\\";break;default:t+=\\\".mjs\\\"}n(),window.observables[\\\"native\\\"].executeZxScript$({fileContent:i,fileName:t}).subscribe(s)}}function executeOnTiddler(t,e=\\\"text\\\"){if(t){var i=$tw.wiki.getTiddler(t),a=(null==i?void 0:i.fields.type)||\\\"text/vnd.tiddlywiki\\\",i=(null==i?void 0:i.fields[e])||\\\"\\\";const n=\\\"$:/state/linonetwo/zx-script/output/\\\".concat(t);execute(t,i,a,()=>{$tw.wiki.setText(n,\\\"text\\\",void 0,\\\"\\\")},t=>{var e=$tw.wiki.getTiddlerText(n);$tw.wiki.setText(n,\\\"text\\\",void 0,\\\"\\\".concat(null!=e?e:\\\"\\\",\\\"\\\\n\\\").concat(null!=t?t:\\\"\\\")),$tw.wiki.setText(n,\\\"type\\\",void 0,\\\"text/vnd.tiddlywiki\\\")})}}function executeOnAnyCode(t,e,i=\\\"application/javascript\\\"){if(t){const a=\\\"$:/state/linonetwo/zx-script/output/\\\".concat(t);execute(\\\"zx-tmp-\\\".concat(t),e,i,()=>{$tw.wiki.setText(a,\\\"text\\\",void 0,\\\"\\\")},t=>{var e=$tw.wiki.getTiddlerText(a);$tw.wiki.setText(a,\\\"text\\\",void 0,\\\"\\\".concat(null!=e?e:\\\"\\\",\\\"\\\\n\\\").concat(null!=t?t:\\\"\\\")),$tw.wiki.setText(a,\\\"type\\\",void 0,\\\"text/vnd.tiddlywiki\\\")})}}exports.name=\\\"zx-message\\\",exports.platforms=[\\\"browser\\\",\\\"node\\\"],exports.after=[\\\"startup\\\"],exports.synchronous=!0,exports.startup=function(){$tw.rootWidget.addEventListener(\\\"tm-zx\\\",function(e){var i,a=null==(a=e.paramObject)?void 0:a.title;if(a)return executeOnTiddler(a,null==(a=e.paramObject)?void 0:a.field),!1;var a=null==(a=e.paramObject)?void 0:a.id,n=null==(n=e.paramObject)?void 0:n.code;if(a&&n){let t=(null==(i=e.paramObject)?void 0:i.mime)||\\\"text/vnd.tiddlywiki\\\";switch(null==(i=e.paramObject)?void 0:i.language){case\\\"markdown\\\":case\\\"md\\\":t=\\\"text/x-markdown\\\";break;case\\\"js\\\":case\\\"javascript\\\":t=\\\"application/javascript\\\";break;case\\\"ts\\\":case\\\"typescript\\\":t=\\\"application/typescript\\\";break;case\\\"bash\\\":case\\\"zsh\\\":case\\\"shell\\\":case\\\"sh\\\":t=\\\"text/x-shellscript\\\"}return executeOnAnyCode(a,n,t),!1}return!0})};\"}}}",
        "title": "$:/plugins/linonetwo/zx-script",
        "type": "application/json",
        "version": "1.0.0",
        "Modern.TiddlyDev#SHA256-Hashed": "a6d1f1d5e399af7b036dc169691f6217b152f9965647639d3680c219aac5b4c2"
    }
]