Escaping wikitext to JSON-valid string in templates

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Escaping wikitext to JSON-valid string in templates

Tom Schulze-2
Hello everyone,

I am having trouble escaping and displaying wikitext in a way that is
JSON-safe. I did some research but none of the provided
MagicWords/ParserFUnctions/etc seem to be suited for this purpose.
Please refer to my gitLab snippet <https://gitlab.com/snippets/1723632>
to see the sample code of the query, template and widget.

My goal is to build up a structure like this using a widget (to load my
custom JS), a cargo query and finally a template to display the items
row by row:

<div class="item" data-item='{"content":"data from a mediawiki form
field"}'>Description</div>
<div class="item" data-item='{"content":"data from a mediawiki form
field"}'>Description</div>

The data is taken from a PageForms textarea form field (the user can
enter any data she wants). This piece of HTML gets parsed by a custom
Java Script on page load for further processing. As soon as characters
like single (') or double quotes (") appear, the whole JSON string gets
messed up and the JavaScript JSON parser throws errors. Even worse, the
DOM structure becomes fragmented when single quotes appear. So
client-side fixing w/ JavaScript is impossible/tedious.

Any solution is welcome, also restricting the types of characters used.
Ideally, I would just need to wrap the parameter passed from the query
to the template in some kind of Magic Word which escapes/strips out
unwanted characters.

What options do I have ? I am open for different approaches...

Kind regards,

Tom

_______________________________________________
Wikitech-l mailing list
[hidden email]
https://lists.wikimedia.org/mailman/listinfo/wikitech-l