feat: added clip description at last newline (#25)

This commit is contained in:
Ryan Hullah
2023-12-14 16:10:43 -05:00
committed by GitHub
parent b7c20771bd
commit 97a481333d
+12 -8
View File
@@ -4,6 +4,7 @@ import fetch from 'node-fetch';
/** /**
* Stylizes a markdown body into an appropriate embed message style. * Stylizes a markdown body into an appropriate embed message style.
* Remove Carriage Return character to reduce size
* Remove HTML comments (commonly added by 'Generate release notes' button) * Remove HTML comments (commonly added by 'Generate release notes' button)
* Better URL linking for common Github links: PRs, Issues, Compare * Better URL linking for common Github links: PRs, Issues, Compare
* Redundant whitespace and newlines removed, keeping at max 2 to provide space between paragraphs * Redundant whitespace and newlines removed, keeping at max 2 to provide space between paragraphs
@@ -15,6 +16,7 @@ import fetch from 'node-fetch';
*/ */
const formatDescription = (description) => { const formatDescription = (description) => {
let edit = description let edit = description
.replace(/\r/g, '')
.replace(/<!--.*?-->/gs, '') .replace(/<!--.*?-->/gs, '')
.replace( .replace(
new RegExp( new RegExp(
@@ -94,21 +96,23 @@ function getContext () {
* *
* @param {string} str * @param {string} str
* @param {number} maxLength * @param {number} maxLength
* @param {string=} url * @param {string} [url]
* @param {boolean} [clipAtLine=false]
*/ */
function limit(str, maxLength, url) { function limit(str, maxLength, url, clipAtLine) {
clipAtLine ??= false
if (str.length <= maxLength) if (str.length <= maxLength)
return str return str
let replacement = '…' let replacement = clipAtLine ? '\n…' : '…'
if (url) { if (url) {
replacement = `([${replacement}](${url}))` replacement = `${clipAtLine ? '\n' : ''}([…](${url}))`
} }
maxLength = maxLength - replacement.length maxLength = maxLength - replacement.length
str = str.substring(0, maxLength) str = str.substring(0, maxLength)
const lastWhitespace = str.search(/[^\s]*$/) const lastNewline = str.search(new RegExp(`[^${clipAtLine ? '\n' : '\s'}]*$`))
if (lastWhitespace > -1) { if (lastNewline > -1) {
str = str.substring(0, lastWhitespace) str = str.substring(0, lastNewline)
} }
return str + replacement return str + replacement
@@ -147,7 +151,7 @@ async function run () {
if (footerTimestamp == 'true') embedMsg.timestamp = new Date().toISOString(); if (footerTimestamp == 'true') embedMsg.timestamp = new Date().toISOString();
let embedSize = embedMsg.title.length + (embedMsg.footer?.text?.length ?? 0) let embedSize = embedMsg.title.length + (embedMsg.footer?.text?.length ?? 0)
embedMsg.description = limit(embedMsg.description, Math.min(getMaxDescription(), 6000 - embedSize), embedMsg.url) embedMsg.description = limit(embedMsg.description, Math.min(getMaxDescription(), 6000 - embedSize), embedMsg.url, true)
let requestBody = { let requestBody = {
embeds: [embedMsg] embeds: [embedMsg]