I’m imagining a plugin to use images from Kroki, which is an omnibus text-to-diagram tool. I’m not ready to start coding it yet, but was thinking through how I might do it.
Obviously, this would be simple enough to do the same way PlantUML does it, online only. But I think we could do better than that. I think we should be able to cache the image generated and only create new ones when the underlying text is changed (and saved). This would mean that we would only need to be online when creating and saving such images, not when using them.
I’m picturing it as an entirely new tiddler type with multiple fields being used for the process, text, of course, but also stored, which caches a hash of the text and which we can can use to check for changes., and rendered which stores the SVG data returned by Kroki.
Add a new tiddler type for Kroki. No idea of a proper MIME type, but when triggered (by a save or at startup), it would
- deflate and base64 encode the
textfield - check if the result matches the
storedfield- if it did, call the
image/svg+xmlformat with therenderedfield, rendering that - if not, and offline, render the
textfield as a formattedtextblock with some sort of warning that the data cannot be retrieved while offline, a remove anyrenderedproperty (or leave it to show the older value?) - if not, and online, call Kroki’s API with the value of the
inputTypefield and this value- if this returns an error, display the error near the editing box
- if the return is good, store the resulting value in the
renderedfield, and render that.
- if it did, call the
Also add a startup action that – if we’re online – scans for this type of tiddler without a rendered property and performs most of the above.
Major Problem: While this would be either an extension to ImageParser.js, or more likely an alternative to it, I don’t yet know how that’s called, and it looks quite possible that such functions do not have access to the tiddler fields. (If they do, it would have to be passed through the options parameter.)
An alternative to storing this information in the tiddler would be to have a single data tiddler (plain text or JSON) with keys of these hashes and values of the SVG text. I like this less; if that tiddler gets screwed up, all your images might be destroyed, but it might be simpler to set up. And it might let us just use a widget or macro instead of a new type.
Questions:
- Does the above sound reasonable?
- Where to do type parsers such as ImageParser.js get invoked?
- Does the
optionsobject they’re supplied include the current tiddler or offer a way to get to it? - Is there a
deflatetool built into the core, or would the plugin need to supply one?