UNPKG

longform-markdown-splitter

Version:

Splits and transforms markdown files from obsidian for usage in hugo.

168 lines (167 loc) 6.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CSV_Exporter = void 0; var fs = require("fs"); var client_http = require("http"); var client_https = require("https"); var url_1 = require("url"); var md_mapping_1 = require("./md-mapping"); var CSV_Exporter = (function () { function CSV_Exporter() { } CSV_Exporter.transform_to_json = function (job_parameter) { var csv_buffer = fs.readFileSync(job_parameter.readPath); var rows_array = csv_buffer.toString().split("\n"); var json_obj_array = []; var header_csv = rows_array[0]; var header_array = new Array(); if (header_csv.trim().length > 0) { var inSQuotes = false; var inDQuotes = false; var tmpParam = ""; for (var i = 0; i < header_csv.length; ++i) { var char = header_csv.substring(i, i + 1); if (char == "'") inSQuotes = inSQuotes ? false : !inDQuotes; else if (char == '"') inDQuotes = inDQuotes ? false : !inSQuotes; if (char == ",") { if (inSQuotes) tmpParam += char; else if (inDQuotes) tmpParam += char; else { header_array.push(CSV_Exporter.cleanup(tmpParam)); tmpParam = ""; } } else tmpParam += char; } header_array.push(CSV_Exporter.cleanup(tmpParam)); } for (var i = 1; i < rows_array.length - 1; i++) { var json_obj = {}; var str = rows_array[i]; var s = ""; var flag = 0; for (var _i = 0, str_1 = str; _i < str_1.length; _i++) { var ch = str_1[_i]; if (ch === '"' && flag === 0) { flag = 1; } else if (ch === '"' && flag == 1) flag = 0; if (ch === "," && flag === 0) ch = "|"; if (ch !== '"') s += ch; } var properties = s.split("|"); for (var j in header_array) { json_obj[header_array[j]] = properties[j]; } if (job_parameter.hasOwnProperty("mappings")) { var mapper = new md_mapping_1.MD_Mapper(); mapper.addMappings(job_parameter.mappings); mapper.do_mappings(json_obj, json_obj); } json_obj_array.push(json_obj); } var json = JSON.stringify(json_obj_array, null, 4); fs.writeFileSync(job_parameter.writePath, json); }; CSV_Exporter.download_all_images_from_json = function (source_file, target_folder, url_property_name) { var rawdata = fs.readFileSync(source_file); var json = JSON.parse(rawdata.toString()); if (Array.isArray(json)) { json.forEach(function (element) { if (url_property_name in element) { var url = element[url_property_name]; if (CSV_Exporter.is_valid_url(url)) { target_folder = target_folder.endsWith("/") ? target_folder : "".concat(target_folder, "/"); var name_1 = url.substring(url.lastIndexOf("/") + 1) + ".jpg"; console.log("Try Download from url: ".concat(url)); CSV_Exporter.download_image(url, "".concat(target_folder).concat(name_1)) .then(function (result) { element.Cover_Image = "".concat(target_folder).concat(name_1); fs.writeFileSync(source_file, JSON.stringify(json, null, 4)); }) .catch(console.error); } else { console.log("Download url invalid: ".concat(url)); } } }); } else { } }; CSV_Exporter.download_image = function (url, filepath) { return new Promise(function (resolve, reject) { var client = url.startsWith("https://") ? client_https : client_http; client.get(url, function (response) { if (response.statusCode === 200) { var write_stream = fs.createWriteStream(filepath); response .pipe(write_stream) .on("error", reject) .once("close", function () { return resolve(filepath); }); } else { response.resume(); reject(new Error("Download from ".concat(url, " Failed With Status Code: ").concat(response.statusCode))); } }); }); }; CSV_Exporter.is_valid_url_protocol = function (url) { try { var newUrl = new url_1.URL(url); return newUrl.protocol === "http:" || newUrl.protocol === "https:"; } catch (err) { return false; } }; CSV_Exporter.is_valid_url = function (url) { try { var myURL = new url_1.URL(url); return true; } catch (error) { console.log("".concat(error.input, " is not a valid url")); return false; } }; CSV_Exporter.trim_char = function (str, ch) { var start = 0, end = str.length; while (start < end && str[start] === ch) ++start; while (end > start && str[end - 1] === ch) --end; return start > 0 || end < str.length ? str.substring(start, end) : str; }; CSV_Exporter.cleanup = function (str) { str = str.replace(/[(),"-]/g, ''); str = str.replace(/[ ]/g, '_'); return str; }; CSV_Exporter.imageToBase64 = function (url, callback) { fetch(url) .then(function (response) { return response.blob(); }) .then(function (blob) { var reader = new FileReader(); reader.readAsDataURL(blob); reader.onloadend = function () { var base64String = reader.result; callback(base64String); }; }); }; return CSV_Exporter; }()); exports.CSV_Exporter = CSV_Exporter;