Skip to content

Instantly share code, notes, and snippets.

@vsimko
Created February 7, 2018 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vsimko/9ecd8b51e40ac63a502824db100d2e78 to your computer and use it in GitHub Desktop.
Save vsimko/9ecd8b51e40ac63a502824db100d2e78 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
// TODO: this is currently just a helper script included from `fix-mkdocs-pages.sh`
const fs = require('fs')
const yaml = require('js-yaml')
function firstObjectKey(obj) {
return Object.keys(obj)[0]
}
function dfs(node) {
if (node instanceof Array) {
return node.map(x => dfs(x)) // go deeper
}
if (node instanceof Object) {
const key = firstObjectKey(node)
const entry = node[key]
if (entry instanceof Array) {
if (entry.length === 1) {
return entry[0] // the actual transformation happens here
} else {
node[key] = dfs(entry) // go deeper
}
}
}
return node
}
const data = fs.readFileSync('/dev/stdin', 'utf8')
const yamldoc = yaml.safeLoad(data)
const newdoc = dfs(yamldoc)
console.log(yaml.safeDump(newdoc))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment