@tryghost/helpers 1.1.73 → 1.1.75
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/helpers.js +62 -93
- package/es/helpers.js.map +1 -1
- package/package.json +6 -6
- package/umd/helpers.min.js +1 -1
- package/umd/helpers.min.js.map +1 -1
package/es/helpers.js
CHANGED
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
import 'core-js/modules/es6.regexp.split.js';
|
|
2
|
-
import 'core-js/modules/es6.regexp.replace.js';
|
|
3
|
-
import 'core-js/modules/es6.regexp.match.js';
|
|
4
|
-
import 'core-js/modules/es6.function.name.js';
|
|
5
|
-
import 'core-js/modules/es6.array.slice.js';
|
|
6
|
-
|
|
7
1
|
/**
|
|
8
2
|
* The base implementation of `_.findIndex` and `_.findLastIndex` without
|
|
9
3
|
* support for iteratee shorthands.
|
|
@@ -3483,14 +3477,13 @@ function trim(string, chars, guard) {
|
|
|
3483
3477
|
* @param visibility
|
|
3484
3478
|
* @returns {*}
|
|
3485
3479
|
*/
|
|
3486
|
-
|
|
3487
|
-
var parse = function parse(visibility) {
|
|
3480
|
+
const parse = visibility => {
|
|
3488
3481
|
if (!visibility) {
|
|
3489
3482
|
return ['public'];
|
|
3490
3483
|
}
|
|
3491
|
-
|
|
3492
3484
|
return map(visibility.split(','), trim);
|
|
3493
3485
|
};
|
|
3486
|
+
|
|
3494
3487
|
/**
|
|
3495
3488
|
* Filter resources by visibility.
|
|
3496
3489
|
*
|
|
@@ -3502,31 +3495,29 @@ var parse = function parse(visibility) {
|
|
|
3502
3495
|
* @param {Function} [fn] - function to apply to each item before returning
|
|
3503
3496
|
* @returns {Array|Object} filtered items
|
|
3504
3497
|
*/
|
|
3505
|
-
|
|
3506
|
-
var filter = function filter(items, visibility, fn) {
|
|
3498
|
+
const filter = (items, visibility, fn) => {
|
|
3507
3499
|
if (isFunction(visibility)) {
|
|
3508
3500
|
fn = visibility;
|
|
3509
3501
|
visibility = null;
|
|
3510
3502
|
}
|
|
3503
|
+
const memo = isArray$1(items) ? [] : {};
|
|
3504
|
+
const visArray = isArray$1(visibility) ? visibility : parse(visibility);
|
|
3511
3505
|
|
|
3512
|
-
|
|
3513
|
-
|
|
3514
|
-
|
|
3515
|
-
var defaultVisibility = 'public';
|
|
3516
|
-
var returnByDefault = includes(visArray, defaultVisibility); // We don't want to change the structure of what is returned
|
|
3506
|
+
// Fallback behaviour for items that don't have visibility set on them
|
|
3507
|
+
const defaultVisibility = 'public';
|
|
3508
|
+
const returnByDefault = includes(visArray, defaultVisibility);
|
|
3517
3509
|
|
|
3510
|
+
// We don't want to change the structure of what is returned
|
|
3518
3511
|
return reduce(items, function (accumulator, item, key) {
|
|
3519
3512
|
// If the item has visibility, check to see if it matches, else if there's no visibility check for a match with the default visibility
|
|
3520
3513
|
if (includes(visArray, 'all') || item.visibility && includes(visArray, item.visibility) || !item.visibility && returnByDefault) {
|
|
3521
|
-
|
|
3522
|
-
|
|
3514
|
+
const newItem = fn ? fn(item) : item;
|
|
3523
3515
|
if (isArray$1(items)) {
|
|
3524
3516
|
accumulator.push(newItem);
|
|
3525
3517
|
} else {
|
|
3526
3518
|
accumulator[key] = newItem;
|
|
3527
3519
|
}
|
|
3528
3520
|
}
|
|
3529
|
-
|
|
3530
3521
|
return accumulator;
|
|
3531
3522
|
}, memo);
|
|
3532
3523
|
};
|
|
@@ -3548,24 +3539,20 @@ var visibility = /*#__PURE__*/Object.freeze({
|
|
|
3548
3539
|
function countWords(text) {
|
|
3549
3540
|
if (!text) {
|
|
3550
3541
|
return 0;
|
|
3551
|
-
}
|
|
3552
|
-
|
|
3553
|
-
|
|
3542
|
+
}
|
|
3543
|
+
// protect against Handlebars.SafeString
|
|
3554
3544
|
if (Object.prototype.hasOwnProperty.call(text, 'string')) {
|
|
3555
3545
|
text = text.string;
|
|
3556
3546
|
}
|
|
3557
|
-
|
|
3558
3547
|
text = text.replace(/<(.|\n)*?>/g, ' '); // strip any HTML tags
|
|
3559
3548
|
|
|
3560
|
-
|
|
3561
|
-
|
|
3562
|
-
|
|
3563
|
-
|
|
3564
|
-
|
|
3549
|
+
const pattern = /[a-zA-ZÀ-ÿ0-9_\u0392-\u03c9\u0410-\u04F9]+|[\u4E00-\u9FFF\u3400-\u4dbf\uf900-\ufaff\u3040-\u309f\uac00-\ud7af]+/g;
|
|
3550
|
+
const RTLPattern = /([\u0600-\u06ff]+|[\u0591-\u05F4]+)/g;
|
|
3551
|
+
const match = text.match(pattern) || text.match(RTLPattern);
|
|
3552
|
+
let count = 0;
|
|
3565
3553
|
if (match === null) {
|
|
3566
3554
|
return count;
|
|
3567
3555
|
}
|
|
3568
|
-
|
|
3569
3556
|
for (var i = 0; i < match.length; i += 1) {
|
|
3570
3557
|
if (match[i].charCodeAt(0) >= 0x4e00) {
|
|
3571
3558
|
count += match[i].length;
|
|
@@ -3573,7 +3560,6 @@ function countWords(text) {
|
|
|
3573
3560
|
count += 1;
|
|
3574
3561
|
}
|
|
3575
3562
|
}
|
|
3576
|
-
|
|
3577
3563
|
return count;
|
|
3578
3564
|
}
|
|
3579
3565
|
|
|
@@ -3586,28 +3572,28 @@ function countWords(text) {
|
|
|
3586
3572
|
function countImages(html) {
|
|
3587
3573
|
if (!html) {
|
|
3588
3574
|
return 0;
|
|
3589
|
-
}
|
|
3590
|
-
|
|
3591
|
-
|
|
3575
|
+
}
|
|
3576
|
+
// protect against Handlebars.SafeString
|
|
3592
3577
|
if (Object.prototype.hasOwnProperty.call(html, 'string')) {
|
|
3593
3578
|
html = html.string;
|
|
3594
3579
|
}
|
|
3595
|
-
|
|
3596
3580
|
return (html.match(/<img(.|\n)*?>/g) || []).length;
|
|
3597
3581
|
}
|
|
3598
3582
|
|
|
3599
3583
|
function estimatedReadingTimeInMinutes(_ref) {
|
|
3600
|
-
|
|
3601
|
-
|
|
3602
|
-
|
|
3603
|
-
|
|
3604
|
-
|
|
3605
|
-
|
|
3584
|
+
let {
|
|
3585
|
+
wordCount,
|
|
3586
|
+
imageCount
|
|
3587
|
+
} = _ref;
|
|
3588
|
+
const wordsPerMinute = 275;
|
|
3589
|
+
const wordsPerSecond = wordsPerMinute / 60;
|
|
3590
|
+
let readingTimeSeconds = wordCount / wordsPerSecond;
|
|
3591
|
+
|
|
3592
|
+
// add 12 seconds for the first image, 11 for the second, etc. limiting at 3
|
|
3606
3593
|
for (var i = 12; i > 12 - imageCount; i -= 1) {
|
|
3607
3594
|
readingTimeSeconds += Math.max(i, 3);
|
|
3608
3595
|
}
|
|
3609
|
-
|
|
3610
|
-
var readingTimeMinutes = Math.round(readingTimeSeconds / 60);
|
|
3596
|
+
let readingTimeMinutes = Math.round(readingTimeSeconds / 60);
|
|
3611
3597
|
return readingTimeMinutes;
|
|
3612
3598
|
}
|
|
3613
3599
|
/**
|
|
@@ -3622,17 +3608,14 @@ function readingMinutes(html, additionalImages) {
|
|
|
3622
3608
|
if (!html) {
|
|
3623
3609
|
return '';
|
|
3624
3610
|
}
|
|
3625
|
-
|
|
3626
|
-
|
|
3627
|
-
var wordCount = countWords(html);
|
|
3628
|
-
|
|
3611
|
+
let imageCount = countImages(html);
|
|
3612
|
+
let wordCount = countWords(html);
|
|
3629
3613
|
if (additionalImages) {
|
|
3630
3614
|
imageCount += additionalImages;
|
|
3631
3615
|
}
|
|
3632
|
-
|
|
3633
3616
|
return estimatedReadingTimeInMinutes({
|
|
3634
|
-
wordCount
|
|
3635
|
-
imageCount
|
|
3617
|
+
wordCount,
|
|
3618
|
+
imageCount
|
|
3636
3619
|
});
|
|
3637
3620
|
}
|
|
3638
3621
|
|
|
@@ -3647,29 +3630,23 @@ function readingMinutes(html, additionalImages) {
|
|
|
3647
3630
|
*/
|
|
3648
3631
|
|
|
3649
3632
|
function readingTime (post) {
|
|
3650
|
-
|
|
3651
|
-
|
|
3652
|
-
|
|
3653
|
-
|
|
3633
|
+
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
3634
|
+
const minuteStr = typeof options.minute === 'string' ? options.minute : '1 min read';
|
|
3635
|
+
const minutesStr = typeof options.minutes === 'string' ? options.minutes : '% min read';
|
|
3654
3636
|
if (!post.html && !post.reading_time) {
|
|
3655
3637
|
return '';
|
|
3656
3638
|
}
|
|
3657
|
-
|
|
3658
|
-
var imageCount = 0;
|
|
3659
|
-
|
|
3639
|
+
let imageCount = 0;
|
|
3660
3640
|
if (post.feature_image) {
|
|
3661
3641
|
imageCount += 1;
|
|
3662
3642
|
}
|
|
3663
|
-
|
|
3664
|
-
|
|
3665
|
-
var readingTime = '';
|
|
3666
|
-
|
|
3643
|
+
const time = post.reading_time || readingMinutes(post.html, imageCount);
|
|
3644
|
+
let readingTime = '';
|
|
3667
3645
|
if (time <= 1) {
|
|
3668
3646
|
readingTime = minuteStr;
|
|
3669
3647
|
} else {
|
|
3670
3648
|
readingTime = minutesStr.replace('%', time);
|
|
3671
3649
|
}
|
|
3672
|
-
|
|
3673
3650
|
return readingTime;
|
|
3674
3651
|
}
|
|
3675
3652
|
|
|
@@ -4351,36 +4328,29 @@ var zip$1 = zip;
|
|
|
4351
4328
|
* @param {function} [options.fn] - function to call on each tag, default returns tag.name
|
|
4352
4329
|
* @returns {String|*} processed tags, comma separated names by default
|
|
4353
4330
|
*/
|
|
4354
|
-
|
|
4355
4331
|
function tags (data) {
|
|
4356
|
-
|
|
4357
|
-
|
|
4358
|
-
|
|
4359
|
-
|
|
4360
|
-
|
|
4361
|
-
|
|
4362
|
-
|
|
4363
|
-
|
|
4364
|
-
|
|
4365
|
-
|
|
4366
|
-
|
|
4367
|
-
return tag.name;
|
|
4368
|
-
};
|
|
4369
|
-
|
|
4332
|
+
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
4333
|
+
let output = '';
|
|
4334
|
+
let separator = options.separator ? options.separator : '';
|
|
4335
|
+
let prefix = options.prefix ? options.prefix : '';
|
|
4336
|
+
let suffix = options.suffix ? options.suffix : '';
|
|
4337
|
+
let limit = options.limit ? parseInt(options.limit, 10) : undefined;
|
|
4338
|
+
let from = options.from ? parseInt(options.from, 10) : 1;
|
|
4339
|
+
let to = options.to ? parseInt(options.to, 10) : undefined;
|
|
4340
|
+
let visibilityArr = parse(options.visibility);
|
|
4341
|
+
let fallback = options.fallback ? isArray$1(options.fallback) ? options.fallback : [options.fallback] : undefined;
|
|
4342
|
+
let displayFn = options.fn ? options.fn : tag => tag.name;
|
|
4370
4343
|
if (data.tags && data.tags.length) {
|
|
4371
4344
|
output = filter(data.tags, visibilityArr, displayFn);
|
|
4372
|
-
|
|
4373
4345
|
if (size(output) === 0 && fallback) {
|
|
4374
4346
|
output = filter(fallback, visibilityArr, displayFn);
|
|
4375
4347
|
}
|
|
4376
|
-
|
|
4377
4348
|
from -= 1; // From uses 1-indexed, but array uses 0-indexed.
|
|
4378
|
-
|
|
4379
4349
|
to = to || limit + from || output.length;
|
|
4380
4350
|
output = output.slice(from, to);
|
|
4381
|
-
}
|
|
4382
|
-
|
|
4351
|
+
}
|
|
4383
4352
|
|
|
4353
|
+
// If we have a result from the filtering process...
|
|
4384
4354
|
if (size(output) > 0) {
|
|
4385
4355
|
// Check to see if options.fn returned a string, or something else
|
|
4386
4356
|
if (isString(output[0])) {
|
|
@@ -4391,26 +4361,25 @@ function tags (data) {
|
|
|
4391
4361
|
// Else, operate on the array, and return an array
|
|
4392
4362
|
if (separator) {
|
|
4393
4363
|
// If we have a separator, use lodash to make pairs of items & separators
|
|
4394
|
-
output = zip$1(output, fill(Array(output.length), separator));
|
|
4395
|
-
|
|
4364
|
+
output = zip$1(output, fill(Array(output.length), separator));
|
|
4365
|
+
// Flatten our pairs, and remove the final separator
|
|
4396
4366
|
output = flatten(output).slice(0, -1);
|
|
4397
|
-
}
|
|
4398
|
-
|
|
4399
|
-
|
|
4400
|
-
output = concat(prefix, output, suffix); // Remove any falsy items after all that (i.e. if prefix/suffix were empty);
|
|
4367
|
+
}
|
|
4401
4368
|
|
|
4369
|
+
// Add our prefix and suffix
|
|
4370
|
+
output = concat(prefix, output, suffix);
|
|
4371
|
+
// Remove any falsy items after all that (i.e. if prefix/suffix were empty);
|
|
4402
4372
|
output = compact(output);
|
|
4403
4373
|
}
|
|
4404
4374
|
}
|
|
4405
|
-
|
|
4406
4375
|
return output;
|
|
4407
4376
|
}
|
|
4408
4377
|
|
|
4409
|
-
|
|
4410
|
-
countImages
|
|
4411
|
-
countWords
|
|
4412
|
-
visibility
|
|
4413
|
-
readingMinutes
|
|
4378
|
+
const utils = {
|
|
4379
|
+
countImages,
|
|
4380
|
+
countWords,
|
|
4381
|
+
visibility,
|
|
4382
|
+
readingMinutes
|
|
4414
4383
|
};
|
|
4415
4384
|
|
|
4416
4385
|
export { readingTime, tags, utils };
|