0

Is it possible to limit kramdown automatic generation of header IDs up to, i.e., ?

More information in docs: https://kramdown.gettalong.org/converter/html.html#auto-ids

curious
  • 791
  • 2
  • 12
  • 27

2 Answers2

1

To limit kramdown automatic generation of header IDs up to h4, you have two options:

Write you own id in those headers greater than 4:

##### I have a custom id
{: #my_custom_id}

Disable the auto id generation for those headers:

##### I don't have id!
{::options auto_ids="false" /}
marcanuy
  • 23,118
  • 9
  • 64
  • 113
  • I see, unfortunately, those solutions don't fit my needs. Hope that developers implement better one some day. – curious May 19 '17 at 18:33
0

There's no built-in functionality for that option, but you can monkey-patch. The source of the conversion method is here: https://github.com/gettalong/kramdown/blob/master/lib/kramdown/converter/html.rb#L125

So you can do something like this:

module Kramdown
  module Converter
    class Html
      def convert_header(el, indent)
        attr = el.attr.dup
        level = output_header_level(el.options[:level])
        if @options[:auto_ids] && !attr['id'] && (level <= 4)
          attr['id'] = generate_id(el.options[:raw_text])
        end
        @toc << [el.options[:level], attr['id'], el.children] if attr['id'] && in_toc?(el)
        format_as_block_html("h#{level}", attr, inner(el, indent), indent)
      end
    end
  end
end

The addition of the && (level <= 4) makes it behave the way you desire.

Mark Thomas
  • 37,131
  • 11
  • 74
  • 101