Skip to content

Commit 1f4c01d

Browse files
committed
Started moving meta data fetching to functions.
issue #6186 - Moved asset helper logic to a asset url function. - Created author image function to be used in ghost_head helper. - Created author url function to be used in the ghost_head helper. - Created canonical url function to be used in the ghost_head helper. - Moved meta_description helper logic to a function. - Moved excerpt helper logic to a function. - Created an index in data/meta to be used in ghost_head helper to get all data. - Created keyword function to be used in the ghost_head helper. - Created modified data function to be used in the ghost_head helper. - Created next url function to be used in the ghost_head helper. - Created ogType function to be used in the ghost_head helper. - Created previous url function to be used in the ghost_head helper. - Created published data function to be used in the ghost_head helper. - Created rss url function to be used in the ghost_head helper. - Created schema function to be used in the ghost_head helper. - Created structured data function to be used in the ghost_head helper. - Moved meta_title helper logic to a title function. - Moved url helper logic to a url function. - Wrote tests for all the new functions This is just the first step. I plan on refactoring the ghost head to use these new functions.
1 parent 57ae36a commit 1f4c01d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1715
-135
lines changed

Gruntfile.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ var _ = require('lodash'),
196196
src: ['core/test/unit/server_helpers/*_spec.js']
197197
},
198198

199+
metadata: {
200+
src: ['core/test/unit/metadata/*_spec.js']
201+
},
202+
199203
middleware: {
200204
src: ['core/test/unit/middleware/*_spec.js']
201205
},

core/server/data/meta/asset_url.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var config = require('../../config');
2+
3+
function getAssetUrl(context, isAdmin, minify) {
4+
var output = '';
5+
6+
output += config.paths.subdir + '/';
7+
8+
if (!context.match(/^favicon\.ico$/) && !context.match(/^shared/) && !context.match(/^asset/)) {
9+
if (isAdmin) {
10+
output += 'ghost/';
11+
} else {
12+
output += 'assets/';
13+
}
14+
}
15+
16+
// Get rid of any leading slash on the context
17+
context = context.replace(/^\//, '');
18+
19+
// replace ".foo" with ".min.foo" in production
20+
if (minify) {
21+
context = context.replace(/\.([^\.]*)$/, '.min.$1');
22+
}
23+
24+
output += context;
25+
26+
if (!context.match(/^favicon\.ico$/)) {
27+
output = output + '?v=' + config.assetHash;
28+
}
29+
30+
return output;
31+
}
32+
33+
module.exports = getAssetUrl;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var config = require('../../config');
2+
3+
function getAuthorImage(data, absolute) {
4+
var context = data.context ? data.context[0] : null,
5+
blog = config.theme,
6+
contextObject = data[context] || blog;
7+
8+
if (context === 'post' && contextObject.author && contextObject.author.image) {
9+
return config.urlFor('image', {image: contextObject.author.image}, absolute);
10+
}
11+
return null;
12+
}
13+
14+
module.exports = getAuthorImage;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var config = require('../../config');
2+
3+
function getAuthorUrl(data, absolute) {
4+
var context = data.context ? data.context[0] : null;
5+
if (data.author) {
6+
return config.urlFor('author', {author: data.author}, absolute);
7+
}
8+
if (data[context] && data[context].author) {
9+
return config.urlFor('author', {author: data[context].author}, absolute);
10+
}
11+
return null;
12+
}
13+
14+
module.exports = getAuthorUrl;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var config = require('../../config'),
2+
getUrl = require('./url');
3+
4+
function getCanonicalUrl(data) {
5+
return config.urlJoin(config.getBaseUrl(false),
6+
getUrl(data, false));
7+
}
8+
9+
module.exports = getCanonicalUrl;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var config = require('../../config');
2+
3+
function getCoverImage(data) {
4+
var context = data.context ? data.context[0] : null,
5+
blog = config.theme,
6+
contextObject = data[context] || blog;
7+
8+
if (context === 'home' || context === 'author') {
9+
if (contextObject.cover) {
10+
return config.urlFor('image', {image: contextObject.cover}, true);
11+
}
12+
} else {
13+
if (contextObject.image) {
14+
return config.urlFor('image', {image: contextObject.image}, true);
15+
}
16+
}
17+
return null;
18+
}
19+
20+
module.exports = getCoverImage;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var _ = require('lodash'),
2+
config = require('../../config');
3+
4+
function getDescription(data, root) {
5+
var description = '',
6+
context = root ? root.context : null;
7+
8+
if (data.meta_description) {
9+
description = data.meta_description;
10+
} else if (_.contains(context, 'paged')) {
11+
description = '';
12+
} else if (_.contains(context, 'home')) {
13+
description = config.theme.description;
14+
} else if (_.contains(context, 'author') && data.author) {
15+
description = data.author.bio;
16+
} else if (_.contains(context, 'tag') && data.tag) {
17+
description = data.tag.meta_description;
18+
} else if ((_.contains(context, 'post') || _.contains(context, 'page')) && data.post) {
19+
description = data.post.meta_description;
20+
}
21+
22+
return (description || '').trim();
23+
}
24+
25+
module.exports = getDescription;

core/server/data/meta/excerpt.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var downsize = require('downsize');
2+
3+
function getExcerpt(html, truncateOptions) {
4+
// Strip inline and bottom footnotes
5+
var excerpt = html.replace(/<a href="#fn.*?rel="footnote">.*?<\/a>/gi, '');
6+
excerpt = excerpt.replace(/<div class="footnotes"><ol>.*?<\/ol><\/div>/, '');
7+
// Strip other html
8+
excerpt = excerpt.replace(/<\/?[^>]+>/gi, '');
9+
excerpt = excerpt.replace(/(\r\n|\n|\r)+/gm, ' ');
10+
/*jslint regexp:false */
11+
12+
if (!truncateOptions.words && !truncateOptions.characters) {
13+
truncateOptions.words = 50;
14+
}
15+
16+
return downsize(excerpt, truncateOptions);
17+
}
18+
19+
module.exports = getExcerpt;

core/server/data/meta/index.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
var config = require('../../config'),
2+
getUrl = require('./url'),
3+
getCanonicalUrl = require('./canonical_url'),
4+
getPreviousUrl = require('./previous_url'),
5+
getNextUrl = require('./next_url'),
6+
getAuthorUrl = require('./author_url'),
7+
getRssUrl = require('./rss_url'),
8+
getTitle = require('./title'),
9+
getDescription = require('./description'),
10+
getCoverImage = require('./cover_image'),
11+
getAuthorImage = require('./author_image'),
12+
getKeywords = require('./keywords'),
13+
getPublishedDate = require('./published_date'),
14+
getModifiedDate = require('./modified_date'),
15+
getOgType = require('./og_type'),
16+
getStructuredData = require('./structured_data'),
17+
getPostSchema = require('./schema');
18+
19+
function getMetaData(data, root) {
20+
var blog = config.theme, metaData;
21+
22+
metaData = {
23+
url: getUrl(data, true),
24+
canonicalUrl: getCanonicalUrl(data),
25+
previousUrl: getPreviousUrl(data, true),
26+
nextUrl: getNextUrl(data, true),
27+
authorUrl: getAuthorUrl(data, true),
28+
rssUrl: getRssUrl(data, true),
29+
metaTitle: getTitle(data, root),
30+
metaDescription: getDescription(data, root),
31+
coverImage: getCoverImage(data, true),
32+
authorImage: getAuthorImage(data, true),
33+
keywords: getKeywords(data),
34+
publishedDate: getPublishedDate(data),
35+
modifiedDate: getModifiedDate(data),
36+
ogType: getOgType(data),
37+
blog: blog
38+
};
39+
40+
metaData.structuredData = getStructuredData(metaData);
41+
metaData.schema = getPostSchema(metaData, data);
42+
43+
return metaData;
44+
}
45+
46+
module.exports = getMetaData;

core/server/data/meta/keywords.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function getKeywords(data) {
2+
if (data.post && data.post.tags && data.post.tags.length > 0) {
3+
return data.post.tags.map(function (tag) {
4+
return tag.name;
5+
});
6+
}
7+
return null;
8+
}
9+
10+
module.exports = getKeywords;

0 commit comments

Comments
 (0)