Showdown: Automatic anchor link insertion

Created on 9 Apr 2017  路  1Comment  路  Source: showdownjs/showdown

Hi!

Is there an option to automatically add anchor link inside <h> tags just like it is done on documentation sites? If there is no such an option how can I do it with an extension?

For example this:

(new showdown.Converter()).makeHtml(' # Some header ');

Would produce this:

<h1 id="someheader">Some header <a href="#someheader">link</a></h1>

Or this:

<h1 id="someheader"><a href="#someheader">Some header link</a></h1>

I guess it would be a good feature to have.

help wanted

Most helpful comment

You can use an output extension to accomplish this (see fiidle)

showdown.extension('headerlink', function() {
  return [{
    type: 'output',
    regex: /(<h(\d) id="(.+)">(.+?))(<\/h\2>)/g,
    replace: function (wm, g1, g2, id, g4, g5) {
    return g1 + ' <a href="#' + id + '">link</a>' + g5;
    }
  }];
});

>All comments

You can use an output extension to accomplish this (see fiidle)

showdown.extension('headerlink', function() {
  return [{
    type: 'output',
    regex: /(<h(\d) id="(.+)">(.+?))(<\/h\2>)/g,
    replace: function (wm, g1, g2, id, g4, g5) {
    return g1 + ' <a href="#' + id + '">link</a>' + g5;
    }
  }];
});
Was this page helpful?
0 / 5 - 0 ratings

Related issues

JerryYangJin picture JerryYangJin  路  7Comments

ifeltsweet picture ifeltsweet  路  6Comments

LeahPike picture LeahPike  路  5Comments

andistuder picture andistuder  路  5Comments

jonaskello picture jonaskello  路  4Comments