Skip to content

Instantly share code, notes, and snippets.

@robmiller
Created January 20, 2022 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robmiller/4c73578023242e13ea8b76fe8ca11cad to your computer and use it in GitHub Desktop.
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
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