HtmlRenderer- ability to customize element rendering? (javascript)


#1

I’m using commonmark.js to render markdown on my site. I’d like to change the way a couple tags are generated:

  1. anchor tags (<a/>): I’d like to add target="_blank" based on the href’s value.
  2. headings (h1, h2, etc): I’d like to set the heading’s id attribute based on a convention.

What’s the best way to accomplish this? Is there a way to plugin this logic to commonmark.js or should I directly manipulate the rendered output using jquery/etc?

I’ve seen the documentation here on walking the AST but it doesn’t look like this will allow me to do anything that would have the effect of adding attributes to the rendered HTML.

Related stackoverflow post


#2

I’ve been meaning to make the renderer more customizable by splitting out renderers for individual elements and allowing them to be modified individually. Here’s an issue for this: https://github.com/jgm/commonmark.js/issues/6

At this point you have two options:

  1. Modify the HTML renderer (lib/html.js). Search for case 'Link' and it should be pretty obvious what to do.

  2. Traverse the AST looking for links, and replace these with raw html elements. In practice this is ugly, especially since you’d have to do your own escaping, etc.


Links with attributes like target, class, etc
#3

There seems to be a principle solution at http://stackoverflow.com/questions/4425198/markdown-target-blank like following:

With Markdown-2.5.2, you can use this:
[link](url){:target="_blank"}

Waiting now for support by CommonMark.NET (v0.1.0.0 doesn’t support it)