Skip to content

Commit 470c859

Browse files
committed
Refactor playground matching logic to encapsulate the regex
1 parent c332b60 commit 470c859

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

src/modules/playground.ts

Lines changed: 9 additions & 9 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,10 +66,10 @@ 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.url === msg.content && !isHelpChannel(msg.channel)) {
7373
// Message only contained the link
7474
await sendWithMessageOwnership(msg, { embeds: [embed] });
7575
await msg.delete();
@@ -90,12 +90,12 @@ export class PlaygroundModule extends Module {
9090
const attachment = msg.attachments.find(a => a.name === 'message.txt');
9191
if (msg.author.bot || !attachment) return;
9292
const content = await fetch(attachment.url).then(r => r.text());
93-
const exec = PLAYGROUND_REGEX.exec(content);
93+
const exec = matchPlaygroundLink(content);
9494
// By default, if you write a message in the box and then paste a long
9595
// playground link, it will only put the paste in message.txt and will
9696
// put the rest of the message in msg.content
97-
if (!exec || exec[0] !== content) return;
98-
const shortenedUrl = await shortenPlaygroundLink(exec[0]);
97+
if (!exec || exec.url !== content) return;
98+
const shortenedUrl = await shortenPlaygroundLink(exec.url);
9999
const embed = createPlaygroundEmbed(msg.author, exec, shortenedUrl);
100100
await sendWithMessageOwnership(msg, { embeds: [embed] });
101101
if (!msg.content) await msg.delete();
@@ -104,7 +104,7 @@ export class PlaygroundModule extends Module {
104104
@listener({ event: 'messageUpdate' })
105105
async onLongFix(_oldMsg: Message, msg: Message) {
106106
if (msg.partial) await msg.fetch();
107-
const exec = PLAYGROUND_REGEX.exec(msg.content);
107+
const exec = matchPlaygroundLink(msg.content);
108108
if (msg.author.bot || !this.editedLongLink.has(msg.id) || exec) return;
109109
const botMsg = this.editedLongLink.get(msg.id);
110110
// Edit the message to only have the embed and not the "please edit your message" message
@@ -119,7 +119,7 @@ export class PlaygroundModule extends Module {
119119
// Take care when messing with the truncation, it's extremely finnicky
120120
function createPlaygroundEmbed(
121121
author: User,
122-
[_url, query, code]: RegExpExecArray,
122+
{ url: _url, query, code }: PlaygroundLinkMatch,
123123
url: string = _url,
124124
) {
125125
const embed = new MessageEmbed()

src/util/codeBlocks.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@ 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 = { url: string; query: string; code: string };
10+
export function matchPlaygroundLink(
11+
msg: string,
12+
): PlaygroundLinkMatch | undefined {
13+
const match = msg.match(PLAYGROUND_REGEX);
14+
if (!match) return;
15+
const [url, query, code] = match;
16+
return { url, query, code };
17+
}
818

919
export async function findCode(message: Message, ignoreLinks = false) {
1020
const codeInMessage = await findCodeInMessage(message, ignoreLinks);
@@ -43,15 +53,12 @@ async function findCodeInMessage(msg: Message, ignoreLinks = false) {
4353

4454
if (ignoreLinks) return;
4555

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

51-
for (const embed of embeds) {
52-
const match = embed.url?.match(PLAYGROUND_REGEX);
58+
for (const code of codeSources) {
59+
const match = code && matchPlaygroundLink(code);
5360
if (match) {
54-
return decompressFromEncodedURIComponent(match[2]);
61+
return decompressFromEncodedURIComponent(match.code);
5562
}
5663
}
5764
}

0 commit comments

Comments
 (0)