Skip to content

Instantly share code, notes, and snippets.

@magalhini
Created July 19, 2020 20:05
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 magalhini/0d66b591c71a11888293fa6a73c9dc31 to your computer and use it in GitHub Desktop.
Save magalhini/0d66b591c71a11888293fa6a73c9dc31 to your computer and use it in GitHub Desktop.
YAML with Markdown to JSON
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