Custom markdown renderer

The options of marked module has a renderer property of object type. The object contains functions to render markdown tokens to HTML. The renderer option allows to render the tokens in a custom manner. Here is an example of overriding the default link token to render by adding a target attribute to the anchor tag:

// marked-renderer.js
'use strict';

function markedRenderer( marked ) {

  // Get the original renderer.
  var renderer = new marked.Renderer();

  /**
   * Overwrite link rendering to provide target attribute.
   * The title argument can contain the target value separated by a vertical bar.
   * Examples:
   *    * "title" - title contains the title only
   *    * "title | _blank" - title contains both title and target
   *    * "|_blank" - title contains the target only
   *    * "|" - default target is "_blank"
   */
  renderer.link = function (href, title, text) {
    var target = null;

    var pos = (title || '').indexOf( '|' );
    if (pos >= 0) {
      target = title.substring( pos + 1).trim() || '_blank';
      title = title.substring( 0, pos).trim();
    }

    return  '<a href="' + href + '"' +
      (target ? '" target="' + target + '"' : '') +
      (title ? '" title="' + title + '"' : '') +
      '>' + text + '</a>';
  };

  // Returns the customized marked renderer.
  return renderer;
}

module.exports = markedRenderer;

The path of the custom renderer can be set in the configuration.

The renderer object has the following functions:

Block level renderer methods Inline level renderer methods
● code( string code, string language )
● blockquote( string quote )
● html( string html )
● heading( string text, number level )
● hr()
● list( string body, boolean ordered )
● listitem( string text )
● paragraph( string text )
● table( string header, string body )
● tablerow( string content )
● tablecell( string content, object flags )
● strong( string text )
● em( string text )
● codespan( string code )
● br()
● del( string text )
● link( string href, string title, string text )
● image( string href, string title, string text )

For more information see the description of the marked module.