HtmlRenderer- ability to customize element rendering? (javascript)

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

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:

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.

There seems to be a principle solution at html - Can I create links with 'target="_blank"' in Markdown? - Stack Overflow like following:

With Markdown-2.5.2, you can use this:

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