@diplodoc/transform
Version:
A simple transformer of text in YFM (Yandex Flavored Markdown) to HTML
56 lines • 2.01 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
const chalk_1 = require("chalk");
const getObject_1 = __importDefault(require("../getObject"));
const log_1 = require("../log");
const argv_1 = __importDefault(require("./services/argv"));
const evaluation_1 = require("./evaluation");
const lexical_1 = require("./lexical");
const substitutions = (str, builtVars, path) => {
const { keepNotVar } = argv_1.default.getConfig();
if ((0, lexical_1.isSingleVariable)(str)) {
const match = str.match(lexical_1.singleVariable);
if (!match) {
return str;
}
const trimVarPath = match[1].trim();
const value = substituteVariable(trimVarPath, builtVars);
if (value === undefined) {
logNotFoundVariable(trimVarPath, path);
return str;
}
return value;
}
return str.replace(lexical_1.vars, (match, _groupNotVar, flag, groupVar, groupVarValue) => {
if (flag) {
return keepNotVar ? _groupNotVar : groupVar;
}
const trimVarPath = groupVarValue.trim();
if (trimVarPath.startsWith('.')) {
return groupVar;
}
const value = substituteVariable(trimVarPath, builtVars);
if (value === undefined) {
logNotFoundVariable(trimVarPath, path);
return match;
}
return value;
});
};
function logNotFoundVariable(varPath, path) {
log_1.log.warn(`Variable ${(0, chalk_1.bold)(varPath)} not found${path ? ` in ${(0, chalk_1.bold)(path)}` : ''}`);
}
function substituteVariable(varPath, builtVars) {
let value;
if ((0, lexical_1.isVariable)(varPath)) {
value = (0, getObject_1.default)(varPath, builtVars);
}
else {
value = (0, evaluation_1.evalExp)(varPath, builtVars);
}
return value;
}
module.exports = substitutions;
//# sourceMappingURL=substitutions.js.map
;