Skip to content

Commit d8437b4

Browse files
Fix generation of docs that was broken due to HTML safety issues (#2265)
* Fix generation of docs that was broken due to HTML safety issues * add tests for docs generation * is this build order an issue? * run docs test first --------- Co-authored-by: Joel Hawksley <joelhawksley@github.com>
1 parent 35ae4fa commit d8437b4

File tree

5 files changed

+36
-9
lines changed

5 files changed

+36
-9
lines changed

Rakefile

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ Rake::TestTask.new(:engine_test) do |t|
1717
t.test_files = FileList["test/test_engine/**/*_test.rb"]
1818
end
1919

20+
Rake::TestTask.new(:docs_test) do |t|
21+
t.libs << "test"
22+
t.libs << "lib"
23+
t.test_files = FileList["test/docs/*_test.rb"]
24+
end
25+
2026
begin
2127
require "rspec/core/rake_task"
2228
RSpec::Core::RakeTask.new(:spec)
@@ -100,7 +106,7 @@ namespace :docs do
100106

101107
error_keys = registry.keys.select { |key| key.to_s.include?("Error::MESSAGE") }.map(&:to_s)
102108

103-
docs = ActionController::Base.new.render_to_string(
109+
docs = ActionController::Base.renderer.render(
104110
ViewComponent::DocsBuilderComponent.new(
105111
sections: [
106112
ViewComponent::DocsBuilderComponent::Section.new(
@@ -128,10 +134,12 @@ namespace :docs do
128134
)
129135
).chomp
130136

131-
File.open("docs/api.md", "w") do |f|
132-
f.puts(docs)
137+
if ENV["RAILS_ENV"] != "test"
138+
File.open("docs/api.md", "w") do |f|
139+
f.puts(docs)
140+
end
133141
end
134142
end
135143
end
136144

137-
task default: [:test, :engine_test, :spec]
145+
task default: [:docs_test, :test, :engine_test, :spec]

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ nav_order: 6
1010

1111
## main
1212

13+
* Fix generation of ViewComponent documentation that was broken due to HTML safety issues.
14+
15+
*Simon Fish*
16+
1317
* Add documentation on how ViewComponent works.
1418

1519
*Joel Hawksley*

lib/view_component/docs_builder_component.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ nav_order: 3
1212
## <%= section.heading %>
1313

1414
<% section.methods.each do |method| %>
15-
### <%== render ViewComponent::DocsBuilderComponent::MethodDoc.new(method, section.show_types) %>
15+
### <%= render ViewComponent::DocsBuilderComponent::MethodDoc.new(method, section.show_types) %>
1616

1717
<% end %>
1818
<% section.error_klasses.each do |error_klass| %>
19-
### <%== render ViewComponent::DocsBuilderComponent::ErrorKlassDoc.new(error_klass, section.show_types) %>
19+
### <%= render ViewComponent::DocsBuilderComponent::ErrorKlassDoc.new(error_klass, section.show_types) %>
2020

2121
<% end %>
2222
<% end %>

lib/view_component/docs_builder_component.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def error_message
2424
end
2525

2626
def call
27-
<<~DOCS.chomp
27+
<<~DOCS.chomp.html_safe
2828
`#{klass_name}`
2929
3030
#{error_message}
@@ -67,15 +67,15 @@ def deprecation_text
6767
end
6868

6969
def docstring_and_deprecation_text
70-
<<~DOCS.strip
70+
<<~DOCS.strip.html_safe
7171
#{docstring}
7272
7373
#{"_#{deprecation_text}_" if deprecated?}
7474
DOCS
7575
end
7676

7777
def call
78-
<<~DOCS.chomp
78+
<<~DOCS.chomp.html_safe
7979
`#{separator}#{signature_or_name}`#{types}#{suffix}
8080
8181
#{docstring_and_deprecation_text}

test/docs/docs_rake_task_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
class DocsRakeTaskTest < ActiveSupport::TestCase
6+
def test_rake_task
7+
load "Rakefile"
8+
9+
assert_nothing_raised do
10+
Rake::Task["docs:build"].invoke
11+
end
12+
13+
Rake::Task.clear
14+
end
15+
end

0 commit comments

Comments
 (0)