2013-12-06 18:13:15 +04:00
|
|
|
// Contains all path information to be used throughout
|
|
|
|
// the codebase.
|
2013-11-20 17:58:52 +04:00
|
|
|
|
2014-01-03 04:37:21 +04:00
|
|
|
var moment = require('moment'),
|
2014-02-05 12:40:30 +04:00
|
|
|
_ = require('lodash'),
|
2014-01-05 10:40:53 +04:00
|
|
|
ghostConfig = '';
|
|
|
|
|
|
|
|
// ## setConfig
|
|
|
|
// Simple utility function to allow
|
|
|
|
// passing of the ghostConfig
|
|
|
|
// object here to be used locally
|
|
|
|
// to ensure clean depedency graph
|
|
|
|
// (i.e. no circular dependencies).
|
|
|
|
function setConfig(config) {
|
|
|
|
ghostConfig = config;
|
2013-11-20 17:58:52 +04:00
|
|
|
}
|
|
|
|
|
2014-01-03 04:37:21 +04:00
|
|
|
// ## createUrl
|
|
|
|
// Simple url creation from a given path
|
|
|
|
// Ensures that our urls contain the subdirectory if there is one
|
|
|
|
// And are correctly formatted as either relative or absolute
|
|
|
|
// Usage:
|
|
|
|
// createUrl('/', true) -> http://my-ghost-blog.com/
|
|
|
|
// E.g. /blog/ subdir
|
|
|
|
// createUrl('/welcome-to-ghost/') -> /blog/welcome-to-ghost/
|
|
|
|
// Parameters:
|
|
|
|
// - urlPath - string which must start and end with a slash
|
|
|
|
// - absolute (optional, default:false) - boolean whether or not the url should be absolute
|
2014-02-22 05:25:31 +04:00
|
|
|
// - secure (optional, default:false) - boolean whether or not to use urlSSL or url config
|
2014-01-03 04:37:21 +04:00
|
|
|
// Returns:
|
|
|
|
// - a URL which always ends with a slash
|
2014-02-22 05:25:31 +04:00
|
|
|
function createUrl(urlPath, absolute, secure) {
|
2014-01-03 04:37:21 +04:00
|
|
|
urlPath = urlPath || '/';
|
|
|
|
absolute = absolute || false;
|
2014-02-22 05:25:31 +04:00
|
|
|
var output = '', baseUrl;
|
2014-01-03 04:37:21 +04:00
|
|
|
|
|
|
|
// create base of url, always ends without a slash
|
|
|
|
if (absolute) {
|
2014-02-22 05:25:31 +04:00
|
|
|
baseUrl = (secure && ghostConfig.urlSSL) ? ghostConfig.urlSSL : ghostConfig.url;
|
|
|
|
output += baseUrl.replace(/\/$/, '');
|
2014-01-03 04:37:21 +04:00
|
|
|
} else {
|
2014-01-05 10:40:53 +04:00
|
|
|
output += ghostConfig.paths.subdir;
|
2014-01-03 04:37:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// append the path, always starts and ends with a slash
|
|
|
|
output += urlPath;
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ## urlPathForPost
|
|
|
|
// Always sync
|
|
|
|
// Creates the url path for a post, given a post and a permalink
|
|
|
|
// Parameters:
|
|
|
|
// - post - a json object representing a post
|
2014-12-10 17:03:39 +03:00
|
|
|
// - permalinks - a string containing the permalinks setting
|
2014-01-03 04:37:21 +04:00
|
|
|
function urlPathForPost(post, permalinks) {
|
|
|
|
var output = '',
|
|
|
|
tags = {
|
|
|
|
year: function () { return moment(post.published_at).format('YYYY'); },
|
|
|
|
month: function () { return moment(post.published_at).format('MM'); },
|
|
|
|
day: function () { return moment(post.published_at).format('DD'); },
|
2014-10-26 04:15:24 +03:00
|
|
|
author: function () { return post.author.slug; },
|
|
|
|
slug: function () { return post.slug; },
|
|
|
|
id: function () { return post.id; }
|
2014-01-03 04:37:21 +04:00
|
|
|
};
|
|
|
|
|
2014-06-12 13:44:10 +04:00
|
|
|
if (post.page) {
|
2014-01-03 04:37:21 +04:00
|
|
|
output += '/:slug/';
|
|
|
|
} else {
|
2014-12-10 17:03:39 +03:00
|
|
|
output += permalinks;
|
2014-01-03 04:37:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// replace tags like :slug or :year with actual values
|
|
|
|
output = output.replace(/(:[a-z]+)/g, function (match) {
|
|
|
|
if (_.has(tags, match.substr(1))) {
|
|
|
|
return tags[match.substr(1)]();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ## urlFor
|
|
|
|
// Synchronous url creation for a given context
|
|
|
|
// Can generate a url for a named path, given path, or known object (post)
|
|
|
|
// Determines what sort of context it has been given, and delegates to the correct generation method,
|
|
|
|
// Finally passing to createUrl, to ensure any subdirectory is honoured, and the url is absolute if needed
|
|
|
|
// Usage:
|
|
|
|
// urlFor('home', true) -> http://my-ghost-blog.com/
|
|
|
|
// E.g. /blog/ subdir
|
|
|
|
// urlFor({relativeUrl: '/my-static-page/') -> /blog/my-static-page/
|
|
|
|
// E.g. if post object represents welcome post, and slugs are set to standard
|
|
|
|
// urlFor('post', {...}) -> /welcome-to-ghost/
|
|
|
|
// E.g. if post object represents welcome post, and slugs are set to date
|
|
|
|
// urlFor('post', {...}) -> /2014/01/01/welcome-to-ghost/
|
|
|
|
// Parameters:
|
|
|
|
// - context - a string, or json object describing the context for which you need a url
|
|
|
|
// - data (optional) - a json object containing data needed to generate a url
|
|
|
|
// - absolute (optional, default:false) - boolean whether or not the url should be absolute
|
|
|
|
// This is probably not the right place for this, but it's the best place for now
|
|
|
|
function urlFor(context, data, absolute) {
|
|
|
|
var urlPath = '/',
|
2014-10-21 14:50:34 +04:00
|
|
|
secure, imagePathRe,
|
2014-12-02 17:24:51 +03:00
|
|
|
knownObjects = ['post', 'tag', 'author', 'image'], baseUrl,
|
2014-05-02 03:42:23 +04:00
|
|
|
|
|
|
|
// this will become really big
|
|
|
|
knownPaths = {
|
2014-09-10 08:06:24 +04:00
|
|
|
home: '/',
|
|
|
|
rss: '/rss/',
|
|
|
|
api: '/ghost/api/v0.1'
|
2014-05-02 03:42:23 +04:00
|
|
|
};
|
2014-01-03 04:37:21 +04:00
|
|
|
|
|
|
|
// Make data properly optional
|
|
|
|
if (_.isBoolean(data)) {
|
|
|
|
absolute = data;
|
|
|
|
data = null;
|
|
|
|
}
|
|
|
|
|
2014-02-22 05:25:31 +04:00
|
|
|
// Can pass 'secure' flag in either context or data arg
|
|
|
|
secure = (context && context.secure) || (data && data.secure);
|
|
|
|
|
2014-01-03 04:37:21 +04:00
|
|
|
if (_.isObject(context) && context.relativeUrl) {
|
|
|
|
urlPath = context.relativeUrl;
|
|
|
|
} else if (_.isString(context) && _.indexOf(knownObjects, context) !== -1) {
|
|
|
|
// trying to create a url for an object
|
2014-12-10 17:03:39 +03:00
|
|
|
if (context === 'post' && data.post) {
|
|
|
|
urlPath = data.post.url;
|
|
|
|
secure = data.secure;
|
2014-02-13 20:15:29 +04:00
|
|
|
} else if (context === 'tag' && data.tag) {
|
|
|
|
urlPath = '/tag/' + data.tag.slug + '/';
|
2014-02-22 05:25:31 +04:00
|
|
|
secure = data.tag.secure;
|
2014-07-20 20:41:59 +04:00
|
|
|
} else if (context === 'author' && data.author) {
|
|
|
|
urlPath = '/author/' + data.author.slug + '/';
|
|
|
|
secure = data.author.secure;
|
2014-10-21 14:50:34 +04:00
|
|
|
} else if (context === 'image' && data.image) {
|
|
|
|
urlPath = data.image;
|
|
|
|
imagePathRe = new RegExp('^' + ghostConfig.paths.subdir + '/' + ghostConfig.paths.imagesRelPath);
|
|
|
|
absolute = imagePathRe.test(data.image) ? absolute : false;
|
|
|
|
secure = data.image.secure;
|
2014-11-17 06:38:10 +03:00
|
|
|
|
2014-12-02 17:24:51 +03:00
|
|
|
if (absolute) {
|
|
|
|
// Remove the sub-directory from the URL because ghostConfig will add it back.
|
|
|
|
urlPath = urlPath.replace(new RegExp('^' + ghostConfig.paths.subdir), '');
|
|
|
|
baseUrl = (secure && ghostConfig.urlSSL) ? ghostConfig.urlSSL : ghostConfig.url;
|
|
|
|
baseUrl = baseUrl.replace(/\/$/, '');
|
|
|
|
urlPath = baseUrl + urlPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
return urlPath;
|
2014-12-01 13:43:01 +03:00
|
|
|
} else if (context === 'sitemap-xsl') {
|
|
|
|
absolute = true;
|
|
|
|
urlPath = '/sitemap.xsl';
|
2014-01-03 04:37:21 +04:00
|
|
|
}
|
|
|
|
// other objects are recognised but not yet supported
|
|
|
|
} else if (_.isString(context) && _.indexOf(_.keys(knownPaths), context) !== -1) {
|
|
|
|
// trying to create a url for a named path
|
|
|
|
urlPath = knownPaths[context] || '/';
|
|
|
|
}
|
|
|
|
|
2014-02-22 05:25:31 +04:00
|
|
|
return createUrl(urlPath, absolute, secure);
|
2014-01-03 04:37:21 +04:00
|
|
|
}
|
|
|
|
|
2014-01-05 10:40:53 +04:00
|
|
|
module.exports.setConfig = setConfig;
|
2014-01-03 04:37:21 +04:00
|
|
|
module.exports.urlFor = urlFor;
|
2014-12-10 17:03:39 +03:00
|
|
|
module.exports.urlPathForPost = urlPathForPost;
|