Skip to content

Commit

Permalink
a small change to compactify the code in use for lead text extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
flq committed Nov 24, 2024
1 parent dcb12f1 commit 2f29236
Showing 1 changed file with 3 additions and 11 deletions.
14 changes: 3 additions & 11 deletions support/extract-excerpt.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,8 @@ export function extractExcerptPlugin() {
}

function truncateToNearestWord(str, num) {
// Check if the string is shorter than the specified number of characters
if (str.length <= num) {
return str;
}
// Find the last index of a space within the specified number of characters
if (str.length <= num) return str;

let lastSpace = str.lastIndexOf(" ", num);
// If no space is found, return the specified number of characters
if (lastSpace === -1) {
return str.substring(0, num);
}
// Otherwise, return the substring up to the nearest whole word
return str.substring(0, lastSpace) + "…";
return lastSpace === -1 ? str.substring(0, num) : str.substring(0, lastSpace) + "…";
}

0 comments on commit 2f29236

Please sign in to comment.