forked from TryGhost/Ghost
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request TryGhost#6370 from jtwebman/meta_data_to_functions
Started moving meta data fetching to functions.
- Loading branch information
Showing
43 changed files
with
1,715 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.