Skip to content

Commit 549144b

Browse files
authored
Merge pull request #51702 from akhilgkrishnan/fix-guides-broken-link
Add rake guides:lint task to raise error on broken anchor links
2 parents f84e1eb + 61090b1 commit 549144b

File tree

3 files changed

+49
-14
lines changed

3 files changed

+49
-14
lines changed

guides/Rakefile

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,23 @@ namespace :guides do
2323
end
2424
end
2525

26-
desc "Lint guides, using `mdl`"
27-
task :lint do
28-
require "mdl"
29-
all = Dir.glob("#{__dir__}/source/*.md")
30-
files = all - Dir.glob("#{__dir__}/**/*_release_notes.md") # Ignore release notes
31-
MarkdownLint.run files
26+
desc "Lint guides, using `mdl` and check for broken links"
27+
task lint: ["lint:check_links", "lint:mdl"]
28+
29+
namespace :lint do
30+
desc "Check links in generated HTML guides"
31+
task :check_links do
32+
ENV["GUIDES_LINT"] = "1"
33+
ruby "-Eutf-8:utf-8", "rails_guides.rb"
34+
end
35+
36+
desc "Run mdl to check Markdown files for style guide violations and lint errors"
37+
task :mdl do
38+
require "mdl"
39+
all = Dir.glob("#{__dir__}/source/*.md")
40+
files = all - Dir.glob("#{__dir__}/**/*_release_notes.md") # Ignore release notes
41+
MarkdownLint.run files
42+
end
3243
end
3344

3445
# Validate guides -------------------------------------------------------------------------

guides/rails_guides.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@
2626
only: env_value["ONLY"],
2727
epub: env_flag["EPUB"],
2828
language: env_value["GUIDES_LANGUAGE"],
29-
direction: env_value["DIRECTION"]
29+
direction: env_value["DIRECTION"],
30+
lint: env_flag["GUIDES_LINT"]
3031
).generate

guides/rails_guides/generator.rb

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,52 @@
1313
require "rails_guides/markdown"
1414
require "rails_guides/helpers"
1515
require "rails_guides/epub"
16+
require "debug"
1617

1718
module RailsGuides
1819
class Generator
1920
GUIDES_RE = /\.(?:erb|md)\z/
2021

21-
def initialize(edge:, version:, all:, only:, epub:, language:, direction: nil)
22+
def initialize(edge:, version:, all:, only:, epub:, language:, direction: nil, lint:)
2223
@edge = edge
2324
@version = version
2425
@all = all
2526
@only = only
2627
@epub = epub
2728
@language = language
2829
@direction = direction || "ltr"
30+
@lint = lint
31+
@warnings = []
2932

3033
if @epub
3134
register_special_mime_types
3235
end
3336

3437
initialize_dirs
35-
create_output_dir_if_needed
38+
create_output_dir_if_needed if !dry_run?
3639
initialize_markdown_renderer
3740
end
3841

3942
def generate
4043
generate_guides
41-
process_scss
42-
copy_assets
43-
generate_epub if @epub
44+
45+
if @lint && @warnings.any?
46+
puts "#{@warnings.join("\n")}"
47+
exit 1
48+
end
49+
50+
if !dry_run?
51+
process_scss
52+
copy_assets
53+
generate_epub if @epub
54+
end
4455
end
4556

4657
private
58+
def dry_run?
59+
[@lint].any?
60+
end
61+
4762
def register_special_mime_types
4863
Mime::Type.register_alias("application/xml", :opf, %w(opf))
4964
Mime::Type.register_alias("application/xml", :ncx, %w(ncx))
@@ -168,12 +183,15 @@ def generate_guide(guide, output_file)
168183
epub: @epub
169184
).render(body)
170185

171-
warn_about_broken_links(result)
186+
broken = warn_about_broken_links(result)
187+
if broken.any?
188+
@warnings << "[WARN] BROKEN LINK(s): #{guide}: #{broken.join(", ")}"
189+
end
172190
end
173191

174192
File.open(output_path, "w") do |f|
175193
f.write(result)
176-
end
194+
end if !dry_run?
177195
end
178196

179197
def warn_about_broken_links(html)
@@ -199,13 +217,18 @@ def extract_anchors(html)
199217
end
200218

201219
def check_fragment_identifiers(html, anchors)
220+
broken_links = []
221+
202222
html.scan(/<a\s+href="#([^"]+)/).flatten.each do |fragment_identifier|
203223
next if fragment_identifier == "mainCol" # in layout, jumps to some DIV
204224
unless anchors.member?(CGI.unescape(fragment_identifier))
205225
guess = DidYouMean::SpellChecker.new(dictionary: anchors).correct(fragment_identifier).first
206226
puts "*** BROKEN LINK: ##{fragment_identifier}, perhaps you meant ##{guess}."
227+
broken_links << "##{fragment_identifier}"
207228
end
208229
end
230+
231+
broken_links
209232
end
210233
end
211234
end

0 commit comments

Comments
 (0)