Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Urls starting with / handled as absolute urls from the root #62

Merged
merged 3 commits into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 11 additions & 9 deletions lib/jekyll-relative-links/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def replace_relative_links!(document)
link = link_parts(Regexp.last_match)
next original unless replaceable_link?(link.path)

path = path_from_root(link.path, url_base)
path = path_from_root(link.path, url_base, link.is_absolute)
url = url_for_path(path)
next original unless url

Expand All @@ -67,16 +67,17 @@ def replace_relative_links!(document)
private

# Stores info on a Markdown Link (avoid rubocop's Metrics/ParameterLists warning)
Link = Struct.new(:link_type, :text, :path, :fragment, :title)
Link = Struct.new(:link_type, :text, :path, :fragment, :title, :is_absolute)

def link_parts(matches)
last_inline = 5
link_type = matches[2] ? :inline : :reference
link_text = matches[link_type == :inline ? 2 : last_inline + 1]
link_type = matches[2] ? :inline : :reference
link_text = matches[link_type == :inline ? 2 : last_inline + 1]
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)
fragment = matches[link_type == :inline ? 4 : last_inline + 3]
title = matches[link_type == :inline ? 5 : last_inline + 4]
is_absolute = relative_path.start_with? "/"
Link.new(link_type, link_text, relative_path, fragment, title, is_absolute)
end

def context
Expand All @@ -100,9 +101,10 @@ def potential_targets
@potential_targets ||= site.pages + site.static_files + site.docs_to_write
end

def path_from_root(relative_path, url_base)
def path_from_root(relative_path, url_base, is_absolute)
relative_path.sub!(%r!\A/!, "")
absolute_path = File.expand_path(relative_path, url_base)
base = is_absolute ? "" : url_base
absolute_path = File.expand_path(relative_path, base)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to put this in path_from_root rather than passing it around?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point!

absolute_path.sub(%r!\A#{Regexp.escape(Dir.pwd)}/!, "")
end

Expand Down
2 changes: 2 additions & 0 deletions spec/fixtures/site/page.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

[Page with leading slash](/another-page.md)

[Page with leading slash multi level](/subdir/page.md)

[Reference link][reference]

[Reference with fragment][reference-with-fragment]
Expand Down
2 changes: 2 additions & 0 deletions spec/fixtures/site/subdir/page.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@

[Dir traversal](../page.md)

[leading slash starts from the root](/another-page.md)

![image](../jekyll-logo.png)
9 changes: 9 additions & 0 deletions spec/jekyll-relative-links/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@
expect(page.content).to include("[Page with leading slash](/another-page.html)")
end

it "converts relative links with leading slashes in sub dir" do
expect(page.content).to include("[Page with leading slash multi level](/subdir/page.html)")
end

it "converts pages in sub-directories" do
expect(page.content).to include("[Subdir Page](/subdir/page.html)")
end
Expand All @@ -77,6 +81,11 @@
expect(subdir_page.content).to include("[Dir traversal](/page.html)")
end

it "handles paths from the root" do
expected = "[leading slash starts from the root](/another-page.html)"
expect(subdir_page.content).to include(expected)
end

it "Handles HTML pages" do
expect(page.content).to include("[HTML Page](/html-page.html)")
end
Expand Down