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