Skip to content
Permalink
Browse files

Merge pull request #190 from jooohn/feature/guide-pager

Add guide link to next page
  • Loading branch information...
jodosha committed May 30, 2016
2 parents 7123d20 + fe27a24 commit ebe23b0feaca2c45d30e6177be4f62e4901e098b
Showing with 42 additions and 1 deletion.
  1. +37 −0 config.rb
  2. +5 −1 source/layouts/guides.erb
@@ -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 << %(<div class="pull-left">Prev: <a href="#{prev_url}">#{prev_title}</a></div>)
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 << %(<div class="pull-right">Next: <a href="#{next_url}">#{next_title}</a></div>)
end
links.join
end
end

def guides_navigation
result = ''

@@ -19,7 +19,7 @@
<ul>
<% category.pages.each do |page| %>
<li>
<%= link_to(guide_title(page), File.join('/guides', category.path, page.path)) %>
<%= link_to(guide_title(page), guide_url(category, page)) %>
</li>
<% end %>
</ul>
@@ -28,6 +28,10 @@
</ul>

<%= yield %>

<hr>

<%= guide_pager(current_page, data.guides) %>
</div>

<%= partial 'footer' %>

0 comments on commit ebe23b0

Please sign in to comment.
You can’t perform that action at this time.