From 530847148fc11e65fce8ad5f90e820a40f3b9b73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20B=C3=B6sch-Plepelits?= Date: Fri, 19 Aug 2022 18:21:09 +0100 Subject: [PATCH] Twig Functions: add 'json_pp' filter --- doc/TwigJS.md | 2 ++ src/twigFunctions.js | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/doc/TwigJS.md b/doc/TwigJS.md index 0e7e9982..16a3e5da 100644 --- a/doc/TwigJS.md +++ b/doc/TwigJS.md @@ -78,6 +78,8 @@ Extra filters: * filter `debug`: print the value (and further arguments) to the javascript console (via `console.log()`) * filter `wikipediaAbstract`: shows the abstract of a Wikipedia article in the selected data language (or, if not available, the language which was used in input, resp. 'en' for Wikidata input). Input is either 'language:article' (e.g. 'en:Douglas Adams') or a wikidata id (e.g. 'Q42'). * filter `wikidataEntity`: returns the wikidata entity in structured form (or `null` if the entity is not cached or `false` if it does not exist). Example: https://www.wikidata.org/wiki/Special:EntityData/Q42.json +* filter `json_pp`: JSON pretty print the object. As parameter to the filter, the following options can be passed: + * `indent`: indentation (default: 2) Notes: * Variables will automatically be HTML escaped, unless the filter raw is used, e.g.: `{{ tags.name|raw }}` diff --git a/src/twigFunctions.js b/src/twigFunctions.js index 40584ee2..333a89d0 100644 --- a/src/twigFunctions.js +++ b/src/twigFunctions.js @@ -140,3 +140,11 @@ OverpassLayer.twig.extendFilter('debug', function (value, param) { console.log.apply(null, [ value, ...param ]) return value }) +OverpassLayer.twig.extendFilter('json_pp', function (value, param) { + const options = param[0] || {} + + value = JSON.parse(JSON.stringify(value)) + delete value._keys // remove TwigJS artefact + + return JSON.stringify(value, null, 'indent' in options ? ' '.repeat(options.indent) : ' ') +})