From fe27a243561315839df110e77da62a6a42c6f957 Mon Sep 17 00:00:00 2001 From: jooohn Date: Sun, 29 May 2016 00:11:08 +0900 Subject: [PATCH] feat(guides) add next page link --- config.rb | 37 +++++++++++++++++++++++++++++++++++++ source/layouts/guides.erb | 6 +++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/config.rb b/config.rb index e772f21de..1b447f788 100644 --- a/config.rb +++ b/config.rb @@ -2,6 +2,7 @@ Bundler.require(:default, ENV['SITE_ENV']) if defined?(Bundler) +require 'ostruct' require 'rack/utils' require 'middleman-syntax' require 'lib/github_style_titles' @@ -117,6 +118,42 @@ def guide_title(item) item.title || item.path.split('-').map(&:capitalize).join(' ') end + def guide_url(category, page) + File.join('/guides', category.path, page.path) + end + + def guide_pager(current_page, guides) + current_url = current_page.url.tr('/', '') + flat_guides = guides.categories.flat_map { |category| + category.pages.map { |page| + OpenStruct.new( + category: category, + page: page, + ) + } + } + current_guide_index = flat_guides.index { |guide_page| + guide_url(guide_page.category, guide_page.page).tr('/', '') == current_url + } + if current_guide_index + links = [] + prev_guide = flat_guides[current_guide_index - 1] + if 0 < current_guide_index && prev_guide + prev_url = guide_url(prev_guide.category, prev_guide.page) + prev_title = "#{guide_title(prev_guide.category)} - #{guide_title(prev_guide.page)}" + links << %(
Prev: #{prev_title}
) + end + + next_guide = flat_guides[current_guide_index + 1] + if next_guide + next_url = guide_url(next_guide.category, next_guide.page) + next_title = "#{guide_title(next_guide.category)} - #{guide_title(next_guide.page)}" + links << %(
Next: #{next_title}
) + end + links.join + end + end + def guides_navigation result = '' diff --git a/source/layouts/guides.erb b/source/layouts/guides.erb index cbb112a4e..c2ae3d8da 100644 --- a/source/layouts/guides.erb +++ b/source/layouts/guides.erb @@ -19,7 +19,7 @@ @@ -28,6 +28,10 @@ <%= yield %> + +
+ + <%= guide_pager(current_page, data.guides) %> <%= partial 'footer' %>