Best way to store key-value pairs in plugin settings?

I’m working on a very simple plugin to perform simple textual replacements in a Discourse post. Specifically, when a user types /keyword in a post, I want to replace that sequence with an arbitrary string (in this case, a kaomoji, so that I can map /shrug to ¯\_(ツ)_/¯). Yes, it’s very silly.

In any case, the plugin is mostly complete: I can detect the pattern and perform the replacement in my dev environment. However, the mapping from keyword to kaomoji is currently specified as a Javascript literal in the plugin’s discourse-markdown Javascript file. That’s not great.

I’d like to be able to define the mapping of keywords to kaomoji (or whatever) in the plugin’s settings so that I don’t have to modify a server-side Javascript file every time I want to change what mappings are allowed. I cannot find a good way to do this. The closest I’ve come is using a “list” setting (with values like shrug:¯\_(ツ)_/¯, but that gives me back a string of | seperated strings, which is problematic when working with values like kaomoji that are highly likely to contain | in them.

I could hack around this by disallowing | in the mapping and splitting the list myself, but before I do, I thought I’d ask if there were a better way than “list” to do what I’m talking about. Is there any reasonable way to create and edit set of key-value pairs through a plugin’s settings? What’s the best way to approach this for both ease of editing and maximum permissiveness in the allowed characters?

5 Likes

Overnight it occurred to me that I could also simply use (for example) a JSON string that represented the mapping, then parse that string in the plugin. A JSON string would have the advantage of having escape logic for special characters built in. To really make this work though, you’d want a multiline text box to edit the setting, and the one post I found while searching for this functionality simply recommended a “list” (which was indeed appropriate for the use case in question).

Anyway, some form of first class key-value pair support would be ideal, but this seems like the second-best option. Is a multiline text box possible?

There is first class key-value see: PluginStore, example here: https://meta.discourse.org/t/brand-new-plugin-interface/8793/29

But, if you went this path you would also need to build admin UI involving a controller and ember view to handle data entry into it, also you would need to figure out how to ship the information to the client. Totally doable but a pretty large amount of work.

3 Likes

I wanted to get an initial version out the door, so I have deferred going with this heavy-weight route, and opted for having the user enter a string of JSON to configure the supported kaomoji replacements. It’s clunky, but it works.

In any case, I have the plugin live on my server now. You can find it here. I welcome any feedback. I’m a web, Rails, and Discourse newb, so I’m eager to learn. I did my best to ape the practices of other popular plugins. Thanks for your help.