Created
January 20, 2022 21:11
-
-
Save robmiller/4c73578023242e13ea8b76fe8ca11cad to your computer and use it in GitHub Desktop.
Add automatically generated unique IDs to block elements (p, ul, blockquote, etc.) generated by kramdown, which allows you to link to any paragraph on a page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Kramdown::Converter::Html | |
def add_block_count(attr) | |
@block_count ||= 0 | |
@block_count += 1 | |
unless attr["id"] | |
attr["id"] = "b:#{@block_count}" | |
end | |
end | |
%i(format_as_block_html format_as_indented_block_html).each do |method| | |
alias_method :"original_#{method}", method | |
define_method(method) do |*args| | |
add_block_count(args[1]) | |
send(:"original_#{method}", *args) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment