Skip to content

Commit 2dc9166

Browse files
Merge pull request #968 from carrythebanner/truncate-descr
Add helper function for truncating strings with an ellipsis
2 parents 58785ee + e3b4144 commit 2dc9166

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

site/themes/s2b_hugo_theme/assets/js/cal/helpers.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,14 @@
8484
const startString = startDate.format(googleFormat);
8585
const endString = endDate.format(googleFormat);
8686
const calendarDates = `${startString}/${endString}`;
87-
87+
88+
const descr = $.fn.truncateString(event.details, 500) + `\n\n${event.shareable}`;
89+
8890
googleCalUrl.search = new URLSearchParams({
8991
action: "TEMPLATE",
9092
text: `shift2Bikes: ${event.title}`,
9193
location: event.address,
92-
details: `${event.details}\n\n${event.shareable}`,
94+
details: descr,
9395
dates: calendarDates,
9496
// FIX: this seems better than timezoneless but probably should be configurable.
9597
ctz: `America/Los_Angeles`
@@ -98,6 +100,15 @@
98100
return googleCalUrl.toString();
99101
};
100102

103+
$.fn.truncateString = function ( str, maxLength=250 ) {
104+
let text = str.substring(0,maxLength);
105+
if (str.length > maxLength) {
106+
// replace the last character with an ellipsis
107+
text = text.slice(0, -1) + "…";
108+
}
109+
return text;
110+
};
111+
101112
$.fn.compareTimes = function ( event1, event2 ) {
102113
if ( event1.time < event2.time ) {
103114
return -1;

site/themes/s2b_hugo_theme/assets/js/cal/main.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,7 @@ $(document).ready(function() {
6969
if (event.printdescr) {
7070
$('meta[property="og:description"]')[0].setAttribute("content", event.printdescr);
7171
} else {
72-
var descr = event.details.substring(0,250);
73-
if (event.details.length > 250) {
74-
// replace the last character with an ellipsis
75-
descr = descr.slice(0, -1) + "…";
76-
}
72+
let descr = container.truncateString(event.details, 250);
7773
$('meta[property="og:description"]')[0].setAttribute("content", descr);
7874
}
7975
document.title = event.title + " - Calendar - " + SITE_TITLE;

0 commit comments

Comments
 (0)