Sha256: 95b319b443f72fedaf98b024de9c14d7f625e547d1892aa5ba8eacde47a7b9a4

Contents?: true

Size: 914 Bytes

Versions: 7

Compression:

Stored size: 914 Bytes

Contents

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/#{guide_name}-section-#{$2}#{$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
        "#{guide_name}-section-#{id}"
      end
    end

  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
jsduck-5.0.0 lib/jsduck/guide_anchors.rb
jsduck-5.0.0.beta5 lib/jsduck/guide_anchors.rb
jsduck-5.0.0.beta4 lib/jsduck/guide_anchors.rb
jsduck-4.10.4 lib/jsduck/guide_anchors.rb
jsduck-5.0.0.beta3 lib/jsduck/guide_anchors.rb
jsduck-4.10.3 lib/jsduck/guide_anchors.rb
jsduck-4.10.2 lib/jsduck/guide_anchors.rb