jekyll-relative-links 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ba6b38e69cda482b2142b9e88772c64963323a21
4
- data.tar.gz: 34303fa94a455987bee54fdac2f78a785a968c86
3
+ metadata.gz: 5ca1f80629950904c828211cc5c032e590eb02e7
4
+ data.tar.gz: 0f699f5d6a7dbc1ad7227a03e327155bf58d51e5
5
5
  SHA512:
6
- metadata.gz: 2e8ed38db685fc82e2aafe3a8e32f6c296c38681548873efaa07487b2d480afcbbf46e33b95580c4bb50854050fb28faf9313452ffafe077a1a53f2cdc345861
7
- data.tar.gz: 50630b46713f52d82ef9ee4c4cd17f4a7e1b87d85b2bcab7747e93c94164427709935cb2ff3d29d1fcc4ccd3da0b4e50bca4a634694d13e1fd23bd41ed4b002a
6
+ metadata.gz: 9167289dc30d083a23ff0a33d62f78a49e6406858fd82dc7b4e4bad9fadcd876e98ad6a7279b58a272d0833bae4f3c2d67b2ed075899a4f1f839adf377e387ad
7
+ data.tar.gz: b154836fd6304a5696017183b024978e7e598a203b66d63cbd5f135d13a8d0e56a30149a27a43243d518a3d7afbc4945e1d64c7f90868cefb1534046b88a40fa
@@ -26,29 +26,38 @@ module JekyllRelativeLinks
26
26
 
27
27
  site.pages.each do |page|
28
28
  next unless markdown_extension?(page.extname)
29
- url_base = File.dirname(page.path)
30
-
31
- page.content.gsub!(LINK_REGEX) do |original|
32
- link_type = Regexp.last_match(2) ? :inline : :reference
33
- link_text = Regexp.last_match(link_type == :inline ? 2 : 5)
34
- relative_path = Regexp.last_match(link_type == :inline ? 3 : 6)
35
- fragment = Regexp.last_match(link_type == :inline ? 4 : 7)
36
-
37
- relative_path.sub!(%r!\A/!, "")
38
- url = url_for_path(path_from_root(relative_path, url_base))
39
-
40
- if url
41
- url << fragment if fragment
42
- replacement_text(link_type, link_text, url)
43
- else
44
- original
45
- end
29
+ replace_relative_links!(page)
30
+ end
31
+ end
32
+
33
+ def replace_relative_links!(page)
34
+ url_base = File.dirname(page.path)
35
+
36
+ page.content.gsub!(LINK_REGEX) do |original|
37
+ link_type, link_text, relative_path, fragment = link_parts(Regexp.last_match)
38
+ path = path_from_root(relative_path, url_base)
39
+ url = url_for_path(path)
40
+
41
+ if url
42
+ replacement_text(link_type, link_text, url, fragment)
43
+ else
44
+ original
46
45
  end
47
46
  end
47
+ rescue ArgumentError => e
48
+ raise e unless e.to_s.start_with?("invalid byte sequence in UTF-8")
48
49
  end
49
50
 
50
51
  private
51
52
 
53
+ def link_parts(matches)
54
+ link_type = matches[2] ? :inline : :reference
55
+ link_text = matches[link_type == :inline ? 2 : 5]
56
+ relative_path = matches[link_type == :inline ? 3 : 6]
57
+ fragment = matches[link_type == :inline ? 4 : 7]
58
+ [link_type, link_text, relative_path, fragment]
59
+ end
60
+
52
61
  def context
53
62
  JekyllRelativeLinks::Context.new(site)
54
63
  end
@@ -62,19 +71,23 @@ module JekyllRelativeLinks
62
71
  end
63
72
 
64
73
  def url_for_path(path)
65
- extension = File.extname(path)
66
- return unless markdown_extension?(extension)
74
+ target = potential_targets.find { |p| p.relative_path.sub(%r!\A/!, "") == path }
75
+ relative_url(target.url) if target
76
+ end
67
77
 
68
- page = site.pages.find { |p| p.path == path }
69
- relative_url(page.url) if page
78
+ def potential_targets
79
+ @potential_targets ||= (site.pages + site.static_files)
70
80
  end
71
81
 
72
82
  def path_from_root(relative_path, url_base)
83
+ relative_path.sub!(%r!\A/!, "")
73
84
  absolute_path = File.expand_path(relative_path, url_base)
74
85
  absolute_path.sub(%r!\A#{Dir.pwd}/!, "")
75
86
  end
76
87
 
77
- def replacement_text(type, text, url)
88
+ def replacement_text(type, text, url, fragment = nil)
89
+ url << fragment if fragment
90
+
78
91
  if type == :inline
79
92
  "[#{text}](#{url})"
80
93
  else
@@ -1,3 +1,3 @@
1
1
  module JekyllRelativeLinks
2
- VERSION = "0.3.0".freeze
2
+ VERSION = "0.4.0".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-relative-links
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Balter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-13 00:00:00.000000000 Z
11
+ date: 2017-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -83,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
83
  version: '0'
84
84
  requirements: []
85
85
  rubyforge_project:
86
- rubygems_version: 2.5.2
86
+ rubygems_version: 2.6.11
87
87
  signing_key:
88
88
  specification_version: 4
89
89
  summary: A Jekyll plugin to convert relative links to markdown files to their rendered