Skip to content

Commit

Permalink
Merge pull request #21 from Financial-Times/topic-url-helper
Browse files Browse the repository at this point in the history
added helper for converting search strings to stream urls
  • Loading branch information
matthew-andrews committed Jan 23, 2015
2 parents 5954659 + f2f9c0c commit 0742427
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ Encoding strings to be output safely in html
- `{{encode q mode='uriComponent'}}` outputs the result of `encodeURIComponent(q)` (`{{encode q }}` will also do this)
- `{{encode q mode='uri'}}` outputs the result of `encodeURI(q)`

### topicUrl
Takes a topic identifier (currently something like `topic:"European%20Cars"`) and converts to a next stream url `/stream/topic/European%20Cars`
- `{{topicUrl searchString}}`

### paragraphs
Outputting some paragraphs from a larger chunk of html, zero indexed
- `{{{paragraphs body start=0 end=1}}}` will output the first paragraph of `body`. *Note the triple mustaches*
Expand Down
2 changes: 2 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var removeImageTags = require('./src/handlebars/remove-image-tags');
var normalizeName = require('./src/normalize-name');
var ifEquals = require('./src/handlebars/if-equals');
var ifAll = require('./src/handlebars/if-all');
var topicUrl = require('./src/handlebars/topic-url');
var dateformat = require('./src/handlebars/dateformat');
var resize = require('./src/handlebars/resize');
var encode = require('./src/handlebars/encode');
Expand Down Expand Up @@ -58,6 +59,7 @@ module.exports = function(options) {
helpers.ifEquals = ifEquals;
helpers.ifAll = ifAll;
helpers.encode = encode;
helpers.topicUrl = topicUrl;

app.use('/' + name, express.static(directory + '/public', {
setHeaders: function(res) {
Expand Down
19 changes: 19 additions & 0 deletions src/handlebars/topic-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

module.exports = function(id, options) {
// we treat id as a search string (what we're using as identifiers in v1)
var parts = id.match(/^(.+)\:"([^"]+)"$/);

if (parts) {
parts = parts.slice(1, 3);
if(['page'].indexOf(parts[0]) === -1) {
return '/stream/' + parts.join('/');
} else {
return '/' + parts.join('/');
}
}

// currently don't understand any other kinds of id e.g. capi2 id... to be fleshed out later
// just return noop url for now, with something we can track
return '#" data-track-invalid-url-' + id;
};

0 comments on commit 0742427

Please sign in to comment.