Skip to content

Commit da1f2c0

Browse files
authored
Merge pull request #218 from Retsam/suppress-embed
Don't show a preview for a playground link if wrapped in < >
2 parents c332b60 + 6d2b6a8 commit da1f2c0

File tree

2 files changed

+47
-24
lines changed

2 files changed

+47
-24
lines changed

src/modules/playground.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import { TS_BLUE } from '../env';
1616
import {
1717
makeCodeBlock,
1818
findCode,
19-
PLAYGROUND_REGEX,
20-
truncate,
19+
matchPlaygroundLink,
20+
PlaygroundLinkMatch,
2121
} from '../util/codeBlocks';
2222
import { LimitedSizeMap } from '../util/limitedSizeMap';
2323
import {
@@ -66,12 +66,14 @@ export class PlaygroundModule extends Module {
6666
async onPlaygroundLinkMessage(msg: Message) {
6767
if (msg.author.bot) return;
6868
if (msg.content[0] === '!') return;
69-
const exec = PLAYGROUND_REGEX.exec(msg.content);
69+
const exec = matchPlaygroundLink(msg.content);
7070
if (!exec) return;
7171
const embed = createPlaygroundEmbed(msg.author, exec);
72-
if (exec[0] === msg.content && !isHelpChannel(msg.channel)) {
72+
if (exec.isWholeMatch && !isHelpChannel(msg.channel)) {
7373
// Message only contained the link
74-
await sendWithMessageOwnership(msg, { embeds: [embed] });
74+
await sendWithMessageOwnership(msg, {
75+
embeds: [embed],
76+
});
7577
await msg.delete();
7678
} else {
7779
// Message also contained other characters
@@ -90,21 +92,23 @@ export class PlaygroundModule extends Module {
9092
const attachment = msg.attachments.find(a => a.name === 'message.txt');
9193
if (msg.author.bot || !attachment) return;
9294
const content = await fetch(attachment.url).then(r => r.text());
93-
const exec = PLAYGROUND_REGEX.exec(content);
95+
const exec = matchPlaygroundLink(content);
9496
// By default, if you write a message in the box and then paste a long
9597
// playground link, it will only put the paste in message.txt and will
9698
// put the rest of the message in msg.content
97-
if (!exec || exec[0] !== content) return;
98-
const shortenedUrl = await shortenPlaygroundLink(exec[0]);
99+
if (!exec?.isWholeMatch) return;
100+
const shortenedUrl = await shortenPlaygroundLink(exec.url);
99101
const embed = createPlaygroundEmbed(msg.author, exec, shortenedUrl);
100-
await sendWithMessageOwnership(msg, { embeds: [embed] });
102+
await sendWithMessageOwnership(msg, {
103+
embeds: [embed],
104+
});
101105
if (!msg.content) await msg.delete();
102106
}
103107

104108
@listener({ event: 'messageUpdate' })
105109
async onLongFix(_oldMsg: Message, msg: Message) {
106110
if (msg.partial) await msg.fetch();
107-
const exec = PLAYGROUND_REGEX.exec(msg.content);
111+
const exec = matchPlaygroundLink(msg.content);
108112
if (msg.author.bot || !this.editedLongLink.has(msg.id) || exec) return;
109113
const botMsg = this.editedLongLink.get(msg.id);
110114
// Edit the message to only have the embed and not the "please edit your message" message
@@ -119,7 +123,7 @@ export class PlaygroundModule extends Module {
119123
// Take care when messing with the truncation, it's extremely finnicky
120124
function createPlaygroundEmbed(
121125
author: User,
122-
[_url, query, code]: RegExpExecArray,
126+
{ url: _url, query, code, isEscaped }: PlaygroundLinkMatch,
123127
url: string = _url,
124128
) {
125129
const embed = new MessageEmbed()
@@ -179,13 +183,16 @@ function createPlaygroundEmbed(
179183
formattedSection.replace(/^\s*\n|\n\s*$/g, '') +
180184
(prettyEndChar === pretty.length ? '' : '\n...');
181185

182-
if (!startLine && !endLine) {
183-
embed.setFooter(
184-
'You can choose specific lines to embed by selecting them before copying the link.',
185-
);
186+
if (!isEscaped) {
187+
embed.setDescription('**Preview:**' + makeCodeBlock(content));
188+
if (!startLine && !endLine) {
189+
embed.setFooter(
190+
'You can choose specific lines to embed by selecting them before copying the link.',
191+
);
192+
}
186193
}
187194

188-
return embed.setDescription('**Preview:**' + makeCodeBlock(content));
195+
return embed;
189196
}
190197

191198
async function shortenPlaygroundLink(url: string) {

src/util/codeBlocks.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,26 @@ import { getReferencedMessage } from './getReferencedMessage';
44

55
const CODEBLOCK_REGEX = /```(?:ts|typescript|js|javascript)?\n([\s\S]+)```/;
66

7-
export const PLAYGROUND_REGEX = /https?:\/\/(?:www\.)?(?:typescriptlang|staging-typescript)\.org\/(?:[a-z]{2,3}\/)?(?:play|dev\/bug-workbench)(?:\/index\.html)?\/?(\??(?:\w+=[^\s#&]*)?(?:\&\w+=[^\s#&]*)*)#code\/([\w\-%+_]+={0,4})/;
7+
const PLAYGROUND_REGEX = /<?(https?:\/\/(?:www\.)?(?:typescriptlang|staging-typescript)\.org\/(?:[a-z]{2,3}\/)?(?:play|dev\/bug-workbench)(?:\/index\.html)?\/?(\??(?:\w+=[^\s#&]*)?(?:\&\w+=[^\s#&]*)*)#code\/([\w\-%+_]+={0,4}))>?/;
8+
9+
export type PlaygroundLinkMatch = {
10+
url: string;
11+
query: string;
12+
code: string;
13+
isWholeMatch: boolean;
14+
/* Is the url wrapped in < > ? */
15+
isEscaped: boolean;
16+
};
17+
export function matchPlaygroundLink(
18+
msg: string,
19+
): PlaygroundLinkMatch | undefined {
20+
const match = msg.match(PLAYGROUND_REGEX);
21+
if (!match) return;
22+
const [possiblyEscapedUrl, url, query, code] = match;
23+
const isWholeMatch = msg === possiblyEscapedUrl;
24+
const isEscaped = possiblyEscapedUrl.length === url.length + 2;
25+
return { url, query, code, isWholeMatch, isEscaped };
26+
}
827

928
export async function findCode(message: Message, ignoreLinks = false) {
1029
const codeInMessage = await findCodeInMessage(message, ignoreLinks);
@@ -43,15 +62,12 @@ async function findCodeInMessage(msg: Message, ignoreLinks = false) {
4362

4463
if (ignoreLinks) return;
4564

46-
const match = content.match(PLAYGROUND_REGEX);
47-
if (match) {
48-
return decompressFromEncodedURIComponent(match[2]);
49-
}
65+
const codeSources = [content, ...embeds.map(({ url }) => url)];
5066

51-
for (const embed of embeds) {
52-
const match = embed.url?.match(PLAYGROUND_REGEX);
67+
for (const code of codeSources) {
68+
const match = code && matchPlaygroundLink(code);
5369
if (match) {
54-
return decompressFromEncodedURIComponent(match[2]);
70+
return decompressFromEncodedURIComponent(match.code);
5571
}
5672
}
5773
}

0 commit comments

Comments
 (0)