Browse Source

Separated enable by whitelist method

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
aea94c4e32
  1. 2
      lib/index.js
  2. 43
      lib/ruler.js
  3. 19
      test/ruler.js

2
lib/index.js

@ -77,7 +77,7 @@ MarkdownIt.prototype.configure = function (presets) {
if (presets.components) { if (presets.components) {
Object.keys(presets.components).forEach(function (name) { Object.keys(presets.components).forEach(function (name) {
if (presets.components[name].rules) { if (presets.components[name].rules) {
self[name].ruler.enable(presets.components[name].rules, true); self[name].ruler.enableOnly(presets.components[name].rules);
} }
}); });
} }

43
lib/ruler.js

@ -148,37 +148,40 @@ Ruler.prototype.push = function (ruleName, fn, options) {
}; };
// Enable list of rules by names. If `strict` is true, then all non listed // Enable rules by names.
// rules will be disabled.
// //
Ruler.prototype.enable = function (list, strict) { Ruler.prototype.enable = function (list, ignoreInvalid) {
if (!Array.isArray(list)) { if (!Array.isArray(list)) { list = [ list ]; }
list = [ list ];
}
// In strict mode disable all existing rules first
if (strict) {
this.__rules__.forEach(function (rule) {
rule.enabled = false;
});
}
// Search by name and enable // Search by name and enable
list.forEach(function (name) { list.forEach(function (name) {
var idx = this.__find__(name); var idx = this.__find__(name);
if (idx < 0) { throw new Error('Rules manager: invalid rule name ' + name); } if (idx < 0) {
if (ignoreInvalid) { return; }
throw new Error('Rules manager: invalid rule name ' + name);
}
this.__rules__[idx].enabled = true; this.__rules__[idx].enabled = true;
}, this); }, this);
this.__cache__ = null; this.__cache__ = null;
}; };
// Disable list of rules by names. // Enable rules by whitelisted names (others will be disables).
// //
Ruler.prototype.disable = function (list) { Ruler.prototype.enableOnly = function (list, ignoreInvalid) {
if (!Array.isArray(list)) { list = [ list ]; }
this.__rules__.forEach(function (rule) { rule.enabled = false; });
this.enable(list, ignoreInvalid);
};
// Disable rules by names.
//
Ruler.prototype.disable = function (list, ignoreInvalid) {
if (!Array.isArray(list)) { if (!Array.isArray(list)) {
list = [ list ]; list = [ list ];
} }
@ -187,9 +190,11 @@ Ruler.prototype.disable = function (list) {
list.forEach(function (name) { list.forEach(function (name) {
var idx = this.__find__(name); var idx = this.__find__(name);
if (idx < 0) { throw new Error('Rules manager: invalid rule name ' + name); } if (idx < 0) {
if (ignoreInvalid) { return; }
throw new Error('Rules manager: invalid rule name ' + name);
}
this.__rules__[idx].enabled = false; this.__rules__[idx].enabled = false;
}, this); }, this);
this.__cache__ = null; this.__cache__ = null;

19
test/ruler.js

@ -87,7 +87,7 @@ describe('Ruler', function () {
ruler.push('test', function foo() {}); ruler.push('test', function foo() {});
ruler.push('test2', function bar() {}); ruler.push('test2', function bar() {});
ruler.enable('test', true); ruler.enableOnly('test');
rules = ruler.getRules(''); rules = ruler.getRules('');
assert.strictEqual(rules.length, 1); assert.strictEqual(rules.length, 1);
}); });
@ -131,4 +131,21 @@ describe('Ruler', function () {
}); });
}); });
it('should not fail on invalid rule name in silent mode', function () {
var ruler = new Ruler();
ruler.push('test', function foo() {});
assert.doesNotThrow(function () {
ruler.enable('invalid name', true);
});
assert.doesNotThrow(function () {
ruler.enableOnly('invalid name', true);
});
assert.doesNotThrow(function () {
ruler.disable('invalid name', true);
});
});
}); });

Loading…
Cancel
Save