0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-04-08 02:52:39 -05:00

Primary tag (#8669)

refs #8668

- return primary tag from Post API
- support primary tag in URL
This commit is contained in:
Hannah Wolfe 2017-07-31 13:00:03 +04:00 committed by Katharina Irrgang
parent 4333604480
commit 353e11dafb
7 changed files with 23 additions and 11 deletions

View file

@ -27,7 +27,8 @@
"preview": "p",
"private": "private",
"subscribe": "subscribe",
"amp": "amp"
"amp": "amp",
"primaryTagFallback": "all"
},
"slugs": {
"reserved": ["admin", "app", "apps", "archive", "archives", "categories",

View file

@ -53,7 +53,7 @@
"defaultValue": "/:slug/",
"validations": {
"matches": "^(\/:?[a-z0-9_-]+){1,5}\/$",
"matches": "(:id|:slug|:year|:month|:day|:author)",
"matches": "(:id|:slug|:year|:month|:day|:author|:primary_tag)",
"notContains": "/ghost/"
}
},

View file

@ -508,6 +508,11 @@ Post = ghostBookshelf.Model.extend({
attrs.author = attrs.author || attrs.author_id;
delete attrs.author_id;
}
// If the current column settings allow it...
if (!options.columns || (options.columns && options.columns.indexOf('primary_tag') > -1)) {
// ... attach a computed property of primary_tag which is the first tag or null
attrs.primary_tag = attrs.tags && attrs.tags.length > 0 ? attrs.tags[0] : null;
}
if (!options.columns || (options.columns && options.columns.indexOf('url') > -1)) {
attrs.url = utils.url.urlPathForPost(attrs);

View file

@ -174,14 +174,16 @@ function createUrl(urlPath, absolute, secure) {
function urlPathForPost(post) {
var output = '',
permalinks = settingsCache.get('permalinks'),
primaryTagFallback = config.get('routeKeywords').primaryTagFallback,
publishedAtMoment = moment.tz(post.published_at || Date.now(), settingsCache.get('active_timezone')),
tags = {
year: function () { return publishedAtMoment.format('YYYY'); },
month: function () { return publishedAtMoment.format('MM'); },
day: function () { return publishedAtMoment.format('DD'); },
year: function () { return publishedAtMoment.format('YYYY'); },
month: function () { return publishedAtMoment.format('MM'); },
day: function () { return publishedAtMoment.format('DD'); },
author: function () { return post.author.slug; },
slug: function () { return post.slug; },
id: function () { return post.id; }
primary_tag: function () { return post.primary_tag ? post.primary_tag.slug : primaryTagFallback; },
slug: function () { return post.slug; },
id: function () { return post.id; }
};
if (post.page) {
@ -191,7 +193,7 @@ function urlPathForPost(post) {
}
// replace tags like :slug or :year with actual values
output = output.replace(/(:[a-z]+)/g, function (match) {
output = output.replace(/(:[a-z_]+)/g, function (match) {
if (_.has(tags, match.substr(1))) {
return tags[match.substr(1)]();
}

View file

@ -35,6 +35,7 @@ describe('Configuration API', function () {
author: 'author',
page: 'page',
preview: 'p',
primaryTagFallback: 'all',
private: 'private',
subscribe: 'subscribe',
amp: 'amp'

View file

@ -104,7 +104,10 @@ describe('Schedules API', function () {
api.schedules.getScheduledPosts()
.then(function (result) {
result.posts.length.should.eql(5);
Object.keys(result.posts[0].toJSON()).should.eql(['id', 'published_at', 'created_at', 'author', 'url', 'comment_id']);
Object.keys(result.posts[0].toJSON()).should.eql(
// @TODO: the computed properties shouldn't be appearing here! Needs a fix
['id', 'published_at', 'created_at', 'author', 'primary_tag', 'url', 'comment_id']
);
done();
})
.catch(done);

View file

@ -23,8 +23,8 @@ var _ = require('lodash'),
post: _(schema.posts).keys()
// does not return all formats by default
.without('mobiledoc', 'amp', 'plaintext')
// swaps author_id to author, and always returns a computed 'url' property
.without('author_id').concat('author', 'url', 'comment_id')
// swaps author_id to author, and always returns computed properties: url, comment_id, primary_tag
.without('author_id').concat('author', 'url', 'comment_id', 'primary_tag')
.value(),
// User API always removes the password field
user: _(schema.users).keys().without('password').without('ghost_auth_access_token').value(),