Showdown: Hooking into converter.makeHtml() to customize HTML output

Created on 2 Nov 2016  路  3Comments  路  Source: showdownjs/showdown

Dear everyone

I would like to add some extra attributes or some value into HTML output

Example Markdown

![](img01.png)

Expected HTML Output

<img src="/path/to/images/img01.png>

Where/how can I hook into the conversion process from markdown to HTML?, or must I do this after the HTML output?

Thanks everyone

help wanted

Most helpful comment

what kind of attributes do want to add? some classes?

you could make an extension

showdown.extension('myext', function () {
  return [{
    type: "output",
    filter: function (html, converter, options) {
      //parse the html string
      var liveHtml = document.createElement('div');
      liveHtml.innerHTML = html;
      var images = liveHtml.getElementsByTagName('img');
      var image;
      for(var i = 0; i < images.length; i++) {
        image = images[i];
        image.classList.add('some-css-class');
      }
      return liveHtml.innerHTML;
    }
  }];
});

live fiddle: https://jsfiddle.net/n122569s/

UPDATE:
if you want for the src of the images to not fail with 404 when you parse the html string, you can try using jQuery

showdown.extension('myext', function () {
  return [{
    type: "output",
    filter: function (html, converter, options) {
      //parse the html string
      var liveHtml = $('<div></div>').html(html);
      //var liveHtml = html;
      $('img', liveHtml).each(function(){ 
        var image = $(this);
        image.addClass('some-css-class');
        // or even prepend the src
        image.attr('src', function(index, src) {
          return 'https://obedm503.github.io/bootmark/' + src;
        });
      });
      return liveHtml.html();
    }
  }];
});

fiddle: https://jsfiddle.net/f43s2w9x/

All 3 comments

what kind of attributes do want to add? some classes?

you could make an extension

showdown.extension('myext', function () {
  return [{
    type: "output",
    filter: function (html, converter, options) {
      //parse the html string
      var liveHtml = document.createElement('div');
      liveHtml.innerHTML = html;
      var images = liveHtml.getElementsByTagName('img');
      var image;
      for(var i = 0; i < images.length; i++) {
        image = images[i];
        image.classList.add('some-css-class');
      }
      return liveHtml.innerHTML;
    }
  }];
});

live fiddle: https://jsfiddle.net/n122569s/

UPDATE:
if you want for the src of the images to not fail with 404 when you parse the html string, you can try using jQuery

showdown.extension('myext', function () {
  return [{
    type: "output",
    filter: function (html, converter, options) {
      //parse the html string
      var liveHtml = $('<div></div>').html(html);
      //var liveHtml = html;
      $('img', liveHtml).each(function(){ 
        var image = $(this);
        image.addClass('some-css-class');
        // or even prepend the src
        image.attr('src', function(index, src) {
          return 'https://obedm503.github.io/bootmark/' + src;
        });
      });
      return liveHtml.html();
    }
  }];
});

fiddle: https://jsfiddle.net/f43s2w9x/

ALso check the documentation regarding extensions https://github.com/showdownjs/showdown/wiki/Extensions

Thanks @obedm503 , @tivie, I've got some ideas what to do next :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

reisraff picture reisraff  路  5Comments

buremba picture buremba  路  4Comments

JerryYangJin picture JerryYangJin  路  7Comments

DesignResponds picture DesignResponds  路  3Comments

savsharma2 picture savsharma2  路  7Comments