This repository has been archived on 2023-01-27. You can view files and clone it, but cannot push or open issues or pull requests.
cad/lib/content/md.js
2022-10-15 19:16:08 +02:00

79 lines
3.2 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveConfig = exports.read_fragments = exports.md_edit_wrap = exports.parse_config = void 0;
const debug = require("../..");
const path = require("path");
const dir_1 = require("@plastichub/fs/dir");
const read_1 = require("@plastichub/fs/read");
const exists_1 = require("@plastichub/fs/exists");
const utils_1 = require("@plastichub/core/utils");
const osr_cli_commons_1 = require("@plastichub/osr-cli-commons");
const primitives_1 = require("@plastichub/core/primitives");
const lib_1 = require("../../lib/");
const js_beautify_1 = require("js-beautify");
const md_tables = require('markdown-table');
exports.parse_config = (config, root) => {
if (Object.keys(config)) {
for (const key in config) {
let val = config[key];
if (primitives_1.isArray(val)) {
if (key !== 'authors') {
config[key] = lib_1.md2html(md_tables(val));
}
}
else if (primitives_1.isString(val)) {
if (val.endsWith('.csv')) {
const parsed = path.parse(root);
let csv = path.resolve(`${parsed.dir}/${parsed.base}/${val}`);
debug.info("Parsing CSV " + csv);
if (exists_1.sync(csv)) {
csv = read_1.sync(csv) || "";
try {
csv = lib_1.md2html(lib_1.csvToMarkdown(csv));
config[key] = csv;
}
catch (e) {
debug.error(`Error converting csv to md ${val}`);
}
}
else {
debug.error(`Can't find CSV file at ${csv}`, parsed);
}
}
}
}
}
};
exports.md_edit_wrap = (content, f, prefix = '', context = '') => {
return js_beautify_1.html_beautify(`<div prefix="${prefix}" file="${path.parse(f).base}" context="${context}" class="fragment">${content}</div>`);
};
exports.read_fragments = (src, config, prefix = '', context = '') => {
if (!exists_1.sync(src)) {
debug.warn(`Create template folder ${src}`);
dir_1.sync(src);
}
let fragments = osr_cli_commons_1.files(src, '*.html');
fragments.map((f) => {
config[path.parse(f).name] = exports.md_edit_wrap(lib_1.toHTML(f, true), f, prefix, context);
});
fragments = osr_cli_commons_1.files(src, '*.md');
fragments.map((f) => {
config[path.parse(f).name] = exports.md_edit_wrap(lib_1.toHTML(f, false), f, prefix, context);
});
return config;
};
const _resolve = (config) => {
for (const key in config) {
if (config[key] && typeof config[key] == 'string') {
const resolved = utils_1.substitute(config[key], config);
config[key] = resolved;
}
}
return config;
};
exports.resolveConfig = (config) => {
config = _resolve(config);
config = _resolve(config);
return config;
};
//# sourceMappingURL=md.js.map