summaryrefslogtreecommitdiffstats
path: root/blog/_plugins
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2017-08-28 22:23:44 +0200
committerHarald Eilertsen <haraldei@anduin.net>2017-08-28 22:23:44 +0200
commit18608c867a6cbe7880ea0273c463d4be93f2cc9f (patch)
tree83a5257811f15e7eec5bdcc600d8207b4fb15d61 /blog/_plugins
parent8715cb575680b18da3f1292d90f6a9ec10e12864 (diff)
downloadnorsk-urskog-main-18608c867a6cbe7880ea0273c463d4be93f2cc9f.tar.gz
norsk-urskog-main-18608c867a6cbe7880ea0273c463d4be93f2cc9f.tar.bz2
norsk-urskog-main-18608c867a6cbe7880ea0273c463d4be93f2cc9f.zip
Fix prev/next links for posts.
Now points to prev/next post in same language as the current post.
Diffstat (limited to 'blog/_plugins')
-rw-r--r--blog/_plugins/prev_next.rb57
1 files changed, 57 insertions, 0 deletions
diff --git a/blog/_plugins/prev_next.rb b/blog/_plugins/prev_next.rb
new file mode 100644
index 0000000..7e9c983
--- /dev/null
+++ b/blog/_plugins/prev_next.rb
@@ -0,0 +1,57 @@
+require 'pry-byebug'
+
+module Jekyll
+ class PrevNextTag < Liquid::Tag
+
+ def initialize(tag_name, text, tokens)
+ super
+ @tag = tag_name
+ @text = text
+ end
+
+ def render(context)
+ page = context['page']
+ other_page = case @tag
+ when "link_to_prev_page" then find_prev(page)
+ when "link_to_next_page" then find_next(page)
+ else nil
+ end
+
+ if other_page
+ %Q{<a href="#{other_page['url']}">#{@text} #{other_page['title']}</a>}
+ else
+ ""
+ end
+ end
+
+ private
+
+ def find_prev(obj)
+ n = obj.previous
+ loop do
+ if n && n['lang'] != obj['lang']
+ n = n.previous
+ else
+ break
+ end
+ end
+ return n
+ end
+
+ def find_next(obj)
+ n = obj.next
+ loop do
+ if n && n['lang'] != obj['lang']
+ n = n.next
+ else
+ break
+ end
+ end
+ return n
+ end
+
+ end
+end
+
+Liquid::Template.register_tag('link_to_next_page', Jekyll::PrevNextTag)
+Liquid::Template.register_tag('link_to_prev_page', Jekyll::PrevNextTag)