Created
February 7, 2018 11:21
-
-
Save vsimko/9ecd8b51e40ac63a502824db100d2e78 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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