diff --git a/lib/jekyll-relative-links/generator.rb b/lib/jekyll-relative-links/generator.rb index 0adeeee..1a99886 100644 --- a/lib/jekyll-relative-links/generator.rb +++ b/lib/jekyll-relative-links/generator.rb @@ -78,7 +78,28 @@ def link_parts(matches) relative_path = matches[link_type == :inline ? 3 : last_inline + 2] fragment = matches[link_type == :inline ? 4 : last_inline + 3] title = matches[link_type == :inline ? 5 : last_inline + 4] - Link.new(link_type, link_text, relative_path, fragment, title) + Link.new(link_type, link_text, strip_angle_brackets(relative_path), fragment, title) + end + + # Strip angle brackets from link paths to handle GitHub's angle bracket syntax + # + # GitHub UI uses angle brackets to wrap URLs with special characters like spaces: + # [text](). This method removes the wrapping angle brackets + # so the path can be properly matched against files on disk. + # + # @param path [String, nil] The link path that may have angle brackets + # @return [String, nil] The path with angle brackets removed, or the original if no brackets + # + # @example + # strip_angle_brackets("") + # #=> "file with spaces.md" + # + # strip_angle_brackets("regular-file.md") + # #=> "regular-file.md" + def strip_angle_brackets(path) + return path unless path + + path.sub(%r!\A<(.+)>\z!, '\1') end def context diff --git a/spec/fixtures/site/page with space.md b/spec/fixtures/site/page with space.md new file mode 100644 index 0000000..9e0d376 --- /dev/null +++ b/spec/fixtures/site/page with space.md @@ -0,0 +1,6 @@ +--- +--- + +# Page with space + +[Back to main](page.md) diff --git a/spec/fixtures/site/page.md b/spec/fixtures/site/page.md index b481e24..ddd084b 100644 --- a/spec/fixtures/site/page.md +++ b/spec/fixtures/site/page.md @@ -62,3 +62,11 @@ Content end [reference-with-whitespace]: another-page.md [reference-with-title]: another-page.md "This is a reference with a title" + +[Link with raw space](page with space.md) + +[Link with angle brackets]() + +[Reference with space][ref-space] + +[ref-space]: diff --git a/spec/jekyll_relative_links/filter_spec.rb b/spec/jekyll_relative_links/filter_spec.rb index e2f3497..f551095 100644 --- a/spec/jekyll_relative_links/filter_spec.rb +++ b/spec/jekyll_relative_links/filter_spec.rb @@ -63,6 +63,12 @@ def make_filter expect(filter.rellinks(html)).to eq(html) end + it "handles links with spaces (URL-encoded)" do + html = "

Link with space

" + expected = "

Link with space

" + expect(filter.rellinks(html)).to eq(expected) + end + private def set_subdir_context diff --git a/spec/jekyll_relative_links/generator_spec.rb b/spec/jekyll_relative_links/generator_spec.rb index e11da60..f36e3ed 100644 --- a/spec/jekyll_relative_links/generator_spec.rb +++ b/spec/jekyll_relative_links/generator_spec.rb @@ -55,6 +55,18 @@ expect(page.content).to include("[Page with Symbols?](/page%20with%20symbols?.html)") end + it "converts relative links with raw spaces" do + expect(page.content).to include("[Link with raw space](/page%20with%20space.html)") + end + + it "converts relative links with angle brackets" do + expect(page.content).to include("[Link with angle brackets](/page%20with%20space.html)") + end + + it "converts reference links with angle brackets" do + expect(page.content).to include("[ref-space]: /page%20with%20space.html") + end + it "converts relative links with permalinks" do expect(page.content).to include("[Page with permalink](/page-with-permalink/)") end