diff --git a/README.md b/README.md index 6fd6fa20..00e20797 100644 --- a/README.md +++ b/README.md @@ -297,6 +297,7 @@ The `HTMLProofer` constructor takes an optional hash of additional options: | `internal_domains`| An array of Strings containing domains that will be treated as internal urls. | `[]` | | `log_level` | Sets the logging level, as determined by [Yell](https://github.com/rudionrails/yell). One of `:debug`, `:info`, `:warn`, `:error`, or `:fatal`. | `:info` | `only_4xx` | Only reports errors for links that fall within the 4xx status code range. | `false` | +| `root_dir` | The absolute path to the directory serving your html-files. Used when running html-proofer on a file, rather than a directory. | "" | | `typhoeus_config` | A JSON-formatted string. Parsed using `JSON.parse` and mapped on top of the default configuration values so that they can be overridden. | `{}` | | `url_ignore` | An array of Strings or RegExps containing URLs that are safe to ignore. It affects all HTML attributes. Note that non-HTTP(S) URIs are always ignored. | `[]` | | `url_swap` | A hash containing key-value pairs of `RegExp => String`. It transforms URLs that match `RegExp` into `String` via `gsub`. | `{}` | diff --git a/bin/htmlproofer b/bin/htmlproofer index 51f3e95e..eed31fbc 100755 --- a/bin/htmlproofer +++ b/bin/htmlproofer @@ -47,6 +47,7 @@ Mercenary.program(:htmlproofer) do |p| p.option 'typhoeus_config', '--typhoeus-config CONFIG', String, 'JSON-formatted string of Typhoeus config. Will override the html-proofer defaults.' p.option 'url_ignore', '--url-ignore link1,[link2,...]', Array, 'A comma-separated list of Strings or RegExps containing URLs that are safe to ignore. It affects all HTML attributes. Note that non-HTTP(S) URIs are always ignored' p.option 'url_swap', '--url-swap re:string,[re:string,...]', Array, 'A comma-separated list containing key-value pairs of `RegExp => String`. It transforms URLs that match `RegExp` into `String` via `gsub`. The escape sequences `\\:` should be used to produce literal `:`s.' + p.option 'root_dir', '--root-folder PATH', String, 'The absolute path to the directory serving your html-files. Used when running html-proofer on a file, rather than a directory.' p.action do |args, opts| args = ['.'] if args.empty? diff --git a/lib/html-proofer/element.rb b/lib/html-proofer/element.rb index cf93a6b1..d0f88b8d 100644 --- a/lib/html-proofer/element.rb +++ b/lib/html-proofer/element.rb @@ -164,7 +164,12 @@ def file_path end if path =~ %r{^/} # path relative to root - base = File.directory?(@check.src) ? @check.src : File.dirname(@check.src) + if File.directory?(@check.src) + base = @check.src + else + root_dir = @check.options[:root_dir] + base = root_dir ? root_dir : File.dirname(@check.src) + end elsif File.exist?(File.expand_path(path, @check.src)) || File.exist?(File.expand_path(path_dot_ext, @check.src)) # relative links, path is a file base = File.dirname @check.path elsif File.exist?(File.join(File.dirname(@check.path), path)) || File.exist?(File.join(File.dirname(@check.path), path_dot_ext)) # relative links in nested dir, path is a file diff --git a/spec/html-proofer/fixtures/links/working_root_link_internal.html b/spec/html-proofer/fixtures/links/working_root_link_internal.html new file mode 100644 index 00000000..9ca8490c --- /dev/null +++ b/spec/html-proofer/fixtures/links/working_root_link_internal.html @@ -0,0 +1,10 @@ + + + +

+ Blah blah blah. + A real link! +

+ + + diff --git a/spec/html-proofer/links_spec.rb b/spec/html-proofer/links_spec.rb index 3214dc78..bf87b0c4 100644 --- a/spec/html-proofer/links_spec.rb +++ b/spec/html-proofer/links_spec.rb @@ -79,6 +79,12 @@ expect(proofer.failed_tests.first).to match(%r{internally linking to .\/notreal.html, which does not exist}) end + it 'succeeds for working internal-root-links pointing to other folder' do + broken_root_link_internal_filepath = "#{FIXTURES_DIR}/links/working_root_link_internal.html" + proofer = run_proofer(broken_root_link_internal_filepath, :file, root_dir: 'spec/html-proofer/fixtures') + expect(proofer.failed_tests).to eq [] + end + it 'fails for link with no href' do missing_link_href_filepath = "#{FIXTURES_DIR}/links/missing_link_href.html" proofer = run_proofer(missing_link_href_filepath, :file)