Browse Source

Added bulk enable/disable sugar

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
abaf1e3553
  1. 20
      lib/index.js
  2. 32
      test/misc.js

20
lib/index.js

@ -84,6 +84,24 @@ MarkdownIt.prototype.configure = function (presets) {
};
// Sugar to enable rules by names in all chains at once
//
MarkdownIt.prototype.enable = function (list) {
[ 'core', 'block', 'inline' ].forEach(function (chain) {
this[chain].ruler.enable(list, true);
}, this);
};
// Sugar to disable rules by names in all chains at once
//
MarkdownIt.prototype.disable = function (list) {
[ 'core', 'block', 'inline' ].forEach(function (chain) {
this[chain].ruler.disable(list, true);
}, this);
};
// Sugar for curried plugins init:
//
// var md = new MarkdownIt();
@ -109,6 +127,7 @@ MarkdownIt.prototype.parse = function (src, env) {
return state.tokens;
};
// Main method that does all magic :)
//
MarkdownIt.prototype.render = function (src, env) {
@ -129,6 +148,7 @@ MarkdownIt.prototype.parseInline = function (src, env) {
return state.tokens;
};
// Render single string, without wrapping it to paragraphs
//
MarkdownIt.prototype.renderInline = function (src, env) {

32
test/misc.js

@ -128,6 +128,38 @@ describe('API', function () {
assert.strictEqual(md.render('a \\\nb'), '<p>a <br>\nb</p>\n');
});
it('bulk enable/disable rules in different chains', function () {
var md = markdownit();
var was = {
core: md.core.ruler.getRules('').length,
block: md.block.ruler.getRules('').length,
inline: md.inline.ruler.getRules('').length
};
// Disable 2 rule in each chain & compare result
md.disable([ 'block', 'inline', 'code', 'fences', 'emphasis', 'entity' ]);
var now = {
core: md.core.ruler.getRules('').length + 2,
block: md.block.ruler.getRules('').length + 2,
inline: md.inline.ruler.getRules('').length + 2
};
assert.deepEqual(was, now);
// Enable the same rules back
md.enable([ 'block', 'inline', 'code', 'fences', 'emphasis', 'entity' ]);
var back = {
core: md.core.ruler.getRules('').length,
block: md.block.ruler.getRules('').length,
inline: md.inline.ruler.getRules('').length
};
assert.deepEqual(was, back);
});
});

Loading…
Cancel
Save