Skip to content

Commit 504675b

Browse files
authored
Merge pull request #303 from danielparks/fix_markdown_anchors
(#300) Fix anchor links in Markdown docs
2 parents 5fe07ba + b08df58 commit 504675b

File tree

6 files changed

+33
-11
lines changed

6 files changed

+33
-11
lines changed

lib/puppet-strings/markdown/base.rb

+18-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ def see
9595

9696
# @return [Array] parameter tag hashes
9797
def params
98-
select_tags('param')
98+
tags = @tags.select { |tag| tag[:tag_name] == 'param' }.map do |param|
99+
param[:link] = clean_link("$#{name}::#{param[:name]}")
100+
param
101+
end
102+
tags.empty? ? nil : tags
99103
end
100104

101105
# @return [Array] example tag hashes
@@ -151,7 +155,7 @@ def toc_info
151155

152156
# @return [String] makes the component name suitable for a GitHub markdown link
153157
def link
154-
name.delete('::').strip.gsub(' ','-').downcase
158+
clean_link(name)
155159
end
156160

157161
# Some return, default, or valid values need to be in backticks. Instead of fu in the handler or code_object, this just does the change on the front.
@@ -195,5 +199,17 @@ def select_tags(name)
195199
tags = @tags.select { |tag| tag[:tag_name] == name }
196200
tags.empty? ? nil : tags
197201
end
202+
203+
# Convert an input into a string appropriate for an anchor name.
204+
#
205+
# This converts any character not suitable for an id attribute into a '-'. Generally we're running this on Puppet identifiers for types and
206+
# variables, so we only need to worry about the special characters ':' and '$'. With namespaces Puppet variables this should always be produce a
207+
# unique result from a unique input, since ':' only appears in pairs, '$' only appears at the beginning, and '-' never appears.
208+
#
209+
# @param [String] the input to convert
210+
# @return [String] the anchor-safe string
211+
def clean_link(input)
212+
input.tr('^a-zA-Z0-9_-', '-')
213+
end
198214
end
199215
end

lib/puppet-strings/markdown/resource_type.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,19 @@ def checks
2929
def properties_and_checks
3030
return nil if properties.nil? && checks.nil?
3131

32-
((properties || []) + (checks || [])).sort_by { |p| p[:name] }
32+
((properties || []) + (checks || [])).sort_by { |p| p[:name] }.map do |prop|
33+
prop[:link] = clean_link("$#{name}::#{prop[:name]}")
34+
prop
35+
end
3336
end
3437

3538
def parameters
3639
return nil unless @registry[:parameters]
3740

38-
@registry[:parameters].sort_by { |p| p[:name] }
41+
@registry[:parameters].sort_by { |p| p[:name] }.map do |param|
42+
param[:link] = clean_link("$#{name}::#{param[:name]}")
43+
param
44+
end
3945
end
4046

4147
def regex_in_data_type?(data_type)

lib/puppet-strings/markdown/templates/classes_and_defines.erb

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@
5050
The following parameters are available in the `<%= name %>` <%= @type %>:
5151

5252
<% params.each do |param| -%>
53-
* [`<%= param[:name] %>`](#<%= param[:name] %>)
53+
* [`<%= param[:name] %>`](#<%= param[:link] %>)
5454
<% end -%>
5555

5656
<% params.each do |param| -%>
57-
##### <a name="<%= param[:name] %>"></a>`<%= param[:name] %>`
57+
##### <a name="<%= param[:link] %>"></a>`<%= param[:name] %>`
5858

5959
<% if param[:types] -%>
6060
Data type: `<%= param[:types].join(', ') -%>`

lib/puppet-strings/markdown/templates/data_type.erb

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ Alias of
5858
The following parameters are available in the `<%= name %>` <%= @type %>:
5959

6060
<% params.each do |param| -%>
61-
* [`<%= param[:name] %>`](#<%= param[:name] %>)
61+
* [`<%= param[:name] %>`](#<%= param[:link] %>)
6262
<% end -%>
6363

6464
<% params.each do |param| -%>
65-
##### <a name="<%= param[:name] %>"></a>`<%= param[:name] %>`
65+
##### <a name="<%= param[:link] %>"></a>`<%= param[:name] %>`
6666

6767
<% if param[:types] -%>
6868
Data type: `<%= param[:types].join(', ') -%>`

lib/puppet-strings/markdown/templates/resource_type.erb

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ Default value: `<%= prop[:default] %>`
9898
The following parameters are available in the `<%= name %>` <%= @type %>.
9999

100100
<% parameters.each do |param| -%>
101-
* [`<%= param[:name] %>`](#<%= param[:name] %>)
101+
* [`<%= param[:name] %>`](#<%= param[:link] %>)
102102
<% end -%>
103103

104104
<% parameters.each do |param| -%>
105-
##### <a name="<%= param[:name] %>"></a>`<%= param[:name] %>`
105+
##### <a name="<%= param[:link] %>"></a>`<%= param[:name] %>`
106106

107107
<% if param[:values] -%>
108108
Valid values: `<%= param[:values].map { |value| value_string(value) }.join('`, `') %>`

spec/unit/puppet-strings/markdown/base_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class klass::yeah(
141141

142142
describe '#link' do
143143
it 'returns a valid link' do
144-
expect(component.link).to eq 'klassyeah'
144+
expect(component.link).to eq 'klass--yeah'
145145
end
146146
end
147147
end

0 commit comments

Comments
 (0)