var fs = require('fs'); var path = require('path'); // parameters var inputFolder = "notes"; var outputFolder = "result"; var includeheaders = true; var files = fs.readdirSync(inputFolder); for (var i in files) { var file = files[i]; if (file.indexOf(".uecd") === -1) { fs.copyFileSync(path.join(inputFolder, file), path.join(outputFolder, file)); } else { var data = fs.readFileSync(path.join(inputFolder, file), 'utf8'); var md = mdify(JSON.parse(data), file); fs.writeFileSync(path.join(outputFolder, file).replace(".uecd", ".md"), md); } } function mdify(data, file) { var result = ""; var tags = []; for (var i in data.ops) { var op = data.ops[i]; if (op.insert.image) { // inline base64 //result += "![](" + op.insert.image + ")"; //external var alt = op.attributes && op.attributes.alt ? op.attributes.alt : ""; if (op.insert.image.indexOf("data:image") !== -1) { var buffer = Buffer.from(op.insert.image.replace(/^data:image\/jpeg;base64,/, ""), "base64"); var imageFileName = file.replace(".uecd", i + ".jpeg"); fs.writeFileSync(path.join(outputFolder, imageFileName), buffer); result += "![" + alt + "](" + imageFileName + ")"; } else { result += "![" + alt + "](" + op.insert.image + ")"; } } else if (!op.attributes) { if (op.insert.divider) { result += "---\n"; } else { result += op.insert; } } else if (op.attributes.italic) { result += "*" + op.insert + "*"; } else if (op.attributes.bold) { result += "**" + op.insert + "**"; } else if (op.attributes.underline) { result += "__" + op.insert + "__"; } else if (op.attributes.strike) { result += "~~" + op.insert + "~~"; } else if (op.attributes.tag) { //result += "#" + op.insert; tags.push(op.insert); } else if (op.attributes.header) { var result = result.split("\n"); result[result.length - 1] = "#".repeat(op.attributes.header) + " " + result[result.length - 1]; result = result.join("\n"); result += op.insert; } else if (op.attributes.link) { if (op.insert == op.attributes.link) { result += op.insert; } else { result += "[" + op.insert + "](" + op.attributes.link + ")"; } } else if (op.attributes.code) { result += "`" + op.insert + "`"; } else if (op.attributes.list == "bullet") { var result = result.split("\n"); result[result.length - 1] = "\t".repeat(op.attributes.indent) + "* " + result[result.length - 1]; result = result.join("\n"); result += op.insert; } else if (op.attributes.list == "unchecked") { var result = result.split("\n"); result[result.length - 1] = "\t".repeat(op.attributes.indent) + "[ ] " + result[result.length - 1]; result = result.join("\n"); result += op.insert; } else if (op.attributes.list == "checked") { var result = result.split("\n"); result[result.length - 1] = "\t".repeat(op.attributes.indent) + "[x] " + result[result.length - 1]; result = result.join("\n"); result += op.insert; } else if (op.attributes.list == "ordered") { var result = result.split("\n"); result[result.length - 1] = "\t".repeat(op.attributes.indent) + "1. " + result[result.length - 1]; result = result.join("\n"); result += op.insert; } else if (op.attributes.file) { result += "[[" + op.attributes.file.filetitle + "]]"; // traditional link //result += "[" + op.attributes.file.filetitle + "](" + op.attributes.file.filetitle + ".md)"; } else if (op.attributes["code-block"]) { var result = result.split("\n"); result[result.length - 1] = "```" + result[result.length - 1] + "```"; result = result.join("\n"); result += op.insert.replace(/\n\n/g, "\n``````\n");; } else if (op.attributes.blockquote) { var result = result.split("\n"); result[result.length - 1] = "> " + result[result.length - 1]; result = result.join("\n"); result += op.insert.replace(/\n\n/g, "\n> \n"); } else { result += op.insert; } } // Post processing // remove nbsp, indent and trailing result = result.replace(/ /g, " "); result = result.replace(/\n[ ]*/g, "\n"); result = result.replace(/[ ]*\n/g, "\n"); // insert line endings result = result.split('\n'); var blockMarkers = ["* ", "1. ", "> ", "```", "["]; for (var i = 0; i < result.length; i++) { var inBlock = false; for (var j in blockMarkers) { var block = blockMarkers[j]; if (result[i].replace(/\t/g, "").startsWith(block) && result[i + 1].replace(/\t/g, "").startsWith(block)) { inBlock = true; break; } } if (!inBlock && Boolean(result[i + 1])) { result.splice(i + 1, 0, ""); i++; } } result = result.join("\n"); // merge code blocks result = result.replace(/```\n```/g, "\n"); result = result.replace(/```/g, "\n```\n"); // escape ~ in unix path result = result.replace(/~\//g, "\\~/"); // headers if (includeheaders) { var headers = { title: file.replace(".uecd", ""), tags: tags.join(", ") }; if (/[.|mars|mai|juin|août] 202\d /.test(file)) { var index = file.indexOf("202"); headers.title = headers.title.substring(index + 5); headers.date = file.substring(0, index + 4); } var headersmd = "---\n"; for (var i in headers) { headersmd += i + ": " + headers[i] + "\n"; } headersmd += "---"; if (result[0] != "\n") headersmd += "\n"; if (result[1] != "\n") headersmd += "\n"; result = headersmd + result; } return result; }