1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-10-30 14:46:19 +00:00
TiddlyWiki5/plugins/tiddlywiki/katex/ui/config/macro.tid
Jieao Song 62610f0666
Add custom macro editor for KaTeX plugin (#5933)
* Add custom macro editor for KaTeX plugin

* Use list

* Better escape

* Tweaks

* Remove const

* Catch bad macros

* Capitalize tags

* Name KaTeX-macro tiddlers properly

* UI tweaks

* Move input string to temp

* Improve UI; import macros using LaTeX cmd directly
2021-09-10 21:28:13 +01:00

138 lines
4.1 KiB
Plaintext

title: $:/plugins/tiddlywiki/katex/ui/config/macro
tags: $:/tags/KaTeX/Config
caption: Custom macro editor
\define katex-escape(text)
<$vars t=$text$>
<$latex text={{{[<t>search-replace:g:regexp[(?<!\\)#(\d)],[{\#_$1}]]}}}>
</$vars>
\end
\define katex-create-macro()
<$vars loc={{$:/temp/katex/new-macro-name}}>
<$action-createtiddler $basetitle={{{[<loc>addprefix[$:/plugins/tiddlywiki/katex/macros/]]}}} tags="$:/tags/KaTeX/Macro" type="text/plain" caption={{$:/temp/katex/new-macro-name}} text={{$:/temp/katex/new-macro-command}} $overwrite=yes/>
</$vars>
\end
\define katex-edit-cell()
<$reveal state="$:/temp/katex/edit-macro" type=match text={{!!title}}>
<$edit-text tiddler=<<currentTiddler>> tag="input" default=""/>
</$reveal>
\end
\define katex-view-cell()
<$reveal state="$:/temp/katex/edit-macro" type=nomatch text={{!!title}}>
<tt><$view field=text/></tt>
</$reveal>
\end
\define katex-edit-macro-button()
<$reveal state="$:/temp/katex/edit-macro" type=nomatch text={{!!title}}>
<$button class="tc-btn-invisible tc-btn-dropdown">
<$action-setfield $tiddler="$:/temp/katex/edit-macro" text={{!!title}}/>
{{$:/core/images/edit-button}}
</$button>
</$reveal>
\end
\define katex-save-macro-button()
<$reveal state="$:/temp/katex/edit-macro" type=match text={{!!title}}>
<$button class="tc-btn-invisible tc-btn-dropdown">
<$action-deletetiddler $tiddler="$:/temp/katex/edit-macro"/>
{{$:/core/images/done-button}}
</$button>
</$reveal>
\end
<table>
<tr>
<th>Macro</th>
<th>Command</th>
<th>Preview</th>
<th></th>
</tr>
<$list filter="[all[shadows+tiddlers]tag[$:/tags/KaTeX/Macro]!has[draft.of]sort[caption]]">
<tr>
<td><tt><$text text={{!!caption}}/></tt></td>
<td><<katex-edit-cell>><<katex-view-cell>></td>
<td><<katex-escape {{!!text}}>></td>
<td>
<<katex-edit-macro-button>>
<<katex-save-macro-button>>
<$button class="tc-btn-invisible tc-btn-dropdown">
<$action-deletetiddler $tiddler={{!!title}}/>
{{$:/core/images/delete-button}}
</$button>
</td>
</tr>
</$list>
<tr>
<td colspan="4" align="left">Add a new macro</td>
</tr>
<tr>
<td><$edit-text tiddler="$:/temp/katex/new-macro-name" tag="input" default=""/></td>
<td><$edit-text tiddler="$:/temp/katex/new-macro-command" tag="input" default=""/></td>
<td><<katex-escape {{$:/temp/katex/new-macro-command}}>></td>
<td><$button actions=<<katex-create-macro>>>
{{$:/language/EditTemplate/Fields/Add/Button}}
</$button></td>
</tr>
<tr><td colspan="4" align="left">
<details>
<summary>Import</summary>
<$edit-text tiddler="$:/temp/katex/import-macro" tag="textarea" default="" class="tc-edit-texteditor" placeholder="You can type commands like \def\ZZ{\mathbb{Z}} and import them automatically."/>
<$vars macros={{$:/temp/katex/import-macro}} sep="%.*\n|\n+" re="^\\g?def([^{]*){(.*)}.*">
<$button>
<$list filter="[<macros>splitregexp<sep>regexp<re>]" variable=line>
<$vars m={{{[<line>search-replace:g:regexp<re>,[$1]]}}} c={{{[<line>search-replace:g:regexp<re>,[$2]]}}}>
<$action-createtiddler $basetitle={{{[<m>addprefix[$:/plugins/tiddlywiki/katex/macros/]]}}} tags="$:/tags/KaTeX/Macro" type="text/plain" caption=<<m>> text=<<c>> $overwrite=yes/>
</$vars>
</$list>
Import
</$button>
<$button>
<$action-setfield $tiddler="$:/temp/katex/import-macro" text=""/>
Clear
</$button>
''Preview''
<table>
<$list filter="[<macros>splitregexp<sep>regexp<re>]" variable=line>
<$vars m={{{[<line>search-replace:g:regexp<re>,[$1]]}}} c={{{[<line>search-replace:g:regexp<re>,[$2]]}}}>
<tr>
<td><tt><<m>></tt></td>
<td><tt><<c>></tt></td>
<td><$macrocall $name=katex-escape text="<<c>>"/></td></tr>
</$vars>
</$list>
</table>
</$vars>
</details>
</td>
</tr>
<tr><td colspan="4" align="left">
<details>
<summary>Usage</summary>
<ul>
<li>
You can add entries like `\ZZ`, `\mathbb{Z}`, which will render as <$latex text="\mathbb{Z}"/>.
</li>
<li>
An entry mapping `\dd#1#2` to `\frac{d#1}{d#2}` will create a macro with two arguments; `\dd{f}{x}` will then render as <$latex text="\frac{df}{dx}"/>.
</li>
<li>
Note that the macros defined here have higher priority than those defined using `\gdef`. Also, deleted macros will continue to exist until a full refresh.
</li>
</ul>
</details>
</td>
</tr>
</table>