Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for NotFoundRoute, Redirect, Pagination, Tags #36

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 46 additions & 1 deletion lib/isomorphic/create-routes.coffee
Expand Up @@ -3,6 +3,7 @@ filter = require 'lodash/collection/filter'
sortBy = require 'lodash/collection/sortBy'
last = require 'lodash/array/last'
includes = require 'underscore.string/include'
{ config } = require 'config'

module.exports = (pages, pagesReq) ->
templates = {}
Expand All @@ -12,6 +13,33 @@ module.exports = (pages, pagesReq) ->
handler: pagesReq './_template'
})

# Adds a handler for <NotFoundRoute /> that will serve as a 404 Page
if config.notFound
templates.rootNotFound = Router.createNotFoundRoute({
name: 'root-not-found'
path: "*"
handler: pagesReq './_404'
parentRoute: templates.root
})

# Adds a handler for <Route path="page/*" /> for pagination
if config.pagination
templates.rootPagination = Router.createRoute({
name: "root-pagination"
path: "page/:pageId"
parentRoute: templates.root
handler: pagesReq './_pagination'
})

# Adds a handler for <Route path="tag/*" /> for pages tagging
if config.tags
templates.rootTags = Router.createRoute({
name: "root-tags"
path: "tag/:tagId"
parentRoute: templates.root
handler: pagesReq './_tag'
})

# Arrange pages in data structure according to their position
# on the file system. Then use this to create routes.
#
Expand Down Expand Up @@ -45,6 +73,15 @@ module.exports = (pages, pagesReq) ->
handler: pagesReq "./" + templateFile.requirePath
})

# Adds a handler for <Route path="page/*" /> (for each templateFile) for pagination
if config.pagination
templates[templateFile.file.dirname + "-pagination"] = Router.createRoute({
name: templateFile.file.dirname + "-pagination"
path: templateFile.templatePath + "page/:pageId"
parentRoute: templates[templateFile.file.dirname]
handler: pagesReq "./" + templateFile.file.dirname + "/_pagination"
})

# Remove files that start with an underscore as this indicates
# the file shouldn't be turned into a page.
filteredPages = filter pages, (page) -> page.file.name.slice(0,1) isnt '_'
Expand Down Expand Up @@ -85,9 +122,17 @@ module.exports = (pages, pagesReq) ->
unless parentRoute
parentRoute = templates.root

if page.data and page.data.redirect
# Adds a handler for <Redirect from="some/where" to="somewhere/else" /> where page has a `redirect` metadata
Router.createRedirect({
name: page.path
from: page.path
to: page.data.redirect
parentRoute: parentRoute
})
# If page is an index page *and* in the same directory as a template,
# it is the default route (for that template).
if includes(page.path, "/index") and
else if includes(page.path, "/index") and
parentRoute.file.dirname is parentTemplateFile.file.dirname
Router.createDefaultRoute({
name: page.path
Expand Down