in tool/md2json.js [7:127]
async function convert(opts) {
// globby does not support '\' yet
const mdPath = opts.path.replace(/\\/g, '/');
const sectionsAnyOf = opts.sectionsAnyOf;
const entry = opts.entry;
const tplEnv = opts.tplEnv;
const maxDepth = opts.maxDepth || 10;
const engineConfig = {
commandOpen: '{{',
commandClose: '}}',
missTarget: 'error'
};
const etplEngine = new etpl.Engine(engineConfig);
etplEngine.addFilter('default', function (source, defaultVal) {
return (source === '' || source == null) ? defaultVal : source;
});
const files = (await globby([mdPath])).filter(function (fileName) {
return fileName.indexOf('__') !== 0;
});
const mdTpls = files.map(function (fileName) {
return fs.readFileSync(fileName, 'utf-8');
});
let mdStr;
// ETPL do not support global variables, without which we have to pass
// parameters like `galleryViewPath` each time `{{use: ...}}` used, which
// is easy to forget. So we mount global variables on Object prototype when
// ETPL rendering.
// I know this approach is ugly, but I am sorry that I have no time to make
// a pull request to ETPL yet.
Object.keys(tplEnv).forEach(function (key) {
if (Object.prototype.hasOwnProperty(key)) {
throw new Error(key + ' can not be used in tpl config');
}
Object.prototype[key] = tplEnv[key];
});
function clearEnvVariables() {
// Restore the global variables.
Object.keys(tplEnv).forEach(function (key) {
delete Object.prototype[key];
});
}
try {
// Render tpl
etplEngine.compile(mdTpls.join('\n'));
mdStr = etplEngine.getRenderer(entry)({});
clearEnvVariables();
}
catch (e) {
// Fild wichi file has error.
mdTpls.forEach((tpl, idx) => {
try {
const debugEngine = new etpl.Engine(engineConfig);
const renderer = debugEngine.compile(tpl);
renderer({});
}
catch (e) {
console.error(`Has syntax error in ${files[idx]}`)
}
});
clearEnvVariables();
throw e;
}
// Markdown to JSON
const schema = mdToJsonSchema(mdStr, maxDepth, opts.imageRoot, entry);
// console.log(mdStr);
let topLevel = schema.option.properties;
(sectionsAnyOf || []).forEach(function (componentName) {
const newProperties = schema.option.properties = {};
const componentNameParsed = componentName.split('.');
componentName = componentNameParsed[0];
for (const name in topLevel) {
if (componentNameParsed.length > 1) {
newProperties[name] = topLevel[name];
const secondLevel = topLevel[name].properties;
const secondNewProps = topLevel[name].properties = {};
for (const secondName in secondLevel) {
makeOptionArr(
secondName, componentNameParsed[1], secondNewProps, secondLevel
);
}
}
else {
makeOptionArr(name, componentName, newProperties, topLevel);
}
}
topLevel = newProperties;
function makeOptionArr(nm, cptName, newProps, level) {
const nmParsed = nm.split('.');
if (nmParsed[0] === cptName) {
newProps[cptName] = newProps[cptName] || {
'type': 'Array',
'items': {
'anyOf': []
}
};
// Use description in excatly #series
if (cptName === nm) {
newProps[cptName].description = level[nm].description;
}
else {
newProps[cptName].items.anyOf.push(level[nm]);
}
}
else {
newProps[nm] = level[nm];
}
}
});
return schema;
}