Sha256: 5113f33789433eaebfa8b7b88376c81bf9521263f5490a56fceda74be136e8c5
Contents?: true
Size: 1.16 KB
Versions: 24
Compression:
Stored size: 1.16 KB
Contents
require 'cgi' module JsDuck # Transforms in-page links so they won't break docs app #!-navigation. # # For example a link to "#automation" in testing guide will be # replaced with "#!/guide/testing-section-automation" and the link # target ID will be transformed into "testing-section-automation". class GuideAnchors def self.transform(html, guide_name) html.gsub(/(<a\s+(?:[^<>]*\s+)?href=['"]#)([^!\/].*?)(['"])/i) do |m| $1 + "!/guide/" + transform_id($2, guide_name) + $3 end.gsub(/(<a\s+(?:[^<>]*\s+)?name=['"])(.*?)(['"])/i) do |m| $1 + transform_id($2, guide_name) + $3 end.gsub(/(<\w+\s+(?:[^<>]*\s+)?id=['"])(.*?)(['"])/i) do |m| $1 + transform_id($2, guide_name) + $3 end end def self.transform_id(id, guide_name) if id =~ /^#{guide_name}-section-/ id else # Escape the ID if it's not already escaped. This check is # needed to avoid re-escaping anchor-links created with # Markdown - these get auto-escaped by RDiscount. id = (id =~ /%[0-9A-F]{2}/) ? id : CGI::escape(id) "#{guide_name}-section-#{id}" end end end end
Version data entries
24 entries across 24 versions & 3 rubygems