Add rake guides:lint task to raise error on broken anchor links
Co-authored-by: zzak <zzakscott@gmail.com>
This commit is contained in:
parent
325c04c1af
commit
61090b1f3f
@ -23,12 +23,23 @@ namespace :guides do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "Lint guides, using `mdl`"
|
desc "Lint guides, using `mdl` and check for broken links"
|
||||||
task :lint do
|
task lint: ["lint:check_links", "lint:mdl"]
|
||||||
require "mdl"
|
|
||||||
all = Dir.glob("#{__dir__}/source/*.md")
|
namespace :lint do
|
||||||
files = all - Dir.glob("#{__dir__}/**/*_release_notes.md") # Ignore release notes
|
desc "Check links in generated HTML guides"
|
||||||
MarkdownLint.run files
|
task :check_links do
|
||||||
|
ENV["GUIDES_LINT"] = "1"
|
||||||
|
ruby "-Eutf-8:utf-8", "rails_guides.rb"
|
||||||
|
end
|
||||||
|
|
||||||
|
desc "Run mdl to check Markdown files for style guide violations and lint errors"
|
||||||
|
task :mdl do
|
||||||
|
require "mdl"
|
||||||
|
all = Dir.glob("#{__dir__}/source/*.md")
|
||||||
|
files = all - Dir.glob("#{__dir__}/**/*_release_notes.md") # Ignore release notes
|
||||||
|
MarkdownLint.run files
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Validate guides -------------------------------------------------------------------------
|
# Validate guides -------------------------------------------------------------------------
|
||||||
|
@ -26,5 +26,6 @@
|
|||||||
only: env_value["ONLY"],
|
only: env_value["ONLY"],
|
||||||
epub: env_flag["EPUB"],
|
epub: env_flag["EPUB"],
|
||||||
language: env_value["GUIDES_LANGUAGE"],
|
language: env_value["GUIDES_LANGUAGE"],
|
||||||
direction: env_value["DIRECTION"]
|
direction: env_value["DIRECTION"],
|
||||||
|
lint: env_flag["GUIDES_LINT"]
|
||||||
).generate
|
).generate
|
||||||
|
@ -13,12 +13,13 @@
|
|||||||
require "rails_guides/markdown"
|
require "rails_guides/markdown"
|
||||||
require "rails_guides/helpers"
|
require "rails_guides/helpers"
|
||||||
require "rails_guides/epub"
|
require "rails_guides/epub"
|
||||||
|
require "debug"
|
||||||
|
|
||||||
module RailsGuides
|
module RailsGuides
|
||||||
class Generator
|
class Generator
|
||||||
GUIDES_RE = /\.(?:erb|md)\z/
|
GUIDES_RE = /\.(?:erb|md)\z/
|
||||||
|
|
||||||
def initialize(edge:, version:, all:, only:, epub:, language:, direction: nil)
|
def initialize(edge:, version:, all:, only:, epub:, language:, direction: nil, lint:)
|
||||||
@edge = edge
|
@edge = edge
|
||||||
@version = version
|
@version = version
|
||||||
@all = all
|
@all = all
|
||||||
@ -26,24 +27,38 @@ def initialize(edge:, version:, all:, only:, epub:, language:, direction: nil)
|
|||||||
@epub = epub
|
@epub = epub
|
||||||
@language = language
|
@language = language
|
||||||
@direction = direction || "ltr"
|
@direction = direction || "ltr"
|
||||||
|
@lint = lint
|
||||||
|
@warnings = []
|
||||||
|
|
||||||
if @epub
|
if @epub
|
||||||
register_special_mime_types
|
register_special_mime_types
|
||||||
end
|
end
|
||||||
|
|
||||||
initialize_dirs
|
initialize_dirs
|
||||||
create_output_dir_if_needed
|
create_output_dir_if_needed if !dry_run?
|
||||||
initialize_markdown_renderer
|
initialize_markdown_renderer
|
||||||
end
|
end
|
||||||
|
|
||||||
def generate
|
def generate
|
||||||
generate_guides
|
generate_guides
|
||||||
process_scss
|
|
||||||
copy_assets
|
if @lint && @warnings.any?
|
||||||
generate_epub if @epub
|
puts "#{@warnings.join("\n")}"
|
||||||
|
exit 1
|
||||||
|
end
|
||||||
|
|
||||||
|
if !dry_run?
|
||||||
|
process_scss
|
||||||
|
copy_assets
|
||||||
|
generate_epub if @epub
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
def dry_run?
|
||||||
|
[@lint].any?
|
||||||
|
end
|
||||||
|
|
||||||
def register_special_mime_types
|
def register_special_mime_types
|
||||||
Mime::Type.register_alias("application/xml", :opf, %w(opf))
|
Mime::Type.register_alias("application/xml", :opf, %w(opf))
|
||||||
Mime::Type.register_alias("application/xml", :ncx, %w(ncx))
|
Mime::Type.register_alias("application/xml", :ncx, %w(ncx))
|
||||||
@ -168,12 +183,15 @@ def generate_guide(guide, output_file)
|
|||||||
epub: @epub
|
epub: @epub
|
||||||
).render(body)
|
).render(body)
|
||||||
|
|
||||||
warn_about_broken_links(result)
|
broken = warn_about_broken_links(result)
|
||||||
|
if broken.any?
|
||||||
|
@warnings << "[WARN] BROKEN LINK(s): #{guide}: #{broken.join(", ")}"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
File.open(output_path, "w") do |f|
|
File.open(output_path, "w") do |f|
|
||||||
f.write(result)
|
f.write(result)
|
||||||
end
|
end if !dry_run?
|
||||||
end
|
end
|
||||||
|
|
||||||
def warn_about_broken_links(html)
|
def warn_about_broken_links(html)
|
||||||
@ -199,13 +217,18 @@ def extract_anchors(html)
|
|||||||
end
|
end
|
||||||
|
|
||||||
def check_fragment_identifiers(html, anchors)
|
def check_fragment_identifiers(html, anchors)
|
||||||
|
broken_links = []
|
||||||
|
|
||||||
html.scan(/<a\s+href="#([^"]+)/).flatten.each do |fragment_identifier|
|
html.scan(/<a\s+href="#([^"]+)/).flatten.each do |fragment_identifier|
|
||||||
next if fragment_identifier == "mainCol" # in layout, jumps to some DIV
|
next if fragment_identifier == "mainCol" # in layout, jumps to some DIV
|
||||||
unless anchors.member?(CGI.unescape(fragment_identifier))
|
unless anchors.member?(CGI.unescape(fragment_identifier))
|
||||||
guess = DidYouMean::SpellChecker.new(dictionary: anchors).correct(fragment_identifier).first
|
guess = DidYouMean::SpellChecker.new(dictionary: anchors).correct(fragment_identifier).first
|
||||||
puts "*** BROKEN LINK: ##{fragment_identifier}, perhaps you meant ##{guess}."
|
puts "*** BROKEN LINK: ##{fragment_identifier}, perhaps you meant ##{guess}."
|
||||||
|
broken_links << "##{fragment_identifier}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
broken_links
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user