class JsDuck::GuideAnchors
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”.
Public Class Methods
transform(html, guide_name)
click to toggle source
# File lib/jsduck/guide_anchors.rb, line 12 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
transform_id(id, guide_name)
click to toggle source
# File lib/jsduck/guide_anchors.rb, line 24 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