Skip to content

Commit 19a7aa4

Browse files
committed
Fix #хештегов / декодирование ссылок в посте
1 parent de2fd32 commit 19a7aa4

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

modules/sender.mjs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class Sender {
3333

3434
post.text += `[**Открыть запись ВКонтакте**](https://vk.com/wall${longpoll ? postData.authorId : postData.from_id}_${postData.id})\n\n`;
3535

36-
if (postData.text) post.text += `${this.FixLinks(await this.FixHashtags(postData.text))}\n\n`;
36+
if (postData.text) post.text += `${await this.FixMarkdown(this.FixLinks(postData.text))}\n\n`;
3737

3838
if (postData.attachments) post.attachments += await this.ParseAttachments(postData.attachments);
3939

@@ -45,7 +45,7 @@ export class Sender {
4545
if (Repost) {
4646
repost.text += `\n\n>>> [**Репост записи**](https://vk.com/wall${longpoll ? Repost.authorId : Repost.from_id}_${Repost.id})\n\n`;
4747

48-
if (Repost.text) repost.text += `${this.FixLinks(await this.FixHashtags(Repost.text))}\n\n`;
48+
if (Repost.text) repost.text += `${await this.FixMarkdown(this.FixLinks(Repost.text))}\n\n`;
4949

5050
if (Repost.attachments) repost.attachments += await this.ParseAttachments(Repost.attachments);
5151
}
@@ -144,27 +144,38 @@ export class Sender {
144144
return text.replace(/(?:\[(https:\/\/vk.com\/[^]+?)\|([^]+?)])/g, "[$2]($1)").replace(/(?:\[([^]+?)\|([^]+?)])/g, "[$2](https://vk.com/$1)");
145145
}
146146

147-
async FixHashtags(text) {
147+
async FixMarkdown(text) {
148148
let fixedText = text;
149149

150150
const regExp = /#([^\s]+)@([a-zA-Z_]+)/g;
151151

152152
const matches = regExp.exec(text);
153153

154+
fixedText = fixedText
155+
.replace(/#([^\s]+)/g, (match, p1) => {
156+
if (match.match(regExp)) return match;
157+
158+
return `[#${p1}](https://vk.com/feed?section=search&q=%23${p1})`;
159+
});
160+
154161
if (matches) {
155162
const resource = await snippets.resolveResource(matches[2])
156163
.catch(() => null);
157164

158-
if (resource && resource.type === "group") fixedText = text.replace(regExp, "[#$1@$2](https://vk.com/$2/$1)");
159-
}
160-
161-
fixedText = fixedText.replace(/#([^\s]+)/g, (match, p1) => {
162-
if (match.match(regExp)) return match;
165+
if (resource && resource.type === "group") {
166+
fixedText = text.replace(regExp, (match, p1, p2) => {
167+
if (p1.match(/[a-zA-Z]+/)) return `[#${p1}@${p2}](https://vk.com/${p2}/${p1})`;
163168

164-
return `[#${p1}](https://vk.com/feed?section=search&q=%23${p1})`;
165-
});
169+
return `[#${p1}@${p2}](https://vk.com/wall-${resource.id}?q=%23${p1})`;
170+
});
171+
}
172+
}
166173

167-
return fixedText;
174+
try {
175+
return decodeURI(fixedText);
176+
} catch {
177+
return fixedText;
178+
}
168179
}
169180

170181
async Send(createdAt) {

0 commit comments

Comments
 (0)