UNPKG

@paroicms/server

Version:
26 lines 823 B
export function toExcerptString(rawText) { return rawText.replaceAll(/[.…,;:!?\s]+/g, replacer).trim(); } function replacer(match) { return match === " " ? " " : " "; } export function truncExcerptToWord(input, maxLength) { const hasEllipsis = input.charAt(input.length - 1) === "…"; const excerpt = toExcerptString(input); let sepIndex = maxLength; if (sepIndex >= excerpt.length) { sepIndex = excerpt.length; } else { for (sepIndex = maxLength; sepIndex >= 0; --sepIndex) { if (excerpt.charAt(sepIndex) === " ") break; } } const result = excerpt.substring(0, sepIndex); if (hasEllipsis || sepIndex < excerpt.length) { return `${result}…`; } return result; } //# sourceMappingURL=excerpt.helpers.js.map