Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow disabling of specific block rules #248

Closed
jaredly opened this issue Apr 17, 2017 · 1 comment
Closed

Allow disabling of specific block rules #248

jaredly opened this issue Apr 17, 2017 · 1 comment

Comments

@jaredly
Copy link

jaredly commented Apr 17, 2017

It's awesome that remark-parser is built in a modular fashion, but there doesn't seem to be a way to take advantage of that modularity.
I'd love to be able to say "parse this string w/o lists or headings", in the way that I can with remarkable see example

@wooorm
Copy link
Member

wooorm commented Apr 22, 2017

Hi Jared! 👋
Sorry for taking a while to get back to you.
You’re correct in that that’s not afancy enable/disable function, but, “there doesn't seem to be a way to take advantage of that modularity” isn’t true. It’s possible to create a plugin for this!

Below is a working plugin, if you wish you can publish it, or just drop it into your own code:

var remark = require('remark');
var inspect = require('unist-util-inspect');

var processor = remark().use(disable, {block: ['list', 'atxHeading', 'setextHeading']});

console.log(inspect(processor.parse('# Heading.\n* List.\n> Blockquote')));

function disable(options) {
  var proto = this.Parser.prototype;
  console.log('block: ', proto.blockMethods);
  console.log('inline: ', proto.inlineMethods);
  ignore('blockMethods', options.block);
  ignore('inlineMethods', options.inline);

  function ignore(key, list) {
    var values = proto[key];
    var index = -1;
    var length = list && list.length;
    var pos;
    while (++index < length) {
      pos = values.indexOf(list[index]);
      if (pos !== -1) {
        values.splice(pos, 1);
      }
    }
  }
}

Yields:

block:  [ 'yamlFrontMatter',
  'newline',
  'indentedCode',
  'fencedCode',
  'blockquote',
  'atxHeading',
  'thematicBreak',
  'list',
  'setextHeading',
  'html',
  'footnote',
  'definition',
  'table',
  'paragraph' ]
inline:  [ 'escape',
  'autoLink',
  'url',
  'html',
  'link',
  'reference',
  'strong',
  'emphasis',
  'deletion',
  'code',
  'break',
  'text' ]
root[3] (1:1-3:13, 0-31)
├─ paragraph[1] (1:1-1:11, 0-10)
│  └─ text: "# Heading." (1:1-1:11, 0-10)
├─ paragraph[1] (2:1-2:8, 11-18)
│  └─ text: "* List." (2:1-2:8, 11-18)
└─ blockquote[1] (3:1-3:13, 19-31)
   └─ paragraph[1] (3:3-3:13, 21-31)
      └─ text: "Blockquote" (3:3-3:13, 21-31)

Cheers! Let me know if you have further questions :) Oh and the Gitter channel is often more responsive than I am on issues :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants