Skip to content

Added possibility to ignore plain HTML markup - #5

Closed
pvorb wants to merge 5 commits into
markedjs:masterfrom
pvorb:ignoreHtml
Closed

pvorb wants to merge 5 commits into
markedjs:masterfrom
pvorb:ignoreHtml

Conversation

@pvorb

@pvorb pvorb commented Oct 30, 2011

Copy link
Copy Markdown

I added the possibility to ignore HTML markup by applying a second argument to the function call.

var text = 'i am using __markdown__ <span>without HTML support</span>.';
console.log(marked(text, { ignoreHtml: true }));

This will print

i am using <strong>markdown</strong> &lt;span&gt;without HTML support&lt;/span&gt.

You can test the behavior via

node test --ignore-html

This change should not impair marked's performance. If you don't use the second argument, everything remains the same as before. Then there's only one additional condition for each call of the function.

This comes in handy when you want to use markdown for user input on your website as it prevents users from doing XSS.

I hope you find this usable. At least, I do. ;-)

@chjj

chjj commented Oct 30, 2011

Copy link
Copy Markdown
Member

I'm really uneasy about something like this. I would prefer to make marked modifiable and hookable from the outside, as opposed to adding options. That would allow many different extensions and anything the user wanted really. Although your code should work, there's also the performance issue. v8 might be smart enough to optimize those boolean checks out, but it still feels like the wrong way of going about it, which is why I haven't implemented anything like that yet.

Ideally, what I would like for a user to be able to do is something like:

var html = marked.rules.html;
marked.rules.html = function() {
  if (opt.ignoreHtml) return;
  return html.apply(this, arguments);
};

I have 2 different (local) branches where I've implemented this, but it's not quite the way I want it.

I really appreciate the interest though. If you want this kind of capability right now, you can still alter it from the outside, it will just be more hack-like.

@chjj chjj closed this Oct 30, 2011
@pvorb

pvorb commented Oct 30, 2011

Copy link
Copy Markdown
Author

Ok. I think that would be more convenient, but it would also require more overhead. So I'll stick with my fork for the moment until you add such functionality.

@matsko

matsko commented Jul 25, 2013

Copy link
Copy Markdown
Contributor

Hey @pvorb @chjj do you guys have a working example of how to ignore blocks of HTML data?

@pvorb
pvorb deleted the ignoreHtml branch July 26, 2013 08:12
@pvorb
pvorb restored the ignoreHtml branch July 26, 2013 08:12
@pvorb

pvorb commented Jul 26, 2013

Copy link
Copy Markdown
Author

@matsko What do you mean with "blocks of HTML data". Only selected blocks? Comment them out:

Some text.

<!--div>
  <figure><img src="pic.jpg"></figure>
</div-->

More text.

@pvorb

pvorb commented Jul 26, 2013

Copy link
Copy Markdown
Author

@matsko Setting sanitize to true will ignore all HTML input.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants