docbrown/app/lib/redcarpet/render/html_rouge.rb
Gabriel Lebec 1e2749be24 feat(markdown): add ids to headers (#713)
* feat(markdown): add anchors for headers

* Fix markdown anchors impl and spec

* Fixes tests for markdown anchors

* Use name for anchors instead of IDs

* Fix approval file

* Remove emojis in links and hyphenate only spaces
2018-11-20 18:17:07 -05:00

37 lines
1 KiB
Ruby

require "rouge/plugins/redcarpet"
module Redcarpet
module Render
class HTMLRouge < HTML
include Rouge::Plugins::Redcarpet
def link(link, _title, content)
# Probably not the best fix but it does it's job of preventing
# a nested links.
return nil if /<a\s.+\/a>/.match?(content)
link_attributes = ""
@options[:link_attributes]&.each do |attribute, value|
link_attributes += %( #{attribute}="#{value}")
end
%(<a href="#{link}"#{link_attributes}>#{content}</a>)
end
def header(title, header_number)
anchor_link = remove_emoji_and_hyphenate(title)
<<~HEREDOC
<h#{header_number}>
<a name="#{anchor_link}" href="##{anchor_link}" class="anchor">
</a>
#{title}
</h#{header_number}>
HEREDOC
end
private
def remove_emoji_and_hyphenate(string)
string.downcase.gsub(EmojiRegex::Regex, "").strip.gsub(/\s/, "-")
end
end
end
end