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
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
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" /}
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.