Skip to content

Commit

Permalink
Merge pull request TryGhost#6370 from jtwebman/meta_data_to_functions
Browse files Browse the repository at this point in the history
Started moving meta data fetching to functions.
  • Loading branch information
ErisDS committed Jan 25, 2016
2 parents 15cb71b + 1f4c01d commit c29187a
Show file tree
Hide file tree
Showing 43 changed files with 1,715 additions and 135 deletions.
4 changes: 4 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ var _ = require('lodash'),
src: ['core/test/unit/server_helpers/*_spec.js']
},

metadata: {
src: ['core/test/unit/metadata/*_spec.js']
},

middleware: {
src: ['core/test/unit/middleware/*_spec.js']
},
Expand Down
33 changes: 33 additions & 0 deletions core/server/data/meta/asset_url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var config = require('../../config');

function getAssetUrl(context, isAdmin, minify) {
var output = '';

output += config.paths.subdir + '/';

if (!context.match(/^favicon\.ico$/) && !context.match(/^shared/) && !context.match(/^asset/)) {
if (isAdmin) {
output += 'ghost/';
} else {
output += 'assets/';
}
}

// Get rid of any leading slash on the context
context = context.replace(/^\//, '');

// replace ".foo" with ".min.foo" in production
if (minify) {
context = context.replace(/\.([^\.]*)$/, '.min.$1');
}

output += context;

if (!context.match(/^favicon\.ico$/)) {
output = output + '?v=' + config.assetHash;
}

return output;
}

module.exports = getAssetUrl;
14 changes: 14 additions & 0 deletions core/server/data/meta/author_image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var config = require('../../config');

function getAuthorImage(data, absolute) {
var context = data.context ? data.context[0] : null,
blog = config.theme,
contextObject = data[context] || blog;

if (context === 'post' && contextObject.author && contextObject.author.image) {
return config.urlFor('image', {image: contextObject.author.image}, absolute);
}
return null;
}

module.exports = getAuthorImage;
14 changes: 14 additions & 0 deletions core/server/data/meta/author_url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var config = require('../../config');

function getAuthorUrl(data, absolute) {
var context = data.context ? data.context[0] : null;
if (data.author) {
return config.urlFor('author', {author: data.author}, absolute);
}
if (data[context] && data[context].author) {
return config.urlFor('author', {author: data[context].author}, absolute);
}
return null;
}

module.exports = getAuthorUrl;
9 changes: 9 additions & 0 deletions core/server/data/meta/canonical_url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var config = require('../../config'),
getUrl = require('./url');

function getCanonicalUrl(data) {
return config.urlJoin(config.getBaseUrl(false),
getUrl(data, false));
}

module.exports = getCanonicalUrl;
20 changes: 20 additions & 0 deletions core/server/data/meta/cover_image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var config = require('../../config');

function getCoverImage(data) {
var context = data.context ? data.context[0] : null,
blog = config.theme,
contextObject = data[context] || blog;

if (context === 'home' || context === 'author') {
if (contextObject.cover) {
return config.urlFor('image', {image: contextObject.cover}, true);
}
} else {
if (contextObject.image) {
return config.urlFor('image', {image: contextObject.image}, true);
}
}
return null;
}

module.exports = getCoverImage;
25 changes: 25 additions & 0 deletions core/server/data/meta/description.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var _ = require('lodash'),
config = require('../../config');

function getDescription(data, root) {
var description = '',
context = root ? root.context : null;

if (data.meta_description) {
description = data.meta_description;
} else if (_.contains(context, 'paged')) {
description = '';
} else if (_.contains(context, 'home')) {
description = config.theme.description;
} else if (_.contains(context, 'author') && data.author) {
description = data.author.bio;
} else if (_.contains(context, 'tag') && data.tag) {
description = data.tag.meta_description;
} else if ((_.contains(context, 'post') || _.contains(context, 'page')) && data.post) {
description = data.post.meta_description;
}

return (description || '').trim();
}

module.exports = getDescription;
19 changes: 19 additions & 0 deletions core/server/data/meta/excerpt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var downsize = require('downsize');

function getExcerpt(html, truncateOptions) {
// Strip inline and bottom footnotes
var excerpt = html.replace(/<a href="#fn.*?rel="footnote">.*?<\/a>/gi, '');
excerpt = excerpt.replace(/<div class="footnotes"><ol>.*?<\/ol><\/div>/, '');
// Strip other html
excerpt = excerpt.replace(/<\/?[^>]+>/gi, '');
excerpt = excerpt.replace(/(\r\n|\n|\r)+/gm, ' ');
/*jslint regexp:false */

if (!truncateOptions.words && !truncateOptions.characters) {
truncateOptions.words = 50;
}

return downsize(excerpt, truncateOptions);
}

module.exports = getExcerpt;
46 changes: 46 additions & 0 deletions core/server/data/meta/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
var config = require('../../config'),
getUrl = require('./url'),
getCanonicalUrl = require('./canonical_url'),
getPreviousUrl = require('./previous_url'),
getNextUrl = require('./next_url'),
getAuthorUrl = require('./author_url'),
getRssUrl = require('./rss_url'),
getTitle = require('./title'),
getDescription = require('./description'),
getCoverImage = require('./cover_image'),
getAuthorImage = require('./author_image'),
getKeywords = require('./keywords'),
getPublishedDate = require('./published_date'),
getModifiedDate = require('./modified_date'),
getOgType = require('./og_type'),
getStructuredData = require('./structured_data'),
getPostSchema = require('./schema');

function getMetaData(data, root) {
var blog = config.theme, metaData;

metaData = {
url: getUrl(data, true),
canonicalUrl: getCanonicalUrl(data),
previousUrl: getPreviousUrl(data, true),
nextUrl: getNextUrl(data, true),
authorUrl: getAuthorUrl(data, true),
rssUrl: getRssUrl(data, true),
metaTitle: getTitle(data, root),
metaDescription: getDescription(data, root),
coverImage: getCoverImage(data, true),
authorImage: getAuthorImage(data, true),
keywords: getKeywords(data),
publishedDate: getPublishedDate(data),
modifiedDate: getModifiedDate(data),
ogType: getOgType(data),
blog: blog
};

metaData.structuredData = getStructuredData(metaData);
metaData.schema = getPostSchema(metaData, data);

return metaData;
}

module.exports = getMetaData;
10 changes: 10 additions & 0 deletions core/server/data/meta/keywords.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function getKeywords(data) {
if (data.post && data.post.tags && data.post.tags.length > 0) {
return data.post.tags.map(function (tag) {
return tag.name;
});
}
return null;
}

module.exports = getKeywords;
13 changes: 13 additions & 0 deletions core/server/data/meta/modified_date.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function getModifiedDate(data) {
var context = data.context ? data.context[0] : null,
modDate;
if (data[context]) {
modDate = data[context].updated_at || null;
if (modDate) {
return new Date(modDate).toISOString();
}
}
return null;
}

module.exports = getModifiedDate;
23 changes: 23 additions & 0 deletions core/server/data/meta/next_url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var config = require('../../config'),
trimmedUrlpattern = /.+(?=\/page\/\d*\/)/,
tagOrAuthorPattern = /\/(tag)|(author)\//;

function getNextUrl(data, absolute) {
var trimmedUrl, next;

if (data.relativeUrl) {
trimmedUrl = data.relativeUrl.match(trimmedUrlpattern);
if (data.pagination && data.pagination.next) {
next = '/page/' + data.pagination.next + '/';
if (trimmedUrl) {
next = trimmedUrl + next;
} else if (tagOrAuthorPattern.test(data.relativeUrl)) {
next = data.relativeUrl.slice(0, -1) + next;
}
return config.urlFor({relativeUrl: next, secure: data.secure}, absolute);
}
}
return null;
}

module.exports = getNextUrl;
12 changes: 12 additions & 0 deletions core/server/data/meta/og_type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function getOgType(data) {
var context = data.context ? data.context[0] : null;
if (context === 'author') {
return 'profile';
}
if (context === 'post') {
return 'article';
}
return 'website';
}

module.exports = getOgType;
18 changes: 18 additions & 0 deletions core/server/data/meta/previous_url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var config = require('../../config'),
trimmedUrlpattern = /.+(?=\/page\/\d*\/)/;

function getPreviousUrl(data, absolute) {
var trimmedUrl, prev;

if (data.relativeUrl) {
trimmedUrl = data.relativeUrl.match(trimmedUrlpattern);
if (data.pagination && data.pagination.prev) {
prev = (data.pagination.prev > 1 ? '/page/' + data.pagination.prev + '/' : '/');
prev = (trimmedUrl) ? trimmedUrl + prev : prev;
return config.urlFor({relativeUrl: prev, secure: data.secure}, absolute);
}
}
return null;
}

module.exports = getPreviousUrl;
13 changes: 13 additions & 0 deletions core/server/data/meta/published_date.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function getPublishedDate(data) {
var context = data.context ? data.context[0] : null,
pubDate;
if (data[context]) {
pubDate = data[context].published_at || data[context].created_at || null;
if (pubDate) {
return new Date(pubDate).toISOString();
}
}
return null;
}

module.exports = getPublishedDate;
7 changes: 7 additions & 0 deletions core/server/data/meta/rss_url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var config = require('../../config');

function getRssUrl(data, absolute) {
return config.urlFor('rss', {secure: data.secure}, absolute);
}

module.exports = getRssUrl;
Loading

0 comments on commit c29187a

Please sign in to comment.