Browse Source

Twig functions: add a 'yaml' filter

master
parent
commit
93b5626670
  1. 1
      doc/TwigJS.md
  2. 9
      src/twigFunctions.js

1
doc/TwigJS.md

@ -80,6 +80,7 @@ Extra filters:
* 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)
* filter `yaml`: YAML pretty print the object. As options the filter, all options to [yaml.dump of js-yaml](https://github.com/nodeca/js-yaml#dump-object---options-) can be used.
Notes:
* Variables will automatically be HTML escaped, unless the filter raw is used, e.g.: `{{ tags.name|raw }}`

9
src/twigFunctions.js

@ -5,6 +5,7 @@ var osmParseDate = require('openstreetmap-date-parser')
var osmFormatDate = require('openstreetmap-date-format')
const natsort = require('natsort').default
const md5 = require('md5')
const yaml = require('js-yaml')
var md5cache = {}
@ -148,3 +149,11 @@ OverpassLayer.twig.extendFilter('json_pp', function (value, param) {
return JSON.stringify(value, null, 'indent' in options ? ' '.repeat(options.indent) : ' ')
})
OverpassLayer.twig.extendFilter('yaml', function (value, param) {
const options = param[0] || {}
value = JSON.parse(JSON.stringify(value))
delete value._keys // remove TwigJS artefact
return yaml.dump(value, options)
})
Loading…
Cancel
Save