Created
July 19, 2020 20:05
-
-
Save magalhini/0d66b591c71a11888293fa6a73c9dc31 to your computer and use it in GitHub Desktop.
YAML with Markdown to JSON
This file contains hidden or 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
import yamlhead from "yamlhead"; | |
import marked from "marked"; | |
const path = require("path"); | |
const fs = require("fs"); | |
const directoryPath = path.join(__dirname, "../data"); | |
async function readDir() { | |
try { | |
await fs.readdir(directoryPath, (err, files) => { | |
let jsonData = []; | |
if (err) { | |
return console.log("Unable to scan directory: " + err); | |
} | |
files.forEach(async function (file, idx) { | |
await yamlhead(`${directoryPath}/${file}`, (err, yaml, data) => { | |
if (err) console.log("Error parsing YAML file: ", err); | |
jsonData = jsonData.concat({ | |
title: yaml.title, | |
body: data, | |
search: yaml.help, | |
}); | |
console.log(marked(data)); | |
idx === files.length - 1 && makeObject(jsonData); | |
}); | |
}); | |
}); | |
} catch (err) { | |
console.log("Error reading directory: ", err); | |
} | |
} | |
async function makeObject(data) { | |
const json = JSON.stringify(data, null, 2); | |
await fs.writeFile("./alldata.json", json, (err) => { | |
if (err) console.log("Error writing JSON file"); | |
console.log("Done!"); | |
}); | |
} | |
readDir(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment