-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathslug.rb
47 lines (38 loc) · 1.3 KB
/
slug.rb
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class SlugTag < Liquid::Block
@@page_by_slug = nil
def initialize(tag_name, text, tokens)
@text = text.strip
end
def index_slugs(site)
return if @@page_by_slug
@@page_by_slug = Hash.new
Jekyll.logger.info " Indexing slugs in #{site.pages.length} pages..."
duplicates = false
page = site.pages.each do |p|
slug = p.data['slug']
if (slug)
if @@page_by_slug.has_key?(slug)
Jekyll.logger.warn "Duplicate slug '#{slug}' on #{p.url}"
duplicates = true
end
@@page_by_slug[slug] = p
end
end
if duplicates
raise "Duplicate slugs found. Aborting"
else
Jekyll.logger.info " Done. Found #{@@page_by_slug.length} unique slugs."
end
end
def render(context)
index_slugs context.registers[:site]
page = @@page_by_slug[@text]
if page
page.url.sub('.html', '')
else
page_url = context.environments.first["page"]["url"]
Jekyll.logger.warn "Slug:", "No page with slug `#{@text}` in #{page_url}. Consider fixing the slug or use normal link."
end
end
end
Liquid::Template.register_tag('slug', SlugTag)