Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

slice theme helper #21742

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions ghost/core/core/frontend/helpers/slice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// # slice helper
//
// Usage examples, for example in .hbs theme templates:
//
// {{#match (slice label start=0 end=2) "@ " }}
// <li><a href="{{url}}">{{ slice label start=2 }}</a</li>
// {{/match}}
//
// Or:
//
// {{ slice "Testing [*]" start=-3 }} => Outputs: [*]

const {SafeString} = require('../services/handlebars');

module.exports = function slice(text, options) {
options = options || {};
options.hash = options.hash || {};

let start = options.hash.start;
let end = options.hash.end;

if (typeof text !== 'string' || typeof start !== 'number') {
return '';
}

let result = (typeof end === 'number') ?
text.slice(start, end) : text.slice(start);

return new SafeString(result);
};
90 changes: 90 additions & 0 deletions ghost/core/test/unit/frontend/helpers/slice.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
const should = require('should');
const handlebars = require('../../../../core/frontend/services/theme-engine/engine').handlebars;

const slice = require('../../../../core/frontend/helpers/slice');
const match = require('../../../../core/frontend/helpers/match');

const configUtils = require('../../../utils/configUtils');

let defaultGlobals;

function compile(templateString) {
const template = handlebars.compile(templateString);
template.with = (locals = {}, globals) => {
globals = globals || defaultGlobals;

return template(locals, globals);
};

return template;
}

describe('{{slice}} helper', function () {
before(function () {
handlebars.registerHelper('slice', slice);
handlebars.registerHelper('match', match);
configUtils.config.set('url', 'https://siteurl.com');

defaultGlobals = {
data: {
site: {
url: configUtils.config.get('url')
}
}
};
});

it('can correctly slice nothing', function () {
compile('{{slice}}')
.with({})
.should.eql('');
});

it('can correctly slice things that resolve to empty', function () {
compile('{{slice tag.slug start=2}}')
.with({tag: {}})
.should.eql('');
});

it('can slice simple strings', function () {
compile('{{slice "@ Mail" start=2}}')
.with({})
.should.eql('Mail');
});

it('can slice simple strings with end', function () {
compile('{{slice "@ Mail" start=0 end=2}}')
.with({})
.should.eql('@ ');
});

it('can slice simple strings with end', function () {
compile('{{slice @site.url start=0 end=5}}')
.with({})
.should.eql('https');
});

it('can silently return empty string with non integer start', function () {
compile('{{slice "- Test" start="12"}}')
.with({})
.should.eql('');
});

it('can ignore non integer end', function () {
compile('{{slice "- Test" start=2 end="end"}}')
.with({})
.should.eql('Test');
});

it('can work with minus start value', function () {
compile('{{slice "Test ->" start=-2}}')
.with({})
.should.eql('->');
});

it('can be used as sub expression', function () {
compile('{{#match (slice "- Test" start=0 end=2) "- "}}true{{else}}false{{/match}}')
.with({})
.should.eql('true');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ describe('Helpers', function () {
const ghostHelpers = [
'asset', 'authors', 'body_class', 'cancel_link', 'concat', 'content', 'content_api_key', 'content_api_url', 'date', 'encode', 'excerpt', 'facebook_url', 'foreach', 'get',
'ghost_foot', 'ghost_head', 'has', 'img_url', 'is', 'link', 'link_class', 'meta_description', 'meta_title', 'navigation',
'next_post', 'page_url', 'pagination', 'plural', 'post_class', 'prev_post', 'price', 'raw', 'reading_time', 't', 'tags', 'title','total_members', 'total_paid_members', 'twitter_url',
'next_post', 'page_url', 'pagination', 'plural', 'post_class', 'prev_post', 'price', 'raw', 'reading_time', 'slice', 't',
'tags', 'title','total_members', 'total_paid_members', 'twitter_url',
'url', 'comment_count', 'collection', 'recommendations', 'readable_url'
];
const experimentalHelpers = ['match', 'tiers', 'comments', 'search'];
Expand Down