renovate/lib/modules/platform/utils/pr-body.ts
MaronHatoum f730ff5394
fix(platform/pr-body): refactor smartTruncate function (#14915)
Co-authored-by: Rhys Arkins <rhys@arkins.net>
2022-04-04 17:49:19 +02:00

31 lines
836 B
TypeScript

const re = new RegExp(
`(?<preNotes>.*### Release Notes)(?<releaseNotes>.*)### Configuration(?<postNotes>.*)`,
's'
);
export function smartTruncate(input: string, len: number): string {
if (input.length < len) {
return input;
}
const reMatch = re.exec(input);
if (!reMatch) {
return input.substring(0, len);
}
const divider = `\n\n</details>\n\n---\n\n### Configuration`;
const preNotes = reMatch.groups?.preNotes ?? '';
const releaseNotes = reMatch.groups?.releaseNotes ?? '';
const postNotes = reMatch.groups?.postNotes ?? '';
const availableLength =
len - (preNotes.length + postNotes.length + divider.length);
if (availableLength <= 0) {
return input.substring(0, len);
} else {
return (
preNotes + releaseNotes.slice(0, availableLength) + divider + postNotes
);
}
}