From 1d3a63d75fce583032333a9d088a0d6d6569f58d Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 7 Jul 2012 16:09:23 +0200 Subject: o Splitting 'hrefs' so the user can click on any path segment. --- routes/index.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'routes') diff --git a/routes/index.js b/routes/index.js index fb81a41..cace65e 100644 --- a/routes/index.js +++ b/routes/index.js @@ -4,9 +4,35 @@ var collection_json = require('collection_json') , util = require('util') , _ = require('underscore'); +function split(str) { + var u = url.parse(str); + + var segments = u.pathname.replace(/\/$/, '').split('/'); + + var splits = _.map(_.range(segments.length), function(i) { + var x = _.clone(u); + x.pathname = _.first(segments, i + 1).join('/'); + x.search = ''; + if(i == 0) { + return [u.protocol + '//' + u.host, url.format(x)]; + } + else { + return ['/' + segments[i], url.format(x)]; + } + }); + + if(u.search) { + splits.push([(u.pathname.match(/\/$/) ? '/' : '') + u.search, url.format(u)]); + } + + return splits; +} +exports.split = split; + function urlgenerator(req) { var host = req.headers.host; return { + split: split, render: function(u) { return 'http://' + host + '/render?url=' + encodeURIComponent(u)}, delete: function(referer, u) { return 'http://' + host + '/delete?referer=' + encodeURIComponent(referer) + '&url=' + encodeURIComponent(u)}, isUrl: function(u) { -- cgit v1.2.3