-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatMessage.js
69 lines (57 loc) · 1.97 KB
/
formatMessage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const { getIssueUrl } = require('./youtrack');
module.exports = formatMessage;
function formatMessage(id, commentId, data) {
const issueUrl = getIssueUrl(id);
const title = encodeHTMLEntities(cutLong(normalizeString(data.title), 200, 30));
const authorName = normalizeString(data.authorName);
const status = normalizeString(data.status);
const body = encodeHTMLEntities(cutLong(cleanText(normalizeString(data.text)), 300, 50));
let text = `*[<${issueUrl}|${id}>] ${title}*\n`;
text += `*Создал* ${authorName}\n`;
text += `*Состояние* ${status}\n`;
text += body;
if (commentId) {
const comment = data.comments.find(({id}) => id === commentId);
if (comment) {
const commentBody = encodeHTMLEntities(cutLong(cleanText(normalizeString(comment.text)), 300, 50));
const authorName = normalizeString(comment.authorName);
text += `\n*Комментарий* ${authorName}\n`;
text += commentBody;
}
}
return {
url: issueUrl,
text
};
}
function normalizeString(text) {
return String(text || '').trim();
}
// удаляем неиспользуемый плейсхолдер перед текстом
const placeholderRE = new RegExp(/(\|\*Ветка в GIT\*\|.*|\|\*Функциональные требования\*\|.*)/g);
// удаляем [](image.png)
const imageRE = new RegExp(/!\[]\(.*?\)/g);
// убираем пустые строки
const doubleEmptyLinesRE = new RegExp(/\n+/g);
function cleanText(text) {
return text
.replace(placeholderRE, '')
.replace(imageRE, '')
.replace(doubleEmptyLinesRE, '\n')
.trim();
}
function encodeHTMLEntities(text) {
return text
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/&/g, '&')
}
const spaceRE = /\s/;
function cutLong(str, maxLength, pad) {
if (str.length > maxLength) {
const spacePosition = str.substr(maxLength, pad).search(spaceRE);
const position = maxLength + (spacePosition === -1 ? 0 : spacePosition);
return str.substr(0, position) + '...';
}
return str;
}