[kramdown-users] nofollow to links

Thomas Leitner t_leitner at gmx.at
Sat Dec 17 06:08:33 EST 2011


On 2011-09-18 10:39 +0200 Nicholas Wieland wrote:
> Hi, is it possible to add attributes to links I generate through
> kramdown? The markdown is inserted by users, I can't touch it, but
> maybe I can have some kind of filter that in some cases adds the
> attributes I need to the generated html.

This can be done by customizing the HTML converter. Here is an example
of a custom HTML converter used by [webgen]:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
module Webgen::ContentProcessor

  class KramdownHtmlConverter < ::Kramdown::Converter::Html

    def initialize(root, options, context) #:nodoc:
      super(root, options)
      @context = context
      @do_convert = if context.options.has_key?('contentprocessor.kramdown.handle_links')
                      context.options['contentprocessor.kramdown.handle_links']
                    else
                      context.website.config['contentprocessor.kramdown.handle_links']
                    end
    end

    # Convert the element tree under +root+ to HTML using the webgen
    +context+ object.
    def self.convert(root, options, context)
      new(root, options, context).convert(root)
    end

    def convert_a(el, indent)
      el.attr['href'] = @context.tag('relocatable', {'path' => el.attr['href']}) if @do_convert
      "<a#{html_attributes(el.attr)}>#{inner(el, indent)}</a>"
    end

    def convert_img(el, indent)
      el.attr['src'] = @context.tag('relocatable', {'path' => el.attr['src']}) if @do_convert
      "<img#{html_attributes(el.attr)} />"
    end

  end

end
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

As you can see, the `a` and `img` tag output is customized. You could
do it in a similar way.

Best regards,
  Thomas

[webgen]: http://webgen.rubyforge.org


More information about the kramdown-users mailing list