Skip to content

Commit 97a4813

Browse files
authoredDec 14, 2023
feat: added clip description at last newline (#25)
1 parent b7c2077 commit 97a4813

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed
 

‎index.js

+12-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import fetch from 'node-fetch';
44

55
/**
66
* Stylizes a markdown body into an appropriate embed message style.
7+
* Remove Carriage Return character to reduce size
78
* Remove HTML comments (commonly added by 'Generate release notes' button)
89
* Better URL linking for common Github links: PRs, Issues, Compare
910
* Redundant whitespace and newlines removed, keeping at max 2 to provide space between paragraphs
@@ -15,6 +16,7 @@ import fetch from 'node-fetch';
1516
*/
1617
const formatDescription = (description) => {
1718
let edit = description
19+
.replace(/\r/g, '')
1820
.replace(/<!--.*?-->/gs, '')
1921
.replace(
2022
new RegExp(
@@ -94,21 +96,23 @@ function getContext () {
9496
*
9597
* @param {string} str
9698
* @param {number} maxLength
97-
* @param {string=} url
99+
* @param {string} [url]
100+
* @param {boolean} [clipAtLine=false]
98101
*/
99-
function limit(str, maxLength, url) {
102+
function limit(str, maxLength, url, clipAtLine) {
103+
clipAtLine ??= false
100104
if (str.length <= maxLength)
101105
return str
102-
let replacement = '…'
106+
let replacement = clipAtLine ? '\n…' : '…'
103107
if (url) {
104-
replacement = `([${replacement}](${url}))`
108+
replacement = `${clipAtLine ? '\n' : ''}([…](${url}))`
105109
}
106110
maxLength = maxLength - replacement.length
107111
str = str.substring(0, maxLength)
108112

109-
const lastWhitespace = str.search(/[^\s]*$/)
110-
if (lastWhitespace > -1) {
111-
str = str.substring(0, lastWhitespace)
113+
const lastNewline = str.search(new RegExp(`[^${clipAtLine ? '\n' : '\s'}]*$`))
114+
if (lastNewline > -1) {
115+
str = str.substring(0, lastNewline)
112116
}
113117

114118
return str + replacement
@@ -147,7 +151,7 @@ async function run () {
147151
if (footerTimestamp == 'true') embedMsg.timestamp = new Date().toISOString();
148152

149153
let embedSize = embedMsg.title.length + (embedMsg.footer?.text?.length ?? 0)
150-
embedMsg.description = limit(embedMsg.description, Math.min(getMaxDescription(), 6000 - embedSize), embedMsg.url)
154+
embedMsg.description = limit(embedMsg.description, Math.min(getMaxDescription(), 6000 - embedSize), embedMsg.url, true)
151155

152156
let requestBody = {
153157
embeds: [embedMsg]

0 commit comments

Comments
 (0)
Please sign in to comment.