Browse Source

Added to top enable/disable methods & errors throw

pull/25/head
Vitaly Puzrin 10 years ago
parent
commit
9783b530db
  1. 38
      lib/index.js
  2. 20
      lib/ruler.js
  3. 2
      lib/rules_core/normalize.js
  4. 17
      test/misc.js

38
lib/index.js

@ -270,11 +270,13 @@ MarkdownIt.prototype.configure = function (presets) {
/** chainable
* MarkdownIt.enable(list)
* MarkdownIt.enable(list, ignoreInvalid)
* - list (String|Array): rule name or list of rule names to enable
* - ignoreInvalid (Boolean): set `true` to ignore errors when rule not found.
*
* Enable list or rules. It will automatically find appropriate components,
* containing rules with given names.
* containing rules with given names. If rule not found, and `ignoreInvalid`
* not set - throws exception.
*
* ##### Example
*
@ -284,24 +286,46 @@ MarkdownIt.prototype.configure = function (presets) {
* .disable('smartquotes');
* ```
**/
MarkdownIt.prototype.enable = function (list) {
MarkdownIt.prototype.enable = function (list, ignoreInvalid) {
var result = [];
if (!Array.isArray(list)) { list = [ list ]; }
[ 'core', 'block', 'inline' ].forEach(function (chain) {
this[chain].ruler.enable(list, true);
result = result.concat(this[chain].ruler.enable(list, true));
}, this);
var missed = list.filter(function (name) { return result.indexOf(name) < 0; });
if (missed.length && !ignoreInvalid) {
throw new Error('MarkdownIt. Failed to enable unknown rule(s): ' + missed);
}
return this;
};
/** chainable
* MarkdownIt.disable(list)
* MarkdownIt.disable(list, ignoreInvalid)
* - list (String|Array): rule name or list of rule names to disable.
* - ignoreInvalid (Boolean): set `true` to ignore errors when rule not found.
*
* The same as [[MarkdownIt.enable]], but turn specified rules off.
**/
MarkdownIt.prototype.disable = function (list) {
MarkdownIt.prototype.disable = function (list, ignoreInvalid) {
var result = [];
if (!Array.isArray(list)) { list = [ list ]; }
[ 'core', 'block', 'inline' ].forEach(function (chain) {
this[chain].ruler.disable(list, true);
result = result.concat(this[chain].ruler.disable(list, true));
}, this);
var missed = list.filter(function (name) { return result.indexOf(name) < 0; });
if (missed.length && !ignoreInvalid) {
throw new Error('MarkdownIt. Failed to disable unknown rule(s): ' + missed);
}
return this;
};

20
lib/ruler.js

@ -245,18 +245,22 @@ Ruler.prototype.push = function (ruleName, fn, options) {
/**
* Ruler.enable(list [, ignoreInvalid])
* Ruler.enable(list [, ignoreInvalid]) -> Array
* - list (String|Array): list of rule names to enable.
* - ignoreInvalid (Boolean): set `true` to ignore errors when rule not found.
*
* Enable rules with given names. If any rule name not found - throw Error.
* Errors can be disabled by second param.
*
* Returns list of found rule names (if no exception happened).
*
* See also [[Ruler.disable]], [[Ruler.enableOnly]].
**/
Ruler.prototype.enable = function (list, ignoreInvalid) {
if (!Array.isArray(list)) { list = [ list ]; }
var result = [];
// Search by name and enable
list.forEach(function (name) {
var idx = this.__find__(name);
@ -266,9 +270,11 @@ Ruler.prototype.enable = function (list, ignoreInvalid) {
throw new Error('Rules manager: invalid rule name ' + name);
}
this.__rules__[idx].enabled = true;
result.push(name);
}, this);
this.__cache__ = null;
return result;
};
@ -292,19 +298,21 @@ Ruler.prototype.enableOnly = function (list, ignoreInvalid) {
/**
* Ruler.disable(list [, ignoreInvalid])
* Ruler.disable(list [, ignoreInvalid]) -> Array
* - list (String|Array): list of rule names to disable.
* - ignoreInvalid (Boolean): set `true` to ignore errors when rule not found.
*
* Disable rules with given names. If any rule name not found - throw Error.
* Errors can be disabled by second param.
*
* Returns list of found rule names (if no exception happened).
*
* See also [[Ruler.enable]], [[Ruler.enableOnly]].
**/
Ruler.prototype.disable = function (list, ignoreInvalid) {
if (!Array.isArray(list)) {
list = [ list ];
}
if (!Array.isArray(list)) { list = [ list ]; }
var result = [];
// Search by name and disable
list.forEach(function (name) {
@ -315,9 +323,11 @@ Ruler.prototype.disable = function (list, ignoreInvalid) {
throw new Error('Rules manager: invalid rule name ' + name);
}
this.__rules__[idx].enabled = false;
result.push(name);
}, this);
this.__cache__ = null;
return result;
};

2
lib/rules_core/normalize.js

@ -16,7 +16,7 @@ module.exports = function inline(state) {
// Normalize newlines
str = state.src.replace(NEWLINES_RE, '\n');
// Strin NULL characters
// Replace NULL characters
str = str.replace(NULL_RE, '\uFFFD');
// Replace tabs with proper number of spaces (1..4)

17
test/misc.js

@ -159,6 +159,23 @@ describe('API', function () {
assert.deepEqual(was, back);
});
it('bulk enable/dusable with errors control', function () {
var md = markdownit();
assert.throws(function () {
md.enable([ 'link', 'code', 'invalid' ]);
});
assert.throws(function () {
md.disable([ 'link', 'code', 'invalid' ]);
});
assert.doesNotThrow(function () {
md.enable([ 'link', 'code' ]);
});
assert.doesNotThrow(function () {
md.disable([ 'link', 'code' ]);
});
});
});

Loading…
Cancel
Save