summaryrefslogtreecommitdiffstats
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
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.
-rw-r--r--blog/_layouts/post.html12
-rw-r--r--blog/_plugins/prev_next.rb57
2 files changed, 63 insertions, 6 deletions
diff --git a/blog/_layouts/post.html b/blog/_layouts/post.html
index 8a3cccb..f4f2a59 100644
--- a/blog/_layouts/post.html
+++ b/blog/_layouts/post.html
@@ -13,11 +13,11 @@ layout: default
{{ content }}
</section>
<footer>
- {% if page.previous %}
- <div class="prev-post"><a href="{{ page.previous.url | prepend: site.baseurl }}">{{ page.previous.title }}</a></div>
- {% endif %}
- {% if page.next %}
- <div class="next-post"><a href="{{ page.next.url | prepend: site.baseurl }}">{{ page.next.title }}</a></div>
- {% endif %}
+ <div class="prev-post">
+ {% link_to_prev_page Forrige: %}
+ </div>
+ <div class="next-post">
+ {% link_to_next_page Neste: %}
+ </div>
</footer>
</article>
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)