Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion lib/jekyll-relative-links/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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](<path with spaces.md>). 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>")
# #=> "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
Expand Down
6 changes: 6 additions & 0 deletions spec/fixtures/site/page with space.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
---

# Page with space

[Back to main](page.md)
8 changes: 8 additions & 0 deletions spec/fixtures/site/page.md
Original file line number Diff line number Diff line change
Expand Up @@ -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](<page with space.md>)

[Reference with space][ref-space]

[ref-space]: <page with space.md>
6 changes: 6 additions & 0 deletions spec/jekyll_relative_links/filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ def make_filter
expect(filter.rellinks(html)).to eq(html)
end

it "handles links with spaces (URL-encoded)" do
html = "<p><a href=\"page%20with%20space.md\">Link with space</a></p>"
expected = "<p><a href=\"/page%20with%20space.html\">Link with space</a></p>"
expect(filter.rellinks(html)).to eq(expected)
end

private

def set_subdir_context
Expand Down
12 changes: 12 additions & 0 deletions spec/jekyll_relative_links/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down