Skip to content

(#300) Fix anchor links in Markdown docs #303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions lib/puppet-strings/markdown/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ def see

# @return [Array] parameter tag hashes
def params
select_tags('param')
tags = @tags.select { |tag| tag[:tag_name] == 'param' }.map do |param|
param[:link] = clean_link("$#{name}::#{param[:name]}")
param
end
tags.empty? ? nil : tags
end

# @return [Array] example tag hashes
Expand Down Expand Up @@ -151,7 +155,7 @@ def toc_info

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

# 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.
Expand Down Expand Up @@ -195,5 +199,17 @@ def select_tags(name)
tags = @tags.select { |tag| tag[:tag_name] == name }
tags.empty? ? nil : tags
end

# Convert an input into a string appropriate for an anchor name.
#
# This converts any character not suitable for an id attribute into a '-'. Generally we're running this on Puppet identifiers for types and
# variables, so we only need to worry about the special characters ':' and '$'. With namespaces Puppet variables this should always be produce a
# unique result from a unique input, since ':' only appears in pairs, '$' only appears at the beginning, and '-' never appears.
#
# @param [String] the input to convert
# @return [String] the anchor-safe string
def clean_link(input)
input.tr('^a-zA-Z0-9_-', '-')
end
end
end
10 changes: 8 additions & 2 deletions lib/puppet-strings/markdown/resource_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@ def checks
def properties_and_checks
return nil if properties.nil? && checks.nil?

((properties || []) + (checks || [])).sort_by { |p| p[:name] }
((properties || []) + (checks || [])).sort_by { |p| p[:name] }.map do |prop|
prop[:link] = clean_link("$#{name}::#{prop[:name]}")
prop
end
end

def parameters
return nil unless @registry[:parameters]

@registry[:parameters].sort_by { |p| p[:name] }
@registry[:parameters].sort_by { |p| p[:name] }.map do |param|
param[:link] = clean_link("$#{name}::#{param[:name]}")
param
end
end

def regex_in_data_type?(data_type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@
The following parameters are available in the `<%= name %>` <%= @type %>:

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

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

<% if param[:types] -%>
Data type: `<%= param[:types].join(', ') -%>`
Expand Down
4 changes: 2 additions & 2 deletions lib/puppet-strings/markdown/templates/data_type.erb
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ Alias of
The following parameters are available in the `<%= name %>` <%= @type %>:

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

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

<% if param[:types] -%>
Data type: `<%= param[:types].join(', ') -%>`
Expand Down
4 changes: 2 additions & 2 deletions lib/puppet-strings/markdown/templates/resource_type.erb
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ Default value: `<%= prop[:default] %>`
The following parameters are available in the `<%= name %>` <%= @type %>.

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

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

<% if param[:values] -%>
Valid values: `<%= param[:values].map { |value| value_string(value) }.join('`, `') %>`
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/puppet-strings/markdown/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class klass::yeah(

describe '#link' do
it 'returns a valid link' do
expect(component.link).to eq 'klassyeah'
expect(component.link).to eq 'klass--yeah'
end
end
end
Expand Down