markdown-proofing
Version:
A markdown proofing platform for individuals, teams, and organizations.
180 lines (141 loc) • 5.71 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
var _rule = require('./rule');
var _rule2 = _interopRequireDefault(_rule);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var MarkdownProofing = function () {
function MarkdownProofing() {
_classCallCheck(this, MarkdownProofing);
this.analyzers = [];
this.rules = [];
}
// The `rootDirOverride` parameter helps with testing
_createClass(MarkdownProofing, [{
key: 'addAnalyzer',
value: function addAnalyzer(analyzer) {
if (this.analyzers.indexOf(analyzer) === -1) {
this.analyzers.push(analyzer);
}
return this;
}
}, {
key: 'addRule',
value: function addRule(messageType, ruleCondition) {
var _this = this;
// ruleCondition could be:
// info, warning < 5, error < 10
//
// We should apply each of these as a separate rule.
var ruleConditions = ruleCondition.split(',').map(function (x) {
return x.trim();
});
ruleConditions.forEach(function (rc) {
_this.rules.push(new _rule2.default(messageType, rc));
});
return this;
}
}, {
key: 'proof',
value: function proof(text) {
var _this2 = this;
var analyzerPromises = [];
this.analyzers.forEach(function (X) {
// Analyzers can return a promise that resolves with an
// `AnalyzerResult`, or they can simply return an `AnalyzerResult`.
var resultOrPromise = new X().analyze(text);
// If the analyzer output is not a promise,
// make it into a promise for consistency.
var promise = resultOrPromise.then ? resultOrPromise : Promise.resolve(resultOrPromise);
analyzerPromises.push(promise);
});
return Promise.all(analyzerPromises).then(function (x) {
// Collect any messages outputted by any analyzers
var analyzerMessages = [];
x.forEach(function (y) {
var applicableMessages = y.messages.filter(function (message) {
return _this2.rules.some(function (rule) {
return rule.matchesCondition(message);
});
});
applicableMessages.forEach(function (m) {
return analyzerMessages.push(m);
});
});
// Sort by message type ascending
analyzerMessages.sort(function (a, b) {
return a.type.localeCompare(b.type);
});
return {
messages: analyzerMessages
};
});
}
}, {
key: 'ruleExists',
value: function ruleExists(messageType) {
var existingRules = this.rules.filter(function (x) {
return x.messageType === messageType;
});
return existingRules.length > 0;
}
}, {
key: 'updateRule',
value: function updateRule(messageType, ruleCondition) {
this.rules = this.rules.filter(function (x) {
return x.messageType !== messageType;
});
return this.addRule(messageType, ruleCondition);
}
}], [{
key: 'createUsingConfiguration',
value: function createUsingConfiguration(configuration, rootDirOverride) {
var _this3 = this;
var markdownProofing = new MarkdownProofing();
if (configuration.presets) {
configuration.presets.forEach(function (x) {
var presetFilePath = (rootDirOverride || __dirname) + '/presets/' + x + '.json';
var presetConfiguration = JSON.parse(_fs2.default.readFileSync(presetFilePath, 'utf-8'));
_this3.addAssetsToInstance(markdownProofing, presetConfiguration, rootDirOverride);
});
}
this.addAssetsToInstance(markdownProofing, configuration, rootDirOverride);
return markdownProofing;
}
}, {
key: 'addAssetsToInstance',
value: function addAssetsToInstance(markdownProofing, configuration, rootDirOverride) {
if (configuration.analyzers) {
configuration.analyzers.forEach(function (x) {
var analyzer = require((rootDirOverride || __dirname) + '/analyzers/' + x + '.js');
markdownProofing.addAnalyzer(analyzer);
});
}
if (configuration.rules) {
var _loop = function _loop(prop) {
var ruleValue = configuration.rules[prop];
if (ruleValue === 'none') {
markdownProofing.rules = markdownProofing.rules.filter(function (x) {
return x.messageType !== prop;
});
} else if (markdownProofing.ruleExists(prop)) {
markdownProofing.updateRule(prop, ruleValue);
} else {
markdownProofing.addRule(prop, ruleValue);
}
};
for (var prop in configuration.rules) {
_loop(prop);
}
}
}
}]);
return MarkdownProofing;
}();
exports.default = MarkdownProofing;
module.exports = exports['default'];
;