Skip to content

Commit 6d2b6a8

Browse files
committed
Don't show a preview for a playground link if wrapped in < >
1 parent 470c859 commit 6d2b6a8

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

src/modules/playground.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,11 @@ export class PlaygroundModule extends Module {
6969
const exec = matchPlaygroundLink(msg.content);
7070
if (!exec) return;
7171
const embed = createPlaygroundEmbed(msg.author, exec);
72-
if (exec.url === 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
@@ -94,10 +96,12 @@ export class PlaygroundModule extends Module {
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.url !== content) return;
99+
if (!exec?.isWholeMatch) return;
98100
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

@@ -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: _url, query, code }: PlaygroundLinkMatch,
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: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,25 @@ import { getReferencedMessage } from './getReferencedMessage';
44

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

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})/;
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}))>?/;
88

9-
export type PlaygroundLinkMatch = { url: string; query: string; code: string };
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+
};
1017
export function matchPlaygroundLink(
1118
msg: string,
1219
): PlaygroundLinkMatch | undefined {
1320
const match = msg.match(PLAYGROUND_REGEX);
1421
if (!match) return;
15-
const [url, query, code] = match;
16-
return { url, query, code };
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 };
1726
}
1827

1928
export async function findCode(message: Message, ignoreLinks = false) {

0 commit comments

Comments
 (0)