-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathauto-link.rb
More file actions
30 lines (26 loc) · 941 Bytes
/
auto-link.rb
File metadata and controls
30 lines (26 loc) · 941 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
require "nokogiri"
require "addressable/uri"
Jekyll::Hooks.register [:pages, :documents], :post_convert do |doc|
next unless doc.output_ext == ".html"
site = doc.site
liquid_context = Liquid::Context.new({}, {}, { site: site })
process_uri = lambda do |path|
uri = Addressable::URI.parse(path)
if uri&.path
uri.path = Liquid::Template.parse("{% link #{uri.path[1..]} %}").render!(liquid_context)
end
uri.to_s
end
fragment = Nokogiri::HTML::DocumentFragment.parse(doc.content)
fragment.css("[src^=\"/assets/\"],[src^=\"/\"][src$=\".md\"],[src^=\"/\"][src*=\".md#\"]").each do |item|
if item["src"]
item["src"] = process_uri.call(item["src"])
end
end
fragment.css("[href^=\"/assets/\"],[href^=\"/\"][href$=\".md\"],[href^=\"/\"][href*=\".md#\"]").each do |item|
if item["href"]
item["href"] = process_uri.call(item["href"])
end
end
doc.content = fragment.to_html
end