Skip to content

Instantly share code, notes, and snippets.

@crutchcorn
Created March 27, 2021 05:56
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 crutchcorn/a7d7d9d5d9690b0497ce86280516aaa1 to your computer and use it in GitHub Desktop.
Save crutchcorn/a7d7d9d5d9690b0497ce86280516aaa1 to your computer and use it in GitHub Desktop.
const path = require('path');
const fs = require('fs');
const xml2js = require('xml2js');
async function walk(dir) {
const finalObj = {};
for await (const d of await fs.promises.opendir(dir)) {
const entry = path.join(dir, d.name);
if (d.isDirectory()) {
finalObj[d.name] = await walk(entry);
continue;
}
const contents = await fs.promises.readFile(entry);
finalObj[d.name] = contents;
continue;
}
return finalObj;
}
async function main() {
const fileContentObj = await walk(path.resolve('./Layouts'));
const parser = new xml2js.Parser(/* options */);
const fileParsedArr = await Promise.all(Object.entries(fileContentObj).map(async ([fileName, fileContents]) => {
const parsedObj = await parser.parseStringPromise(fileContents);
const keys = ['name', 'type', 'default', 'description'];
// Array
const entryArr = parsedObj['layout']['entry'];
let header = `|${keys.map(key => key.toUpperCase()).join('|')}|`;
let seperator = `|${keys.map(_ => '---').join('|')}|`;
const body = entryArr.map(entry => {
const entryArr = keys.map(key => {
const [val] = entry[key] || [''];
return val;
})
return `|${entryArr.join('|')}|`;
}).join('\n');
return [fileName, `${header}\n${seperator}\n${body}`];
}))
await Promise.all(fileParsedArr.map(async ([fileName, fileContents]) => {
await fs.promises.writeFile(path.resolve(`./output/${fileName}.md`), fileContents);
}))
}
main().catch(console.error)
@crutchcorn
Copy link
Author

At some point, I had to debug JSON and had to write this JSON:

"((?!name|type|default|size|entry|description).*)": \[

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment